Append to the string. Note second parameter is the size, not the length. This means that a null terminator is not added unless the input contains it. If there's already a null terminator, it's not discarded before appending! If there's space used, a newline is optionally used as separator between the existing content and the new input. The internal buffer is not grown; the input will be truncated.
| 59 | // The internal buffer is not grown; the input will be truncated. |
| 60 | // The amount of written bytes (the ones that fit) is returned. |
| 61 | size_t Extender::append(const char* s, size_t s_size, bool newline) |
| 62 | { |
| 63 | if (m_pos >= m_buf + m_size) // Full buffer? |
| 64 | return 0; |
| 65 | |
| 66 | const size_t extra = m_buf < m_pos && newline ? 1 : 0; |
| 67 | |
| 68 | if (m_pos + s_size + extra > m_buf + m_size) // Adjust the argument, truncating it. |
| 69 | s_size = m_buf + m_size - m_pos - extra; |
| 70 | |
| 71 | // Do not append newline if there's nothing before or the caller doesn't want it. |
| 72 | if (extra) |
| 73 | *m_pos++ = '\n'; |
| 74 | |
| 75 | memcpy(m_pos, s, s_size); |
| 76 | m_pos += s_size; |
| 77 | return s_size + extra; |
| 78 | } |
| 79 | |
| 80 | // Unlike alloc() and allocFill(), this is not a destructive operation. |
| 81 | // If the requested size is not bigger than the existing one, nothing happens and |
no outgoing calls