frang75 / nappgui_src

SDK for building cross-platform desktop apps in ANSI-C
https://www.nappgui.com
MIT License
503 stars 50 forks source link

Bug in str_splits #124

Closed SamSandq closed 7 months ago

SamSandq commented 7 months ago

The function str_splits does not work with empty splits. This works:

    const char_t *test1 = "1|2|3|4";

    ArrPt(String) *pieces1 = str_splits(test1, "|", FALSE);
    bstd_printf("Size: %d\n", arrpt_size(pieces1, String));

    arrpt_foreach(c, pieces1, String);
        bstd_printf("%s ", tc(c));
    arrpt_end();

it prints

Size: 4
1 2 3 4

However, this does not:

    const char_t *test2 = "1|2||4";

    ArrPt(String) *pieces2 = str_splits(test2, "|", FALSE);
    bstd_printf("Size: %d\n", arrpt_size(pieces2, String));

    arrpt_foreach(c, pieces2, String);
        bstd_printf("% \n", tc(c));
    arrpt_end();

It prints:

Size: 3
1 2 4

But it should handle the two consecutive separators correctly, and print:

Size: 4
1 2  4 

(i.e., the 3rd piece is the empty string).

The trim flag does not affect this behaviour, except that it's actually worse: for a string "1|2| |4" it seems to trim first, and then decide to ignore the empty 3rd piece, and print the size as 3, and the pieces as the incorrect example above.

frang75 commented 7 months ago

Fixed in commit: https://github.com/frang75/nappgui_src/commit/845c4c01ca282abbe6751ed51cb6a28b1860076f The trim and add_empty are diferent operations. Added a new function parameter. https://nappgui.com/en/core/string.html#f52

SamSandq commented 7 months ago

Nice solution, even if not backwards compatible.

frang75 commented 7 months ago

Yes it's correct. I am considering introducing a deprecation system. But, for the moment, it will bring more complications than benefits. In general, the public API is quite stable.