tezc / sc

Common libraries and data structures for C.
BSD 3-Clause "New" or "Revised" License
2.26k stars 245 forks source link

sc_str_append_fmt is trimming whitespaces at the end of the formated strings #51

Closed rafaeldelboni closed 3 years ago

rafaeldelboni commented 3 years ago

This is intended or is there something I'm missing?

Steps to reproduce:

Make a new test on string/str_test.c

void test7()
{
    char* s1;
    char* instruction = "RTS";
    s1 = sc_str_create(NULL);
    sc_str_append_fmt(&s1, "%s ", instruction);
    assert(sc_str_cmp(s1,"RTS "));
}

Error log:

17: sc_str_test: ./sc/string/str_test.c:516: test7: Assertion `sc_str_cmp(s1,"RTS ")' failed.
17/21 Test #17: sc_str_test ......................Subprocess aborted***Exception:   0.07 sec
sc_str_test: ./sc/string/str_test.c:516: test7: Assertion `sc_str_cmp(s1,"RTS ")' failed.
rafaeldelboni commented 3 years ago

Yes I was missing something haha

    char* s1;
    char* s2;
    char* instruction = "RTS";
    s1 = sc_str_create("");
    s2 = sc_str_create("RTS ");
    sc_str_append_fmt(&s1, "%s ", instruction);
    assert(sc_str_cmp(s1,s2));
tezc commented 3 years ago

Doesn't it create "(null) RTS " ? In our codebase, for these kind of scenarios we use :

void test7()
{
    char* s1;
    char* instruction = "RTS";
    s1 = sc_str_create(""); // Initial value
    sc_str_append_fmt(&s1, "%s ", instruction);
    assert(strcmp(s1,"RTS ") == 0); // equals zero
}

But I understand appending to null should create same string as if appending to empty string, at least it's the intuitive way I believe.

tezc commented 3 years ago

I'll think about that and check our usages, hopefully we can change this behaviour. Thanks a lot. :)