| 20 | namespace dena { |
| 21 | |
| 22 | struct string_buffer : private noncopyable { |
| 23 | string_buffer() : buffer(0), begin_offset(0), end_offset(0), alloc_size(0) { } |
| 24 | ~string_buffer() { |
| 25 | DENA_FREE(buffer); |
| 26 | } |
| 27 | const char *begin() const { |
| 28 | return buffer + begin_offset; |
| 29 | } |
| 30 | const char *end() const { |
| 31 | return buffer + end_offset; |
| 32 | } |
| 33 | char *begin() { |
| 34 | return buffer + begin_offset; |
| 35 | } |
| 36 | char *end() { |
| 37 | return buffer + end_offset; |
| 38 | } |
| 39 | size_t size() const { |
| 40 | return end_offset - begin_offset; |
| 41 | } |
| 42 | void clear() { |
| 43 | begin_offset = end_offset = 0; |
| 44 | } |
| 45 | void resize(size_t len) { |
| 46 | if (size() < len) { |
| 47 | reserve(len); |
| 48 | memset(buffer + end_offset, 0, len - size()); |
| 49 | } |
| 50 | end_offset = begin_offset + len; |
| 51 | } |
| 52 | void reserve(size_t len) { |
| 53 | if (alloc_size >= begin_offset + len) { |
| 54 | return; |
| 55 | } |
| 56 | size_t asz = alloc_size; |
| 57 | while (asz < begin_offset + len) { |
| 58 | if (asz == 0) { |
| 59 | asz = 16; |
| 60 | } |
| 61 | const size_t asz_n = asz << 1; |
| 62 | if (asz_n < asz) { |
| 63 | fatal_abort("string_buffer::resize() overflow"); |
| 64 | } |
| 65 | asz = asz_n; |
| 66 | } |
| 67 | void *const p = DENA_REALLOC(buffer, asz); |
| 68 | if (p == 0) { |
| 69 | fatal_abort("string_buffer::resize() realloc"); |
| 70 | } |
| 71 | buffer = static_cast<char *>(p); |
| 72 | alloc_size = asz; |
| 73 | } |
| 74 | void erase_front(size_t len) { |
| 75 | if (len >= size()) { |
| 76 | clear(); |
| 77 | } else { |
| 78 | begin_offset += len; |
| 79 | } |
nothing calls this directly
no outgoing calls
no test coverage detected