From 4dc1f6337d44fec6b09c2e4db40def88ab58822a Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Thu, 7 May 2015 11:30:27 +0200 Subject: [PATCH] mx_util: Add mx_str*beginswith*() --- mx_util.c | 63 ++++++++++++++++++++++++++++++++++++++++ mx_util.h | 4 +++ test_mx_util.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 145 insertions(+), 1 deletion(-) diff --git a/mx_util.c b/mx_util.c index 9b96e2c..a159067 100644 --- a/mx_util.c +++ b/mx_util.c @@ -15,6 +15,69 @@ #include "mx_util.h" +static inline int _mx_strbeginswith(char *str, const char *start, char **endptr, short ignore_case) +{ + size_t len; + int res; + + assert(str); + assert(start); + + len = strlen(start); + if (ignore_case) + res = strncasecmp(str, start, len); + else + res = strncmp(str, start, len); + + if (res != 0 || !endptr) + return !res; + + *endptr = str + len; + + return 1; +} + +inline int mx_strbeginswith(char *str, const char *start, char **endptr) +{ + return _mx_strbeginswith(str, start, endptr, 0); +} + +inline int mx_stribeginswith(char *str, const char *start, char **endptr) +{ + return _mx_strbeginswith(str, start, endptr, 1); +} + +static inline int _mx_strbeginswithany(char *str, char **starts, char **endptr, short ignore_case) +{ + char **s; + char *end; + char *longestmatch = NULL; + int res; + + for (s = starts; *s; s++) { + res = _mx_strbeginswith(str, *s, &end, ignore_case); + if (res && (!longestmatch || end > longestmatch)) + longestmatch = end; + } + + if (longestmatch) { + *endptr = longestmatch; + return 1; + } + + return 0; +} + +inline int mx_strbeginswithany(char *str, char **starts, char **endptr) +{ + return _mx_strbeginswithany(str, starts, endptr, 0); +} + +inline int mx_stribeginswithany(char *str, char **starts, char **endptr) +{ + return _mx_strbeginswithany(str, starts, endptr, 1); +} + inline char *mx_strskipwhitespaces(char *str) { char *s; diff --git a/mx_util.h b/mx_util.h index e4ff24b..f00e645 100644 --- a/mx_util.h +++ b/mx_util.h @@ -4,6 +4,10 @@ #include #include +int mx_strbeginswith(char *str, const char *start, char **endptr); +int mx_stribeginswith(char *str, const char *start, char **endptr); +int mx_strbeginswithany(char *str, char **starts, char **endptr); + char *mx_strskipwhitespaces(char *str); int mx_strtoul(char *str, unsigned long int *to); diff --git a/test_mx_util.c b/test_mx_util.c index 5890ed2..5bb570b 100644 --- a/test_mx_util.c +++ b/test_mx_util.c @@ -105,6 +105,81 @@ static void test_mx_strtoi8(void) assert(mx_strtoi8("-129", &u) == -ERANGE); } +static void test_mx_strbeginswith(void) +{ + char *end = NULL; + + assert(mx_strbeginswith("blahblubb", "", NULL)); + + assert(mx_strbeginswith("blahblubb", "", &end)); + assert(strcmp(end, "blahblubb") == 0); + + assert(mx_strbeginswith("BlahBlubb", "Blah", &end)); + assert(strcmp(end, "Blubb") == 0); + + assert(mx_strbeginswith("blahblubb", "blahblubb", &end)); + assert(*end == 0); + + end = NULL; + + assert(mx_strbeginswith("blahblubb", "blubb", &end) == 0); + assert(end == NULL); + + assert(mx_strbeginswith("blah", "blahblubb", &end) == 0); + assert(end == NULL); + + assert(mx_strbeginswith("Blahblubb", "blah", &end) == 0); + assert(end == NULL); + + assert(mx_strbeginswith("", "blah", &end) == 0); + assert(end == NULL); +} + +static void test_mx_stribeginswith(void) +{ + char *end = NULL; + + assert(mx_stribeginswith("blahblubb", "", NULL)); + + assert(mx_stribeginswith("blahblubb", "", &end)); + assert(strcmp(end, "blahblubb") == 0); + + assert(mx_stribeginswith("BlahBlubb", "Blah", &end)); + assert(strcmp(end, "Blubb") == 0); + + assert(mx_stribeginswith("BlahBlubb", "bLaH", &end)); + assert(strcmp(end, "Blubb") == 0); + + + assert(mx_stribeginswith("blahblubb", "BlahBluBB", &end)); + assert(*end == 0); + + end = NULL; + + assert(mx_stribeginswith("blahblubb", "blubb", &end) == 0); + assert(end == NULL); + + assert(mx_stribeginswith("blah", "blahblubb", &end) == 0); + assert(end == NULL); + + assert(mx_stribeginswith("", "blah", &end) == 0); + assert(end == NULL); +} + +static void test_mx_strbeginswithany(void) +{ + char *end = NULL; + + assert(mx_strbeginswithany("blahblubb", (char *[]){ "bla", "blah", NULL }, &end)); + assert(strcmp(end, "blubb") == 0); + + assert(mx_strbeginswithany("blablubb", (char *[]){ "bla", "blah", NULL }, &end)); + assert(strcmp(end, "blubb") == 0); + + end = NULL; + assert(mx_strbeginswithany("blubb", (char *[]){ "bla", "blah", NULL }, &end) == 0); + assert(end == NULL); +} int main(int argc, char *argv[]) { @@ -113,6 +188,8 @@ int main(int argc, char *argv[]) test_mx_strtoull(); test_mx_strtou8(); test_mx_strtoi8(); - + test_mx_strbeginswith(); + test_mx_stribeginswith(); + test_mx_strbeginswithany(); return 0; }