aboutsummaryrefslogtreecommitdiff
path: root/src/string_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/string_utils.c')
-rw-r--r--src/string_utils.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/string_utils.c b/src/string_utils.c
index 65af0f2..ee5bb2d 100644
--- a/src/string_utils.c
+++ b/src/string_utils.c
@@ -127,3 +127,31 @@ TEST_CASE(strtod_simple) {
TEST_EQUAL(result, 0.5);
TEST_EQUAL(*end, '\0');
}
+
+const char *trim_both(const char *src, size_t *length) {
+ size_t i = 0;
+ while (isspace(src[i])) {
+ i++;
+ }
+ size_t j = strlen(src) - 1;
+ while (j > i && isspace(src[j])) {
+ j--;
+ }
+ *length = j - i + 1;
+ return src + i;
+}
+
+TEST_CASE(trim_both) {
+ size_t length;
+ const char *str = trim_both(" \t\n\r\f", &length);
+ TEST_EQUAL(length, 0);
+ TEST_EQUAL(*str, '\0');
+
+ str = trim_both(" asdfas ", &length);
+ TEST_EQUAL(length, 6);
+ TEST_STRNEQUAL(str, "asdfas", length);
+
+ str = trim_both(" asdf asdf ", &length);
+ TEST_EQUAL(length, 9);
+ TEST_STRNEQUAL(str, "asdf asdf", length);
+}