mirror of https://github.com/yshui/picom.git
string_utils: add trim_both
trim_both removes whitespaces from both side of a string. Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
parent
e757a6461d
commit
7e833744b7
|
@ -127,3 +127,31 @@ TEST_CASE(strtod_simple) {
|
||||||
TEST_EQUAL(result, 0.5);
|
TEST_EQUAL(result, 0.5);
|
||||||
TEST_EQUAL(*end, '\0');
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
char *mstrjoin(const char *src1, const char *src2);
|
char *mstrjoin(const char *src1, const char *src2);
|
||||||
char *mstrjoin3(const char *src1, const char *src2, const char *src3);
|
char *mstrjoin3(const char *src1, const char *src2, const char *src3);
|
||||||
void mstrextend(char **psrc1, const char *src2);
|
void mstrextend(char **psrc1, const char *src2);
|
||||||
|
const char *trim_both(const char *src, size_t *length);
|
||||||
|
|
||||||
/// Parse a floating point number of form (+|-)?[0-9]*(\.[0-9]*)
|
/// Parse a floating point number of form (+|-)?[0-9]*(\.[0-9]*)
|
||||||
double strtod_simple(const char *, const char **);
|
double strtod_simple(const char *, const char **);
|
||||||
|
|
|
@ -84,7 +84,7 @@ struct test_file_metadata __attribute__((weak)) * test_file_head;
|
||||||
const char *part2 = " != " #b; \
|
const char *part2 = " != " #b; \
|
||||||
size_t len2 = len + strlen(part2) + 3; \
|
size_t len2 = len + strlen(part2) + 3; \
|
||||||
char *buf = malloc(len2); \
|
char *buf = malloc(len2); \
|
||||||
snprintf(buf, len2, "\"%.*s\"%s", len, a, part2); \
|
snprintf(buf, len2, "\"%.*s\"%s", (int)len, a, part2); \
|
||||||
SET_FAILURE(buf, true); \
|
SET_FAILURE(buf, true); \
|
||||||
return; \
|
return; \
|
||||||
} \
|
} \
|
||||||
|
|
Loading…
Reference in New Issue