aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2021-08-06 16:20:30 -0300
committerEuAndreh <eu@euandre.org>2021-08-06 16:20:30 -0300
commit18188005336d6e15386e81e57a18d68336746bd5 (patch)
treebba394f6a7446926911b01c1dec3c59d571df2a0
parentsrc/gistatic.c: Trim trailing newline of description files (diff)
downloadgistatic-18188005336d6e15386e81e57a18d68336746bd5.tar.gz
gistatic-18188005336d6e15386e81e57a18d68336746bd5.tar.xz
src/gistatic.c: trim_newline() => strtrim(), trim more chars and a sequence of them
-rw-r--r--src/gistatic.c130
1 files changed, 116 insertions, 14 deletions
diff --git a/src/gistatic.c b/src/gistatic.c
index cfd5c55..052087b 100644
--- a/src/gistatic.c
+++ b/src/gistatic.c
@@ -816,7 +816,32 @@ static void test_escape_html() {
}
#endif
-static void trim_newline(char *const s) {
+static bool should_trim(const char c) {
+ return c == '\n' || c == '\t' || c == '\r' || c == ' ';
+}
+
+#ifdef TEST
+static void test_should_trim() {
+ test_start("test_should_trim");
+ {
+ testing("the \\0 null character");
+ assert(should_trim('\0') == false);
+ test_ok();
+ }
+ {
+ testing("the \\b character");
+ assert(should_trim('\b') == false);
+ test_ok();
+ }
+ {
+ testing("the space character");
+ assert(should_trim(' ') == true);
+ test_ok();
+ }
+}
+#endif
+
+static void strtrim(char *const s) {
if (s == NULL) {
return;
}
@@ -825,42 +850,118 @@ static void trim_newline(char *const s) {
return;
}
- const size_t len = strlen(s);
- if (s[len - 1] == '\n') {
+ size_t len = strlen(s);
+ while (len != 0 && should_trim(s[len - 1])) {
s[len - 1] = '\0';
+ len--;
+ }
+
+ size_t offset_count = 0;
+ while (should_trim(s[offset_count])) {
+ offset_count++;
+ };
+
+ if (offset_count == 0) {
+ return;
+ }
+
+ for (size_t i = 0; s[i]; i++) {
+ s[i] = s[i + offset_count];
}
}
#ifdef TEST
-static void test_trim_newline() {
- test_start("test_trim_newline");
+static void test_strtrim() {
+ test_start("test_strtrim");
{
testing("empty string");
char *const s = "";
- trim_newline(s);
+ strtrim(s);
assert(strcmp(s, "") == 0);
test_ok();
}
{
testing("NULL string");
char *const s = NULL;
- trim_newline(s);
+ strtrim(s);
assert(s == NULL);
test_ok();
}
{
testing("string without a newline");
char *const s = "a string without a newline";
- trim_newline(s);
+ strtrim(s);
assert(strcmp(s, "a string without a newline") == 0);
test_ok();
}
- return;
{
testing("string with a newline");
- char *const s = "a string with an ending newline\n";
- trim_newline(s);
+ char *const s = strdup("a string with an ending newline\n");
+ assert(s);
+ strtrim(s);
assert(strcmp(s, "a string with an ending newline") == 0);
+ free(s);
+ test_ok();
+ }
+ {
+ testing("a single newline");
+ char *const s = strdup("\n");
+ assert(s);
+ strtrim(s);
+ assert(strcmp(s, "") == 0);
+ free(s);
+ test_ok();
+ }
+ {
+ testing("multiple newlines at the end");
+ char *const s = strdup("a string\n\n\n\n\n");
+ assert(s);
+ strtrim(s);
+ assert(strcmp(s, "a string") == 0);
+ free(s);
+ test_ok();
+ }
+ {
+ testing("a single space at the end");
+ char *const s = strdup("a string ");
+ assert(s);
+ strtrim(s);
+ assert(strcmp(s, "a string") == 0);
+ free(s);
+ test_ok();
+ }
+ {
+ testing("a single space at the beginning");
+ char *const s = strdup(" a string");
+ assert(s);
+ strtrim(s);
+ assert(strcmp(s, "a string") == 0);
+ free(s);
+ test_ok();
+ }
+ {
+ testing("multiple newlines");
+ char *const s = strdup("\n\na string\n\n");
+ assert(s);
+ strtrim(s);
+ assert(strcmp(s, "a string") == 0);
+ free(s);
+ test_ok();
+ }
+ {
+ testing("newline on the middle of the string");
+ char *const s = "a\nstring";
+ strtrim(s);
+ assert(strcmp(s, "a\nstring") == 0);
+ test_ok();
+ }
+ {
+ testing("multiple trimmable characters");
+ char *const s = strdup(" \t \n \r a string \n \t \r ");
+ assert(s);
+ strtrim(s);
+ assert(strcmp(s, "a string") == 0);
+ free(s);
test_ok();
}
}
@@ -1093,7 +1194,7 @@ static int index_write_row(FILE *const fd, char *const repopath) {
logerrs("fgets(\"", description_path, "\")",
strerror(errno), __LINE__);
} else {
- trim_newline(description);
+ strtrim(description);
}
if (fclose(f)) {
logerrs("fclose(\"", description_path, "\")",
@@ -1790,7 +1891,7 @@ static int repo_write(
logerrs("fget(\"", description_path, "\")",
strerror(errno), __LINE__);
} else {
- trim_newline(description);
+ strtrim(description);
}
if (fclose(f)) {
logerrs("fclose(\"", description_path, "\")",
@@ -1851,7 +1952,8 @@ static void unit_tests(){
test_strjoin();
test_formatted_date();
test_escape_html();
- test_trim_newline();
+ test_should_trim();
+ test_strtrim();
test_last_commit_date();
git_libgit2_shutdown();
}