From 137d2c97914fd9ac4b79869185c94c402d7719ab Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 6 Feb 2022 12:24:40 +0100 Subject: [PATCH] Remove confusing sc_str_truncate() This util function was error-prone: - it accepted a buffer as parameter (not necessarily a NUL-terminated string) and its length (including the NUL char, if any); - it wrote '\0' over the last character of the buffer, so the last character was lost if the buffer was not a NUL-terminated string, and even worse, it caused undefined behavior if the length was empty; - it returned the length of the resulting NUL-terminated string, which was inconsistent with the input buffer length. In addition, it was not necessarily optimal: - it wrote '\0' twice; - it required to know the buffer length, that is the input string length + 1, in advance. Remove this function, and let the client use strcspn() manually. --- app/src/util/str.c | 8 -------- app/src/util/str.h | 11 ----------- app/tests/test_str.c | 27 --------------------------- 3 files changed, 46 deletions(-) diff --git a/app/src/util/str.c b/app/src/util/str.c index 2d67f816..d78aa9d7 100644 --- a/app/src/util/str.c +++ b/app/src/util/str.c @@ -297,14 +297,6 @@ error: return NULL; } -size_t -sc_str_truncate(char *data, size_t len, const char *endchars) { - data[len - 1] = '\0'; - size_t idx = strcspn(data, endchars); - data[idx] = '\0'; - return idx; -} - ssize_t sc_str_index_of_column(const char *s, unsigned col, const char *seps) { size_t colidx = 0; diff --git a/app/src/util/str.h b/app/src/util/str.h index dfe0cb30..1736bd95 100644 --- a/app/src/util/str.h +++ b/app/src/util/str.h @@ -103,17 +103,6 @@ sc_str_from_wchars(const wchar_t *s); char * sc_str_wrap_lines(const char *input, unsigned columns, unsigned indent); -/** - * Truncate the data after any of the characters from `endchars` - * - * An '\0' is always written at the end of the data string, even if no - * character from `endchars` is encountered. - * - * Return the size of the resulting string (as strlen() would return). - */ -size_t -sc_str_truncate(char *data, size_t len, const char *endchars); - /** * Find the start of a column in a string * diff --git a/app/tests/test_str.c b/app/tests/test_str.c index cc3039e7..4fe8a1df 100644 --- a/app/tests/test_str.c +++ b/app/tests/test_str.c @@ -338,32 +338,6 @@ static void test_wrap_lines(void) { free(formatted); } -static void test_truncate(void) { - char s[] = "hello\nworld\n!"; - size_t len = sc_str_truncate(s, sizeof(s), "\n"); - - assert(len == 5); - assert(!strcmp("hello", s)); - - char s2[] = "hello\r\nworkd\r\n!"; - len = sc_str_truncate(s2, sizeof(s2), "\n\r"); - - assert(len == 5); - assert(!strcmp("hello", s)); - - char s3[] = "hello world\n!"; - len = sc_str_truncate(s3, sizeof(s3), " \n\r"); - - assert(len == 5); - assert(!strcmp("hello", s3)); - - char s4[] = "hello "; - len = sc_str_truncate(s4, sizeof(s4), " \n\r"); - - assert(len == 5); - assert(!strcmp("hello", s4)); -} - static void test_index_of_column(void) { assert(sc_str_index_of_column("a bc d", 0, " ") == 0); assert(sc_str_index_of_column("a bc d", 1, " ") == 2); @@ -417,7 +391,6 @@ int main(int argc, char *argv[]) { test_parse_integer_with_suffix(); test_strlist_contains(); test_wrap_lines(); - test_truncate(); test_index_of_column(); test_remove_trailing_cr(); return 0;