From 522d85ae0a3dd80ec58033289c4f63ba7dfc63a8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 13 Jan 2016 02:21:16 +0100 Subject: basic: add new ascii_strcasecmp_n() call --- src/basic/string-util.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/basic/string-util.c') diff --git a/src/basic/string-util.c b/src/basic/string-util.c index 849e457439..b7b8c97bc0 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -348,6 +348,21 @@ char *ascii_strlower_n(char *t, size_t n) { return t; } +int ascii_strcasecmp_n(const char *a, const char *b, size_t n) { + + for (; n > 0; a++, b++, n--) { + int x, y; + + x = (int) (uint8_t) ascii_tolower(*a); + y = (int) (uint8_t) ascii_tolower(*b); + + if (x != y) + return x - y; + } + + return 0; +} + bool chars_intersect(const char *a, const char *b) { const char *p; -- cgit v1.2.3-54-g00ecf From c174983474d4a010a18e3bb9a59e351a442480f5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 13 Jan 2016 19:45:05 +0100 Subject: basic: add ascii_strcasecmp_nn() call In contrast to ascii_strcasecmp_nn() it takes two character buffers with their individual length. It will then compare the buffers up the smaller size of the two buffers, and finally the length themselves. --- src/basic/string-util.c | 15 +++++++++++++++ src/basic/string-util.h | 1 + src/test/test-string-util.c | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) (limited to 'src/basic/string-util.c') diff --git a/src/basic/string-util.c b/src/basic/string-util.c index b7b8c97bc0..1f95a9abba 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -363,6 +363,21 @@ int ascii_strcasecmp_n(const char *a, const char *b, size_t n) { return 0; } +int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) { + int r; + + r = ascii_strcasecmp_n(a, b, MIN(n, m)); + if (r != 0) + return r; + + if (n < m) + return -1; + else if (n > m) + return 1; + else + return 0; +} + bool chars_intersect(const char *a, const char *b) { const char *p; diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 6374e11119..8ea18f45aa 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -135,6 +135,7 @@ char *ascii_strlower(char *s); char *ascii_strlower_n(char *s, size_t n); int ascii_strcasecmp_n(const char *a, const char *b, size_t n); +int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m); bool chars_intersect(const char *a, const char *b) _pure_; diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c index 4758108706..12889ce873 100644 --- a/src/test/test-string-util.c +++ b/src/test/test-string-util.c @@ -82,8 +82,26 @@ static void test_ascii_strcasecmp_n(void) { assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0); } +static void test_ascii_strcasecmp_nn(void) { + assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0); + assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0); + assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0); + assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0); + + assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0); + assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0); + assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0); + assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0); + assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0); + + assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0); + assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0); + assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0); +} + int main(int argc, char *argv[]) { test_string_erase(); test_ascii_strcasecmp_n(); + test_ascii_strcasecmp_nn(); return 0; } -- cgit v1.2.3-54-g00ecf