Skip to content

Commit

Permalink
Upd: improve sds_catchar implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed Sep 17, 2024
1 parent 9f9bf31 commit 64145ad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/lib/sds_extras.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,9 @@ sds sds_catjsonchar(sds s, const char c) {
*/
sds sds_catchar(sds s, const char c) {
// Make sure there is always space for at least 1 char.
if (sdsavail(s) == 0) {
s = sdsMakeRoomFor(s, 1);
s = sdsMakeRoomFor(s, 1);
if (s == NULL) {
return NULL;
}
size_t i = sdslen(s);
s[i++] = c;
Expand Down
6 changes: 6 additions & 0 deletions test/tests/test_sds_extras.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ UTEST(sds_extras, test_sds_catchar) {
s = sds_catchar(s, 'a');
ASSERT_STREQ("a", s);
ASSERT_EQ(strlen(s), sdslen(s));

s = sdscat(s, "bcdefghijklmnopqrstuvwxy");
s = sds_catchar(s, 'z');
ASSERT_STREQ("abcdefghijklmnopqrstuvwxyz", s);
ASSERT_EQ(strlen(s), sdslen(s));

sdsfree(s);
}

Expand Down

0 comments on commit 64145ad

Please sign in to comment.