Mercurial > hg > dmlib
annotate 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 |
rev | line source |
---|---|
0 | 1 #include "dmlib.h" |
2 #include <stdarg.h> | |
3 | |
813 | 4 |
817 | 5 /* Check if end of the given string str matches needle |
6 * case-insensitively, return pointer to start of the match, | |
7 * if found, NULL otherwise. | |
8 */ | |
816 | 9 char *dm_strrcasecmp(char *str, const char *needle) |
10 { | |
11 if (str == NULL || needle == NULL) | |
12 return NULL; | |
2465
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
13 else |
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
14 { |
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
15 const size_t |
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
16 slen = strlen(str), |
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
17 nlen = strlen(needle); |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
836
diff
changeset
|
18 |
2465
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
19 if (slen < nlen) |
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
20 return NULL; |
816 | 21 |
2494
fcaf2db0cd05
Remove dm_str(n)casecmp() functions, in favour of standard libc ones.
Matti Hamalainen <ccr@tnsp.org>
parents:
2490
diff
changeset
|
22 if (strcasecmp(str + slen - nlen, needle) == 0) |
2465
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
23 return str + slen - nlen; |
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
24 else |
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
25 return NULL; |
63dd0a611586
Sync string handling fixes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2079
diff
changeset
|
26 } |
816 | 27 } |
28 | |
29 | |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
30 /* Implementation of strdup() with a NULL check |
0 | 31 */ |
32 char *dm_strdup(const char *s) | |
33 { | |
34 char *res; | |
35 if (s == NULL) | |
36 return NULL; | |
37 | |
38 if ((res = dmMalloc(strlen(s) + 1)) == NULL) | |
39 return NULL; | |
40 | |
41 strcpy(res, s); | |
42 return res; | |
43 } | |
44 | |
45 | |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
46 /* Implementation of strndup() with NULL check |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
47 */ |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
48 char *dm_strndup(const char *str, const size_t n) |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
49 { |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
50 char *res; |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
51 if (str == NULL) |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
52 return NULL; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
53 |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
54 size_t len = strlen(str); |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
55 if (len > n) |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
56 len = n; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
57 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
58 if ((res = dmMalloc(len + 1)) == NULL) |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
59 return NULL; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
60 |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
61 memcpy(res, str, len); |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
62 res[len] = 0; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
63 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
64 return res; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
65 } |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
66 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
67 |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
68 /* Like strdup, but trims whitespace from the string according to specified flags. |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
69 * See DM_TRIM_* in dmlib.h. If the resulting string would be empty (length 0), |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
70 * NULL is returned. |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
71 */ |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
72 static char * dm_strdup_trim_do(const char *src, size_t len, const int flags) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
73 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
74 char *res; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
75 size_t start, end; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
76 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
77 if (len == 0) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
78 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
79 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
80 // Trim start: find first non-whitespace character |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
81 if (flags & DM_TRIM_START) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
82 for (start = 0; start < len && isspace(src[start]); start++); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
83 else |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
84 start = 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
85 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
86 // Trim end: find last non-whitespace character |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
87 if (flags & DM_TRIM_END) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
88 for (end = len - 1; end > start && isspace(src[end]); end--); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
89 else |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
90 end = len; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
91 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
92 // Allocate memory for result |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
93 if (src[end] == 0 || isspace(src[end])) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
94 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
95 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
96 len = end - start + 1; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
97 if ((res = dmMalloc(len + 1)) == NULL) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
98 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
99 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
100 memcpy(res, src + start, len); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
101 res[len] = 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
102 return res; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
103 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
104 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
105 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
106 char *dm_strdup_trim(const char *src, const int flags) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
107 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
108 if (src == NULL) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
109 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
110 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
111 return dm_strdup_trim_do(src, strlen(src), flags); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
112 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
113 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
114 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
115 char *dm_strndup_trim(const char *src, const size_t n, const int flags) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
116 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
117 size_t len; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
118 if (src == NULL || n == 0) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
119 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
120 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
121 for (len = 0; len < n && src[len]; len++); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
122 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
123 return dm_strdup_trim_do(src, len, flags); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
124 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
125 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
126 |
0 | 127 /* Simulate a sprintf() that allocates memory |
128 */ | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
129 char *dm_strdup_vprintf_len(const char *fmt, va_list args, int *len) |
0 | 130 { |
131 int size = 64; | |
836 | 132 char *buf, *tmp; |
0 | 133 |
134 if ((buf = dmMalloc(size)) == NULL) | |
135 return NULL; | |
136 | |
137 while (1) | |
138 { | |
139 va_list ap; | |
140 va_copy(ap, args); | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
141 *len = vsnprintf(buf, size, fmt, ap); |
0 | 142 va_end(ap); |
143 | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
144 if (*len > -1 && *len < size) |
0 | 145 return buf; |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
146 if (*len > -1) |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
147 size = *len + 1; |
0 | 148 else |
149 size *= 2; | |
150 | |
836 | 151 if ((tmp = dmRealloc(buf, size)) == NULL) |
152 { | |
153 dmFree(buf); | |
0 | 154 return NULL; |
836 | 155 } |
156 else | |
157 buf = tmp; | |
0 | 158 } |
159 } | |
160 | |
161 | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
162 char *dm_strdup_vprintf(const char *fmt, va_list args) |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
163 { |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
164 int len; |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
165 return dm_strdup_vprintf_len(fmt, args, &len); |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
166 } |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
167 |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
168 |
0 | 169 char *dm_strdup_printf(const char *fmt, ...) |
170 { | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
171 int len; |
0 | 172 char *res; |
173 va_list ap; | |
174 | |
175 va_start(ap, fmt); | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
176 res = dm_strdup_vprintf_len(fmt, ap, &len); |
0 | 177 va_end(ap); |
178 | |
179 return res; | |
180 } |