Unlike alloc() and allocFill(), this is not a destructive operation. If the requested size is not bigger than the existing one, nothing happens and the insertion point remains in its current position. Otherwise, reallocation happens and the old contents are copied into the new buffer. Notice only getUsed() bytes are copied. This means a fill pattern will not be preserved. The insertion point is up
| 86 | // to the same position in the string where it was before growing the buffer. |
| 87 | // The position at the insertion point is initialized to zero to aid in debugging. |
| 88 | void Extender::grow(size_t n) |
| 89 | { |
| 90 | if (m_pos == m_buf) |
| 91 | { |
| 92 | // We don't have data to preserve, go faster. |
| 93 | alloc(n); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (m_size < n) |
| 98 | { |
| 99 | const size_t old_pos = getUsed(); |
| 100 | char* const old_buf = m_buf; |
| 101 | |
| 102 | m_buf = FB_NEW char[m_size = n]; |
| 103 | memcpy(m_buf, old_buf, old_pos); // Copy only the used bytes. |
| 104 | m_pos = m_buf + old_pos; // Reposition the current insertion point. |
| 105 | m_pos[0] = 0; // Same as alloc(). |
| 106 | |
| 107 | delete[] old_buf; |
| 108 | } |
| 109 | } |
| 110 |
no outgoing calls
no test coverage detected