Mercurial > hg > th-libs
comparison th_string.c @ 43:8c015ac0c556
Cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 03 Oct 2011 00:21:32 +0300 |
parents | 0d55592e0e01 |
children | 669d2bc97c57 |
comparison
equal
deleted
inserted
replaced
42:7851f704d499 | 43:8c015ac0c556 |
---|---|
24 strcpy(res, s); | 24 strcpy(res, s); |
25 return res; | 25 return res; |
26 } | 26 } |
27 | 27 |
28 | 28 |
29 char *th_strncpy(char * dst, const char * src, const size_t n) | 29 char *th_strncpy(char *dst, const char *src, const size_t n) |
30 { | 30 { |
31 const char *s = src; | 31 const char *s = src; |
32 char *d = dst; | 32 char *d = dst; |
33 size_t i; | 33 size_t i; |
34 assert(src != NULL); | 34 assert(src != NULL); |
56 } | 56 } |
57 | 57 |
58 | 58 |
59 /* Simulate a sprintf() that allocates memory | 59 /* Simulate a sprintf() that allocates memory |
60 */ | 60 */ |
61 char * th_strdup_vprintf(const char *fmt, va_list args) | 61 char *th_strdup_vprintf(const char *fmt, va_list args) |
62 { | 62 { |
63 int size = 64; | 63 int size = 64; |
64 char *buf, *nbuf = NULL; | 64 char *buf, *nbuf = NULL; |
65 | 65 |
66 if ((buf = th_malloc(size)) == NULL) | 66 if ((buf = th_malloc(size)) == NULL) |
90 buf = nbuf; | 90 buf = nbuf; |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 | 94 |
95 char * th_strdup_printf(const char *fmt, ...) | 95 char *th_strdup_printf(const char *fmt, ...) |
96 { | 96 { |
97 char *res; | 97 char *res; |
98 va_list ap; | 98 va_list ap; |
99 | 99 |
100 va_start(ap, fmt); | 100 va_start(ap, fmt); |
126 *buf = tmp; | 126 *buf = tmp; |
127 } | 127 } |
128 | 128 |
129 /* Compare two strings ignoring case [strcasecmp, strncasecmp] | 129 /* Compare two strings ignoring case [strcasecmp, strncasecmp] |
130 */ | 130 */ |
131 int th_strcasecmp(const char * str1, const char * str2) | 131 int th_strcasecmp(const char *str1, const char *str2) |
132 { | 132 { |
133 const char *s1 = str1, *s2 = str2; | 133 const char *s1 = str1, *s2 = str2; |
134 assert(str1 != NULL); | 134 assert(str1 != NULL); |
135 assert(str2 != NULL); | 135 assert(str2 != NULL); |
136 | 136 |
145 | 145 |
146 return (th_tolower(*s1) - th_tolower(*s2)); | 146 return (th_tolower(*s1) - th_tolower(*s2)); |
147 } | 147 } |
148 | 148 |
149 | 149 |
150 int th_strncasecmp(const char * str1, const char * str2, size_t n) | 150 int th_strncasecmp(const char *str1, const char *str2, size_t n) |
151 { | 151 { |
152 const char *s1 = str1, *s2 = str2; | 152 const char *s1 = str1, *s2 = str2; |
153 assert(str1 != NULL); | 153 assert(str1 != NULL); |
154 assert(str2 != NULL); | 154 assert(str2 != NULL); |
155 | 155 |
168 | 168 |
169 | 169 |
170 /* Remove all occurences of control characters, in-place. | 170 /* Remove all occurences of control characters, in-place. |
171 * Resulting string is always shorter or same length than original. | 171 * Resulting string is always shorter or same length than original. |
172 */ | 172 */ |
173 void th_strip_ctrlchars(char * str) | 173 void th_strip_ctrlchars(char *str) |
174 { | 174 { |
175 char *i, *j; | 175 char *i, *j; |
176 assert(str != NULL); | 176 assert(str != NULL); |
177 | 177 |
178 i = str; | 178 i = str; |
188 } | 188 } |
189 | 189 |
190 | 190 |
191 /* Copy a given string over in *result. | 191 /* Copy a given string over in *result. |
192 */ | 192 */ |
193 int th_pstrcpy(char ** result, const char * str) | 193 int th_pstrcpy(char **result, const char *str) |
194 { | 194 { |
195 assert(result != NULL); | 195 assert(result != NULL); |
196 | 196 |
197 if (str == NULL) | 197 if (str == NULL) |
198 return -1; | 198 return -1; |
206 } | 206 } |
207 | 207 |
208 | 208 |
209 /* Concatenates a given string into string pointed by *result. | 209 /* Concatenates a given string into string pointed by *result. |
210 */ | 210 */ |
211 int th_pstrcat(char ** result, const char * str) | 211 int th_pstrcat(char **result, const char *str) |
212 { | 212 { |
213 assert(result != NULL); | 213 assert(result != NULL); |
214 | 214 |
215 if (str == NULL) | 215 if (str == NULL) |
216 return -1; | 216 return -1; |
238 | 238 |
239 /* Find next non-whitespace character in string. | 239 /* Find next non-whitespace character in string. |
240 * Updates iPos into the position of such character and | 240 * Updates iPos into the position of such character and |
241 * returns pointer to the string. | 241 * returns pointer to the string. |
242 */ | 242 */ |
243 const char *th_findnext(const char * str, size_t * pos) | 243 const char *th_findnext(const char *str, size_t *pos) |
244 { | 244 { |
245 assert(str != NULL); | 245 assert(str != NULL); |
246 | 246 |
247 /* Terminating NULL-character is not whitespace! */ | 247 /* Terminating NULL-character is not whitespace! */ |
248 while (th_isspace(str[*pos])) | 248 while (th_isspace(str[*pos])) |
252 } | 252 } |
253 | 253 |
254 | 254 |
255 /* Find next sep-character from string | 255 /* Find next sep-character from string |
256 */ | 256 */ |
257 const char *th_findsep(const char * str, size_t * pos, char sep) | 257 const char *th_findsep(const char *str, size_t *pos, char sep) |
258 { | 258 { |
259 assert(str != NULL); | 259 assert(str != NULL); |
260 | 260 |
261 while (str[*pos] && str[*pos] != sep) | 261 while (str[*pos] && str[*pos] != sep) |
262 (*pos)++; | 262 (*pos)++; |
265 } | 265 } |
266 | 266 |
267 | 267 |
268 /* Find next sep- or whitespace from string | 268 /* Find next sep- or whitespace from string |
269 */ | 269 */ |
270 const char *th_findseporspace(const char * str, size_t * pos, char sep) | 270 const char *th_findseporspace(const char *str, size_t *pos, char sep) |
271 { | 271 { |
272 assert(str != NULL); | 272 assert(str != NULL); |
273 | 273 |
274 while (!th_isspace(str[*pos]) && str[*pos] != sep) | 274 while (!th_isspace(str[*pos]) && str[*pos] != sep) |
275 (*pos)++; | 275 (*pos)++; |
281 /* Compare a string to a pattern. Case-SENSITIVE version. | 281 /* Compare a string to a pattern. Case-SENSITIVE version. |
282 * The matching pattern can consist of any normal characters plus | 282 * The matching pattern can consist of any normal characters plus |
283 * wildcards ? and *. "?" matches any character and "*" matches | 283 * wildcards ? and *. "?" matches any character and "*" matches |
284 * any number of characters. | 284 * any number of characters. |
285 */ | 285 */ |
286 BOOL th_strmatch(const char * str, const char * pattern) | 286 BOOL th_strmatch(const char *str, const char *pattern) |
287 { | 287 { |
288 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; | 288 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; |
289 const char *tmpPattern = NULL; | 289 const char *tmpPattern = NULL; |
290 | 290 |
291 /* Check given pattern and string */ | 291 /* Check given pattern and string */ |
389 } | 389 } |
390 | 390 |
391 | 391 |
392 /* Compare a string to a pattern. Case-INSENSITIVE version. | 392 /* Compare a string to a pattern. Case-INSENSITIVE version. |
393 */ | 393 */ |
394 BOOL th_strcasematch(const char * str, const char * pattern) | 394 BOOL th_strcasematch(const char *str, const char *pattern) |
395 { | 395 { |
396 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; | 396 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; |
397 const char *tmpPattern = NULL; | 397 const char *tmpPattern = NULL; |
398 | 398 |
399 /* Check given pattern and string */ | 399 /* Check given pattern and string */ |