Skip to content

Commit

Permalink
Merge RichString_extendLen() back to RichString_setLen()
Browse files Browse the repository at this point in the history
  • Loading branch information
Explorer09 committed Feb 5, 2025
1 parent 64adba2 commit e6cd415
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions RichString.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,36 @@ 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;
}
}
}

RichString_setChar(this, len, 0);
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);
}
Expand Down

0 comments on commit e6cd415

Please sign in to comment.