Skip to content

Commit

Permalink
mx_util: Add mx_str*beginswith*()
Browse files Browse the repository at this point in the history
  • Loading branch information
mariux committed May 7, 2015
1 parent 09c60f3 commit 4dc1f63
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
63 changes: 63 additions & 0 deletions mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions mx_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include <stdint.h>
#include <stdarg.h>

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);
Expand Down
79 changes: 78 additions & 1 deletion test_mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
{
Expand All @@ -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;
}

0 comments on commit 4dc1f63

Please sign in to comment.