comparison src/dmstring.c @ 2494:fcaf2db0cd05

Remove dm_str(n)casecmp() functions, in favour of standard libc ones.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 28 Apr 2020 20:34:49 +0300
parents 18ec4c092108
children
comparison
equal deleted inserted replaced
2493:ec036e88a0c2 2494:fcaf2db0cd05
1 #include "dmlib.h" 1 #include "dmlib.h"
2 #include <stdarg.h> 2 #include <stdarg.h>
3
4
5 /* Compare two strings ignoring case [strcasecmp, strncasecmp]
6 */
7 int dm_strcasecmp(const char *haystack, const char *needle)
8 {
9 const char *s1 = haystack, *s2 = needle;
10 assert(haystack != NULL);
11 assert(needle != NULL);
12
13 if (haystack == needle)
14 return 0;
15
16 while (*s1 && *s2)
17 {
18 int k = tolower(*s1) - tolower(*s2);
19 if (k != 0)
20 return k;
21 s1++;
22 s2++;
23 }
24
25 return tolower(*s1) - tolower(*s2);
26 }
27
28
29 int dm_strncasecmp(const char *haystack, const char *needle, size_t n)
30 {
31 const char *s1 = haystack, *s2 = needle;
32 assert(haystack != NULL);
33 assert(needle != NULL);
34
35 if (haystack == needle)
36 return 0;
37
38 while (n > 0 && *s1 && *s2)
39 {
40 int k = tolower(*s1) - tolower(*s2);
41 if (k != 0)
42 return k;
43 s1++;
44 s2++;
45 n--;
46 }
47
48 return (n == 0) ? 0 : tolower(*s1) - tolower(*s2);
49 }
50 3
51 4
52 /* Check if end of the given string str matches needle 5 /* Check if end of the given string str matches needle
53 * case-insensitively, return pointer to start of the match, 6 * case-insensitively, return pointer to start of the match,
54 * if found, NULL otherwise. 7 * if found, NULL otherwise.
64 nlen = strlen(needle); 17 nlen = strlen(needle);
65 18
66 if (slen < nlen) 19 if (slen < nlen)
67 return NULL; 20 return NULL;
68 21
69 if (dm_strcasecmp(str + slen - nlen, needle) == 0) 22 if (strcasecmp(str + slen - nlen, needle) == 0)
70 return str + slen - nlen; 23 return str + slen - nlen;
71 else 24 else
72 return NULL; 25 return NULL;
73 } 26 }
74 } 27 }