Skip to content

Commit

Permalink
Merge pull request #7900 from yosefe/topic/ucs-string-test-fix-ucs-st…
Browse files Browse the repository at this point in the history
…ring-buffer

UCS/STRING/TEST: Fix ucs_string_buffer_rtrim() for empty string
  • Loading branch information
yosefe authored Feb 5, 2022
2 parents 0dc3d28 + ca57867 commit 98f5270
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/ucs/datastruct/string_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ void ucs_string_buffer_rtrim(ucs_string_buffer_t *strb, const char *charset)
{
char *ptr = ucs_array_end(&strb->str);

while (ucs_array_length(&strb->str) > 0) {
if (ucs_array_is_empty(&strb->str)) {
/* If the string is empty, do not write '\0' terminator */
return;
}

do {
--ptr;
if (((charset == NULL) && !isspace(*ptr)) ||
((charset != NULL) && (strchr(charset, *ptr) == NULL))) {
Expand All @@ -155,7 +160,7 @@ void ucs_string_buffer_rtrim(ucs_string_buffer_t *strb, const char *charset)
}

ucs_array_set_length(&strb->str, ucs_array_length(&strb->str) - 1);
}
} while (!ucs_array_is_empty(&strb->str));

/* mark the new end of string */
*(ptr + 1) = '\0';
Expand Down
6 changes: 5 additions & 1 deletion test/gtest/ucs/test_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class test_string_buffer : public ucs::test {
void check_extract_mem(ucs_string_buffer_t *strb);
};


UCS_TEST_F(test_string_buffer, appendf) {
ucs_string_buffer_t strb;

Expand Down Expand Up @@ -200,6 +199,11 @@ UCS_TEST_F(test_string_buffer, rtrim) {
static const char *test_string = "wabbalubbadabdab";
ucs_string_buffer_t strb;

ucs_string_buffer_init(&strb);
ucs_string_buffer_rtrim(&strb, "x");
EXPECT_EQ(std::string(""), ucs_string_buffer_cstr(&strb));
ucs_string_buffer_cleanup(&strb);

ucs_string_buffer_init(&strb);
ucs_string_buffer_appendf(&strb, "%s%s", test_string, ",,");
ucs_string_buffer_rtrim(&strb, ",");
Expand Down

0 comments on commit 98f5270

Please sign in to comment.