@@ -42,9 +42,6 @@ class cbuf {
4242 if (_end >= _begin) {
4343 return _size - (_end - _begin) - 1 ;
4444 }
45- if (_begin == _end) {
46- return _size;
47- }
4845 return _begin - _end - 1 ;
4946 }
5047
@@ -62,7 +59,7 @@ class cbuf {
6259 if (getSize () == 0 ) return -1 ;
6360
6461 char result = *_begin;
65- if (++ _begin == _bufend) _begin = _buf ;
62+ _begin = wrap_if_bufend ( _begin + 1 ) ;
6663 return static_cast <int >(result);
6764 }
6865
@@ -78,16 +75,15 @@ class cbuf {
7875 dst += top_size;
7976 }
8077 memcpy (dst, _begin, size_to_read);
81- _begin += size_to_read;
82- if (_begin == _bufend) _begin = _buf;
78+ _begin = wrap_if_bufend (_begin + size_to_read);
8379 return size_read;
8480 }
8581
8682 size_t write (char c) {
8783 if (room () == 0 ) return 0 ;
8884
8985 *_end = c;
90- if (++ _end == _bufend) _end = _buf ;
86+ _end = wrap_if_bufend ( _end + 1 ) ;
9187 return 1 ;
9288 }
9389
@@ -103,8 +99,7 @@ class cbuf {
10399 src += top_size;
104100 }
105101 memcpy (_end, src, size_to_write);
106- _end += size_to_write;
107- if (_end == _bufend) _end = _buf;
102+ _end = wrap_if_bufend (_end + size_to_write);
108103 return size_written;
109104 }
110105
@@ -114,6 +109,10 @@ class cbuf {
114109 }
115110
116111 private:
112+ inline char * wrap_if_bufend (char * ptr) {
113+ return (ptr == _bufend) ? _buf : ptr;
114+ }
115+
117116 size_t _size;
118117 char * _buf;
119118 char * _bufend;
0 commit comments