diff options
| author | Matthias Melcher <github@matthiasm.com> | 2022-12-22 00:18:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-22 00:18:01 +0100 |
| commit | d98c6638938b09934889b5c639a56d4dea10a6fb (patch) | |
| tree | 58bd129f6e21535a483e995ac3325264c8ec1347 /src/Fl_String.cxx | |
| parent | 1d212b7a0394fb72e76b3e713e2aa949cea4d612 (diff) | |
Fix compilation on old gcc (#606)
* Fixing char* use in FLUID
* Fixing const cast
Diffstat (limited to 'src/Fl_String.cxx')
| -rw-r--r-- | src/Fl_String.cxx | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/src/Fl_String.cxx b/src/Fl_String.cxx index 380493b21..59f7ace64 100644 --- a/src/Fl_String.cxx +++ b/src/Fl_String.cxx @@ -69,7 +69,12 @@ Fl_String::~Fl_String() { delete[] value_; } -void Fl_String::alloc_buf(int size) { +/** Grow the buffer size to at least size+1 bytes. + By default, this call destroys the contents of the current buffer. + \param size in bytes + \param preserve_text copy existing text into the new buffer + */ +void Fl_String::alloc_buf(int size, bool preserve_text) { if (size < 0) return; if (size > 0 && size <= capacity_) @@ -79,7 +84,14 @@ void Fl_String::alloc_buf(int size) { char *new_value = new char[new_size]; capacity_ = new_size - 1; - size_ = 0; + if (preserve_text) { + size_ = (int)strlen(value_); + // the new buffer always has a higher capacity than the old one + // make sure we copy the trailing NUL. + memcpy(new_value, value_, size_+1); + } else { + size_ = 0; + } delete[] value_; value_ = new_value; } @@ -111,6 +123,15 @@ int Fl_String::capacity() const { return capacity_; // > 0 ? capacity_ - 1 : capacity_; } +/** Set the minumum capacity to num_bytes plus one for a terminating NUL. + The cintents of the string buffer will be copied if needed. + \param num_bytes minimum size of buffer + */ +void Fl_String::capacity(int num_bytes) { + alloc_buf(num_bytes, true); +} + + void Fl_String::release() { delete[] value_; value_ = 0; |
