From e6cd4158e79592d2bcfe0fe0b921df4b164b4a87 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Thu, 6 Feb 2025 03:39:18 +0800 Subject: [PATCH] Merge RichString_extendLen() back to RichString_setLen() --- RichString.c | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/RichString.c b/RichString.c index afdb49b48..1a6b22550 100644 --- a/RichString.c +++ b/RichString.c @@ -21,27 +21,29 @@ in the source distribution for its full text. #define charBytes(n) (sizeof(CharType) * (n)) -static void RichString_extendLen(RichString* this, int len) { - if (this->chptr == this->chstr) { - // String is in internal buffer - if (len > RICHSTRING_MAXLEN) { - // Copy from internal buffer to allocated string - this->chptr = xMalloc(charBytes(len + 1)); - memcpy(this->chptr, this->chstr, charBytes(this->chlen)); - } else { - // Still fits in internal buffer, do nothing - assert(this->chlen <= RICHSTRING_MAXLEN); - } - } else { - // String is managed externally - if (len > RICHSTRING_MAXLEN) { - // Just reallocate the buffer accordingly - this->chptr = xRealloc(this->chptr, charBytes(len + 1)); +static void RichString_setLen(RichString* this, int len) { + if (len > RICHSTRING_MAXLEN || this->chlen > RICHSTRING_MAXLEN) { + if (this->chptr == this->chstr) { + // String is in internal buffer + if (len > RICHSTRING_MAXLEN) { + // Copy from internal buffer to allocated string + this->chptr = xMalloc(charBytes(len + 1)); + memcpy(this->chptr, this->chstr, charBytes(this->chlen)); + } else { + // Still fits in internal buffer, do nothing + assert(this->chlen <= RICHSTRING_MAXLEN); + } } else { - // Move string into internal buffer and free resources - memcpy(this->chstr, this->chptr, charBytes(len)); - free(this->chptr); - this->chptr = this->chstr; + // String is managed externally + if (len > RICHSTRING_MAXLEN) { + // Just reallocate the buffer accordingly + this->chptr = xRealloc(this->chptr, charBytes(len + 1)); + } else { + // Move string into internal buffer and free resources + memcpy(this->chstr, this->chptr, charBytes(len)); + free(this->chptr); + this->chptr = this->chstr; + } } } @@ -49,15 +51,6 @@ static void RichString_extendLen(RichString* this, int len) { this->chlen = len; } -static void RichString_setLen(RichString* this, int len) { - if (len <= RICHSTRING_MAXLEN && this->chlen <= RICHSTRING_MAXLEN) { - RichString_setChar(this, len, 0); - this->chlen = len; - } else { - RichString_extendLen(this, len); - } -} - void RichString_rewind(RichString* this, int count) { RichString_setLen(this, this->chlen - count); }