refactor(string_utils)!: rename skip_blanks to trim_blank_left and update signature

This commit is contained in:
william.valenduc 2026-01-12 17:41:24 +00:00
parent b7dbf57dfb
commit d1f4e0e88d
3 changed files with 46 additions and 39 deletions

View file

@ -3,17 +3,13 @@
#include <ctype.h> #include <ctype.h>
#include <stddef.h> #include <stddef.h>
ssize_t skip_blanks(char **str) char *trim_blank_left(char *str)
{ {
if (str == NULL || *str == NULL) if (str == NULL)
{ return NULL;
return 0;
} while (*str != '\0' && isblank(*str))
ssize_t skipped = 0; str++;
while (*str[skipped] != '\0' && !isblank(str[skipped]))
{ return str;
skipped++;
}
*str += skipped;
return skipped;
} }

View file

@ -4,9 +4,12 @@
#include <sys/types.h> #include <sys/types.h>
/* /*
* @brief: skips blank characters at the beginning of [str]. * @brief trims leading blank characters (space and tab) from the input string.
* @return: number of characters skipped. * @param str input string to be trimmed.
* @return pointer to the first non-blank character in the string. If the
* string consists entirely of blank characters, returns a pointer to the null
* terminator at the end of the string.
*/ */
ssize_t skip_blanks(char **str); char *trim_blank_left(char *str);
#endif /* STRING_UTILS_H */ #endif /* STRING_UTILS_H */

View file

@ -5,7 +5,7 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include "utils/string_utils/string_utils.h" #include "../../../src/utils/string_utils/string_utils.h"
TestSuite(string_utils); TestSuite(string_utils);
@ -14,10 +14,11 @@ Test(string_utils, skipblank_basic)
char input[] = " Hello World"; char input[] = " Hello World";
char expected_str[] = "Hello World"; char expected_str[] = "Hello World";
ssize_t actual = skip_blanks(input); char *trimmed = trim_blank_left(input);
ssize_t offset = trimmed - input;
ssize_t expected = 2; ssize_t expected = 2;
cr_expect(eq(str, input, expected_str)); cr_expect(eq(str, trimmed, expected_str));
cr_expect(actual == expected); cr_expect(offset == expected);
} }
Test(string_utils, skipblank_noblank) Test(string_utils, skipblank_noblank)
@ -25,10 +26,11 @@ Test(string_utils, skipblank_noblank)
char input[] = "Hello World"; char input[] = "Hello World";
char expected_str[] = "Hello World"; char expected_str[] = "Hello World";
ssize_t actual = skip_blanks(input); char *trimmed = trim_blank_left(input);
ssize_t offset = trimmed - input;
ssize_t expected = 0; ssize_t expected = 0;
cr_expect(eq(str, input, expected_str)); cr_expect(eq(str, trimmed, expected_str));
cr_expect(actual == expected); cr_expect(offset == expected);
} }
Test(string_utils, skipblank_tab) Test(string_utils, skipblank_tab)
@ -36,10 +38,11 @@ Test(string_utils, skipblank_tab)
char input[] = "\tHello World"; char input[] = "\tHello World";
char expected_str[] = "Hello World"; char expected_str[] = "Hello World";
ssize_t actual = skip_blanks(input); char *trimmed = trim_blank_left(input);
ssize_t offset = trimmed - input;
ssize_t expected = 1; ssize_t expected = 1;
cr_expect(eq(str, input, expected_str)); cr_expect(eq(str, trimmed, expected_str));
cr_expect(actual == expected); cr_expect(offset == expected);
} }
Test(string_utils, skipblank_space_tab) Test(string_utils, skipblank_space_tab)
@ -47,10 +50,11 @@ Test(string_utils, skipblank_space_tab)
char input[] = " \tHello World"; char input[] = " \tHello World";
char expected_str[] = "Hello World"; char expected_str[] = "Hello World";
ssize_t actual = skip_blanks(input); char *trimmed = trim_blank_left(input);
ssize_t offset = trimmed - input;
ssize_t expected = 2; ssize_t expected = 2;
cr_expect(eq(str, input, expected_str)); cr_expect(eq(str, trimmed, expected_str));
cr_expect(actual == expected); cr_expect(offset == expected);
} }
Test(string_utils, skipblank_2tab_1space) Test(string_utils, skipblank_2tab_1space)
@ -58,10 +62,11 @@ Test(string_utils, skipblank_2tab_1space)
char input[] = "\t \tHello World"; char input[] = "\t \tHello World";
char expected_str[] = "Hello World"; char expected_str[] = "Hello World";
ssize_t actual = skip_blanks(input); char *trimmed = trim_blank_left(input);
ssize_t offset = trimmed - input;
ssize_t expected = 3; ssize_t expected = 3;
cr_expect(eq(str, input, expected_str)); cr_expect(eq(str, trimmed, expected_str));
cr_expect(actual == expected); cr_expect(offset == expected);
} }
Test(string_utils, skipblank_a_lot) Test(string_utils, skipblank_a_lot)
@ -69,10 +74,11 @@ Test(string_utils, skipblank_a_lot)
char input[] = "\t \t \tHello World"; char input[] = "\t \t \tHello World";
char expected_str[] = "Hello World"; char expected_str[] = "Hello World";
ssize_t actual = skip_blanks(input); char *trimmed = trim_blank_left(input);
ssize_t offset = trimmed - input;
ssize_t expected = 8; ssize_t expected = 8;
cr_expect(eq(str, input, expected_str)); cr_expect(eq(str, trimmed, expected_str));
cr_expect(actual == expected); cr_expect(offset == expected);
} }
Test(string_utils, skipblank_newline) Test(string_utils, skipblank_newline)
@ -80,18 +86,20 @@ Test(string_utils, skipblank_newline)
char input[] = "\nHello World"; char input[] = "\nHello World";
char expected_str[] = "\nHello World"; char expected_str[] = "\nHello World";
ssize_t actual = skip_blanks(input); char *trimmed = trim_blank_left(input);
ssize_t offset = trimmed - input;
ssize_t expected = 0; ssize_t expected = 0;
cr_expect(eq(str, input, expected_str)); cr_expect(eq(str, trimmed, expected_str));
cr_expect(actual == expected); cr_expect(offset == expected);
} }
Test(string_utils, skipblank_nul) Test(string_utils, skipblank_nul)
{ {
char *input = NULL; char *input = NULL;
ssize_t actual = skip_blanks(input); char *trimmed = trim_blank_left(input);
ssize_t offset = trimmed - input;
ssize_t expected = 0; ssize_t expected = 0;
cr_expect(input == NULL); cr_expect(input == NULL);
cr_expect(actual == expected); cr_expect(offset == expected);
} }