25 #ifndef DOZERG_CHAR_BUFFER_H_20121218 26 #define DOZERG_CHAR_BUFFER_H_20121218 35 #include "impl/environment.hh" 48 template<
typename CharT>
54 typedef std::char_traits<__Char> traits_type;
55 typedef typename traits_type::char_type value_type;
56 typedef value_type & reference;
57 typedef const value_type & const_reference;
58 typedef value_type * pointer;
59 typedef const value_type * const_pointer;
60 typedef __gnu_cxx::__normal_iterator<
62 typedef __gnu_cxx::__normal_iterator<
64 typedef size_t size_type;
65 typedef ptrdiff_t difference_type;
66 typedef std::reverse_iterator<iterator> reverse_iterator;
67 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
68 static const size_type npos =
static_cast<size_type
>(-1);
85 , capa_(traits_type::
length(buf))
113 sz_ = std::min(
size, capacity);
126 assign(other.buf_, other.capa_, other.sz_);
133 void swap(__Myt & other)
throw() {
135 std::swap(buf_, other.buf_);
136 std::swap(capa_, other.capa_);
137 std::swap(sz_, other.sz_);
147 iterator
begin(){
return iterator(&buf_[0]);}
153 iterator
end(){
return iterator(&buf_[sz_]);}
158 const_iterator
begin()
const{
return const_iterator(&buf_[0]);}
164 const_iterator
end()
const{
return const_iterator(&buf_[sz_]);}
169 reverse_iterator
rbegin(){
return reverse_iterator(
end());}
174 reverse_iterator
rend(){
return reverse_iterator(
begin());}
179 const_reverse_iterator
rbegin()
const{
return const_reverse_iterator(
end());}
184 const_reverse_iterator
rend()
const{
return const_reverse_iterator(
begin());}
205 size_type
size()
const{
return sz_;}
258 reference
at(size_type i){
260 check_offset_throw(i);
269 const_reference
at(size_type i)
const{
271 check_offset_throw(i);
279 const_pointer
c_str()
const{
return buf_;}
285 const_pointer
data()
const{
return buf_;}
305 void resize(size_type count, value_type val = 0){
306 check_capa_throw(count);
309 assign_aux(buf_ + sz_, count - sz_, val);
325 size_type
copy(pointer buf, size_type count, size_type offset = 0)
const{
326 check_size_throw(offset);
327 if(offset + count > sz_)
328 count = sz_ - offset;
331 copy_aux(buf, buf_ + offset, count);
341 check_capa_throw(sz_ + 1);
350 __Myt &
append(size_type count, value_type val){
return replace(sz_, 0, count, val);}
357 __Myt &
append(const_pointer buf, size_type count){
return replace(sz_, 0, buf, count);}
373 __Myt &
append(
const __Myt & other, size_type offset, size_type count){
return replace(sz_, 0, other, offset, count);}
412 __Myt &
insert(size_type offset, size_type count, value_type val){
return replace(offset, 0, count, val);}
420 __Myt &
insert(size_type offset, const_pointer buf, size_type count){
return replace(offset, 0, buf, count);}
428 __Myt &
insert(size_type offset, const_pointer buf){
return insert(offset, buf, traits_type::length(buf));}
439 __Myt &
insert(size_type offset,
const __Myt & other, size_type newOffset, size_type count){
440 return replace(offset, 0, other, newOffset, count);
448 __Myt &
insert(size_type offset,
const __Myt & other){
return insert(offset, other, 0, other.
size());}
455 iterator
insert(iterator pos, value_type val){
456 replace(pos, pos, size_type(1), val);
472 __Myt &
replace(size_type offset, size_type count, size_type newCount, value_type val){
473 check_size_range_throw(offset, count);
474 check_capa_throw(offset + newCount);
475 check_capa_throw(sz_ + newCount - count);
476 mutate_aux(offset, count, newCount);
479 assign_aux(buf_ + offset, newCount, val);
496 __Myt &
replace(size_type offset, size_type count, const_pointer buf, size_type newCount){
497 check_size_range_throw(offset, count);
498 check_capa_throw(offset + newCount);
499 check_capa_throw(sz_ + newCount - count);
500 mutate_aux(offset, count, newCount);
503 copy_aux(buf_ + offset, buf, newCount);
520 __Myt &
replace(size_type offset, size_type count, const_pointer buf){
521 return replace(offset, count, buf, traits_type::length(buf));
537 __Myt &
replace(size_type offset, size_type count,
const __Myt & other, size_type newOffset, size_type newCount){
538 other.check_size_throw(newOffset);
539 other.check_size_throw(newOffset + newCount);
540 return replace(offset, count, other.buf_ + newOffset, newCount);
554 __Myt &
replace(size_type offset, size_type count,
const __Myt & other){
555 return replace(offset, count, other, size_type(0), other.
size());
570 __Myt &
replace(iterator first, iterator last, size_type count, value_type val){
571 check_iter_range_throw(first, last);
572 return replace(size_type(first -
begin()), size_type(last - first), count, val);
587 __Myt &
replace(iterator first, iterator last, const_pointer buf, size_type count){
588 check_iter_range_throw(first, last);
589 return replace(size_type(first -
begin()), size_type(last - first), buf, count);
604 __Myt &
replace(iterator first, iterator last, const_pointer buf){
605 return replace(first, last, buf, size_type(traits_type::length(buf)));
621 __Myt &
replace(iterator first, iterator last,
const __Myt & other, size_type offset, size_type count){
622 check_iter_range_throw(first, last);
623 return replace(size_type(first -
begin()), size_type(last - first), other, offset, count);
637 __Myt &
replace(iterator first, iterator last,
const __Myt & other){
638 return replace(first, last, other, 0, other.
size());
646 iterator
erase(iterator first, iterator last){
647 replace(first, last, size_type(0), value_type(0));
656 replace(pos, pos + 1, size_type(0), value_type(0));
665 __Myt &
erase(size_type offset = 0, size_type count = npos){
666 check_offset_throw(offset);
667 if(offset + count > sz_)
668 count = sz_ - offset;
669 mutate_aux(offset, limit_count(offset, count), 0);
682 __Myt
substr(size_type offset = 0, size_type count = npos){
683 check_offset_throw(offset);
684 return __Myt(buf_ + offset, capa_ - offset, limit_count(offset, count));
699 int compare(size_type offset, size_type count, const_pointer buf, size_type newCount)
const{
700 check_size_range_throw(offset, count);
701 size_type c = std::min(count, newCount);
705 ret = traits_type::compare(buf_ + offset, buf, c);
708 ret = (count < newCount ? -1 : (count > newCount ? 1 : 0));
723 int compare(size_type offset, size_type count, const_pointer buf)
const{
724 return compare(offset, count, buf, traits_type::length(buf));
752 int compare(size_type offset, size_type count,
const __Myt & other, size_type newOffset, size_type newCount)
const{
753 other.check_size_range_throw(newOffset, newCount);
754 return compare(offset, count, const_pointer(other.buf_ + newOffset), newCount);
768 int compare(size_type offset, size_type count,
const __Myt & other)
const{
769 return compare(offset, count, other, 0, other.
size());
785 void check_offset_throw(size_type i)
const{
787 throw std::out_of_range(
"CCharBuffer offset out of range");
789 void check_size_throw(size_type i)
const{
791 throw std::out_of_range(
"CCharBuffer size out of range");
793 void check_capa_throw(size_type i)
const{
795 throw std::out_of_range(
"CCharBuffer capacity out of range");
797 void check_iter_throw(iterator it)
const{
799 throw std::out_of_range(
"CCharBuffer iterator out of range");
801 void check_size_range_throw(size_type offset, size_type count)
const{
802 check_size_throw(offset);
804 check_size_throw(offset + count);
806 void check_iter_range_throw(iterator first, iterator last)
const{
807 assert(first <= last);
808 check_iter_throw(first);
810 check_iter_throw(last);
812 static void assign_aux(pointer pos, size_type count, value_type val){
815 traits_type::assign(*pos, val);
817 traits_type::assign(pos, count, val);
819 static void copy_aux(pointer dst, const_pointer src, size_type count){
822 traits_type::assign(*dst, *src);
824 traits_type::copy(dst, src, count);
826 static void move_aux(pointer dst, const_pointer src, size_type count){
829 traits_type::assign(*dst, *src);
831 traits_type::move(dst, src, count);
833 void mutate_aux(size_type offset, size_type count, size_type newCount){
834 assert(sz_ >= offset + count);
835 const size_type c = sz_ - offset - count;
836 if(c && count != newCount)
837 move_aux(buf_ + offset + newCount, buf_ + offset + count, c);
838 sz_ += newCount - count;
840 size_type limit_count(size_type offset, size_type count)
const{
841 const size_type c = sz_ - offset;
842 return std::min(c, count);
855 template<
typename CharT>
861 template<
typename CharT>
864 return (0 == right.
compare(left));
867 template<
typename CharT>
870 return (0 == left.
compare(right));
873 template<
typename CharT>
876 return !(left == right);
879 template<
typename CharT>
882 return !(left == right);
885 template<
typename CharT>
888 return !(left == right);
891 template<
typename CharT>
894 return (left.compare(right) < 0);
897 template<
typename CharT>
898 bool operator <(const CharT * left, const CCharBuffer<CharT> & right)
900 return (0 < right.compare(left));
903 template<
typename CharT>
904 bool operator <(const CCharBuffer<CharT> & left,
const CharT * right)
906 return (left.compare(right) < 0);
909 template<
typename CharT>
912 return !(left > right);
915 template<
typename CharT>
916 bool operator <=(const CharT * left, const CCharBuffer<CharT> & right)
918 return !(left > right);
921 template<
typename CharT>
922 bool operator <=(const CCharBuffer<CharT> & left,
const CharT * right)
924 return !(left > right);
927 template<
typename CharT>
930 return (right < left);
933 template<
typename CharT>
936 return (right < left);
939 template<
typename CharT>
942 return (right < left);
945 template<
typename CharT>
948 return !(left < right);
951 template<
typename CharT>
954 return !(left < right);
957 template<
typename CharT>
960 return !(left < right);
965 template<
typename CharT>
CCharBuffer()
Construct a null object with zero capacity.
Definition: char_buffer.hh:73
iterator end()
Get the ending of the data.
Definition: char_buffer.hh:153
__Myt & insert(size_type offset, const_pointer buf, size_type count)
Insert the content of a buffer.
Definition: char_buffer.hh:420
bool empty() const
Test if the data is empty.
Definition: char_buffer.hh:193
__Myt substr(size_type offset=0, size_type count=npos)
Get a substring.
Definition: char_buffer.hh:682
__Myt & append(const_pointer buf, size_type count)
Append content of a buffer to the end of the data.
Definition: char_buffer.hh:357
const_reverse_iterator rbegin() const
Get the ending of the data.
Definition: char_buffer.hh:179
__Myt & replace(size_type offset, size_type count, const_pointer buf)
Replace a range of bytes with the content of a C-style string.
Definition: char_buffer.hh:520
void resize(size_type count, value_type val=0)
Resize the data.
Definition: char_buffer.hh:305
size_type copy(pointer buf, size_type count, size_type offset=0) const
Copy content of a buffer to self.
Definition: char_buffer.hh:325
size_type length() const
Get the size of the data, same as size().
Definition: char_buffer.hh:199
int compare(size_type offset, size_type count, const __Myt &other) const
Compare a range of bytes with another CCharBuffer object.
Definition: char_buffer.hh:768
__Myt & insert(size_type offset, const __Myt &other)
Insert the content of another CCharBuffer.
Definition: char_buffer.hh:448
__Myt & erase(size_type offset=0, size_type count=npos)
Erase a range of bytes.
Definition: char_buffer.hh:665
__Myt & append(const __Myt &other)
Append content of another CCharBuffer content to the end of the data.
Definition: char_buffer.hh:379
reference operator[](size_type i)
Access a byte.
Definition: char_buffer.hh:225
Provide interfaces similar to std::string for raw byte array.
Definition: char_buffer.hh:49
int compare(const_pointer buf) const
Compare self with a C-style string lexicographically.
Definition: char_buffer.hh:736
__Myt & replace(iterator first, iterator last, size_type count, value_type val)
Replace a range of bytes with a number of new bytes.
Definition: char_buffer.hh:570
iterator erase(iterator first, iterator last)
Erase a range of bytes.
Definition: char_buffer.hh:646
const_reference back() const
Access the last byte.
Definition: char_buffer.hh:251
__Myt & assign(pointer buf, size_type capacity, size_type size=0)
Set this object to manage a byte buffer.
Definition: char_buffer.hh:110
__Myt & append(size_type count, value_type val)
Append a number of bytes to the end of the data.
Definition: char_buffer.hh:350
const_pointer c_str() const
Get the underlying C-style string.
Definition: char_buffer.hh:279
size_type size() const
Get the size of the data.
Definition: char_buffer.hh:205
__Myt & insert(size_type offset, size_type count, value_type val)
Insert a number of bytes.
Definition: char_buffer.hh:412
const_iterator end() const
Get the ending of the data.
Definition: char_buffer.hh:164
__Myt & assign(const __Myt &other)
Set this object to manage a buffer from another object.
Definition: char_buffer.hh:124
const_iterator begin() const
Get the beginning of the data.
Definition: char_buffer.hh:158
const_reference at(size_type i) const
Access a certain byte.
Definition: char_buffer.hh:269
reverse_iterator rend()
Get the beginning of the data.
Definition: char_buffer.hh:174
size_type capacity() const
Get the capacity of the underlying byte buffer.
Definition: char_buffer.hh:210
__Myt & replace(iterator first, iterator last, const __Myt &other)
Replace a range of bytes with the content of another CCharBuffer.
Definition: char_buffer.hh:637
size_type max_size() const
Get the maximum number of bytes.
Definition: char_buffer.hh:215
__Myt & replace(size_type offset, size_type count, const_pointer buf, size_type newCount)
Replace a range of bytes with the content of a byte buffer.
Definition: char_buffer.hh:496
int compare(const __Myt &other) const
Compare self with another CCharBuffer object.
Definition: char_buffer.hh:781
__Myt & insert(size_type offset, const_pointer buf)
Insert a C-style string.
Definition: char_buffer.hh:428
iterator erase(iterator pos)
Erase a byte.
Definition: char_buffer.hh:655
__Myt & append(const __Myt &other, size_type offset, size_type count)
Append content of another CCharBuffer content to the end of the data.
Definition: char_buffer.hh:373
__Myt & replace(iterator first, iterator last, const_pointer buf, size_type count)
Replace a range of bytes with the content of a byte buffer.
Definition: char_buffer.hh:587
__Myt & replace(size_type offset, size_type count, const __Myt &other)
Replace a range of bytes with the content of another CCharBuffer.
Definition: char_buffer.hh:554
const_pointer data() const
Get the data pointer.
Definition: char_buffer.hh:285
reference back()
Access the last byte.
Definition: char_buffer.hh:246
void push_back(value_type val)
Append a byte to the end of the data.
Definition: char_buffer.hh:339
const_reference front() const
Access the first byte.
Definition: char_buffer.hh:241
__Myt & replace(size_type offset, size_type count, size_type newCount, value_type val)
Replace a range of bytes with a number of new bytes.
Definition: char_buffer.hh:472
int compare(size_type offset, size_type count, const_pointer buf, size_type newCount) const
Compare a range of bytes with a buffer lexicographically.
Definition: char_buffer.hh:699
void clear()
Empty the data.
Definition: char_buffer.hh:297
__Myt & replace(size_type offset, size_type count, const __Myt &other, size_type newOffset, size_type newCount)
Replace a range of bytes with the content of another CCharBuffer.
Definition: char_buffer.hh:537
reverse_iterator rbegin()
Get the ending of the data.
Definition: char_buffer.hh:169
reference at(size_type i)
Access a certain byte.
Definition: char_buffer.hh:258
CCharBuffer(pointer buf)
Construct from a C-style string.
Definition: char_buffer.hh:83
__Myt & replace(iterator first, iterator last, const_pointer buf)
Replace a range of bytes with the content of a C-style string.
Definition: char_buffer.hh:604
__Myt & operator+=(const __Myt &other)
Append content of another CCharBuffer to the end of the data.
Definition: char_buffer.hh:386
const_reverse_iterator rend() const
Get the beginning of the data.
Definition: char_buffer.hh:184
int compare(size_type offset, size_type count, const_pointer buf) const
Compare a range of bytes with a C-style string lexicographically.
Definition: char_buffer.hh:723
reference front()
Access the first byte.
Definition: char_buffer.hh:236
int compare(size_type offset, size_type count, const __Myt &other, size_type newOffset, size_type newCount) const
Compare a range of bytes with another CCharBuffer object.
Definition: char_buffer.hh:752
iterator begin()
Get the beginning of the data.
Definition: char_buffer.hh:147
iterator insert(iterator pos, value_type val)
Insert a byte.
Definition: char_buffer.hh:455
CCharBuffer(pointer buf, size_type capacity, size_type size=0)
Construct from a byte buffer.
Definition: char_buffer.hh:96
void swap(__Myt &other)
Swap two CCharBuffer objects.
Definition: char_buffer.hh:133
__Myt & insert(size_type offset, const __Myt &other, size_type newOffset, size_type count)
Insert the content of another CCharBuffer.
Definition: char_buffer.hh:439
__Myt & append(const_pointer buf)
Append a C-style string to the end of the data.
Definition: char_buffer.hh:363
__Myt & replace(iterator first, iterator last, const __Myt &other, size_type offset, size_type count)
Replace a range of bytes with the content of another CCharBuffer.
Definition: char_buffer.hh:621