diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-08-15 15:42:58 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-08-15 15:42:58 -0400 |
commit | f4478fca7deea454ae169e2d65db2114475a279d (patch) | |
tree | b1acc198d4eb372ee1484f5f28be473d1decdcbe | |
parent | f0a624cdb494275d3d0e5049eff07ceaa7ebe848 (diff) |
implement
-rw-r--r-- | src/strings/strcasecmp.c | 17 | ||||
-rw-r--r-- | src/strings/strncasecmp.c | 22 |
2 files changed, 37 insertions, 2 deletions
diff --git a/src/strings/strcasecmp.c b/src/strings/strcasecmp.c index e9006fe2..a5955f4e 100644 --- a/src/strings/strcasecmp.c +++ b/src/strings/strcasecmp.c @@ -1,8 +1,23 @@ #include <strings.h> +#include <ctype.h> int strcasecmp(const char *s1, const char *s2) { - return strcasecmp_l (s1, s2, (locale_t)0); + while (*s1 && *s2) { + char c1 = tolower(*s1); + char c2 = tolower(*s2); + if (c1 != c2) { + return c1 - c2; + } + } + + if (*s1) { + return -1; + } else if (*s2) { + return 1; + } + + return 0; } /* diff --git a/src/strings/strncasecmp.c b/src/strings/strncasecmp.c index 8094ccd2..fc02e734 100644 --- a/src/strings/strncasecmp.c +++ b/src/strings/strncasecmp.c @@ -1,8 +1,28 @@ #include <strings.h> +#include <ctype.h> int strncasecmp(const char *s1, const char *s2, size_t n) { - return strncasecmp_l (s1, s2, n, (locale_t)0); + while (n > 0 && *s1 && *s2) { + char c1 = tolower(*s1); + char c2 = tolower(*s2); + if (c1 != c2) { + return c1 - c2; + } + n--; + } + + if (n == 0) { + return 0; + } + + if (*s1) { + return -1; + } else if (*s2) { + return 1; + } + + return 0; } /* |