Mercurial > hg > nnchat
comparison th_string.c @ 47:e47955d42b55
Synced th-libs.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 29 Oct 2008 02:51:18 +0200 |
parents | 707e35b03f89 |
children | e36df57c5b0f |
comparison
equal
deleted
inserted
replaced
46:65b1ac6a1e2e | 47:e47955d42b55 |
---|---|
27 assert(l > 0); | 27 assert(l > 0); |
28 return th_realloc(s, sizeof(char) * l); | 28 return th_realloc(s, sizeof(char) * l); |
29 } | 29 } |
30 | 30 |
31 | 31 |
32 /* Duplicate a string [strdup] | 32 char *th_strncpy(char * dst, const char * src, size_t n) |
33 */ | 33 { |
34 char *th_strdup(char * str) | 34 const char *s = src; |
35 { | 35 char *d = dst; |
36 char *result, *s, *d; | |
37 | |
38 if (!str) return NULL; | |
39 | |
40 /* Allocate memory for destination */ | |
41 result = th_stralloc(strlen(str) + 1); | |
42 if (!result) | |
43 return NULL; | |
44 | |
45 /* Copy to the destination */ | |
46 s = str; | |
47 d = result; | |
48 while (*s) { | |
49 *(d++) = *(s++); | |
50 } | |
51 *d = 0; | |
52 | |
53 return result; | |
54 } | |
55 | |
56 | |
57 char *th_strncpy(char * dst, char * src, size_t n) | |
58 { | |
59 char *s, *d; | |
60 size_t i; | 36 size_t i; |
61 assert(src); | 37 assert(src != NULL); |
62 assert(dst); | 38 assert(dst != NULL); |
63 | 39 |
64 /* Copy to the destination */ | 40 /* Copy to the destination */ |
65 i = n; | 41 i = n; |
66 s = src; | |
67 d = dst; | |
68 while (*s && (i > 0)) { | 42 while (*s && (i > 0)) { |
69 *(d++) = *(s++); | 43 *(d++) = *(s++); |
70 i--; | 44 i--; |
71 } | 45 } |
72 | 46 |
84 | 58 |
85 | 59 |
86 int th_strncmp(char * str1, char * str2, size_t n) | 60 int th_strncmp(char * str1, char * str2, size_t n) |
87 { | 61 { |
88 char *s1, *s2; | 62 char *s1, *s2; |
89 assert(str1); | 63 assert(str1 != NULL); |
90 assert(str2); | 64 assert(str2 != NULL); |
91 | 65 |
92 /* Check the string pointers */ | 66 /* Check the string pointers */ |
93 if (str1 == str2) | 67 if (str1 == str2) |
94 return 0; | 68 return 0; |
95 | 69 |
112 /* Compare two strings ignoring case [strcasecmp, strncasecmp] | 86 /* Compare two strings ignoring case [strcasecmp, strncasecmp] |
113 */ | 87 */ |
114 int th_strcasecmp(char * str1, char * str2) | 88 int th_strcasecmp(char * str1, char * str2) |
115 { | 89 { |
116 char *s1 = str1, *s2 = str2; | 90 char *s1 = str1, *s2 = str2; |
117 assert(str1); | 91 assert(str1 != NULL); |
118 assert(str2); | 92 assert(str2 != NULL); |
119 | 93 |
120 /* Check the string pointers */ | 94 /* Check the string pointers */ |
121 if (str1 == str2) | 95 if (str1 == str2) |
122 return 0; | 96 return 0; |
123 | 97 |
132 | 106 |
133 | 107 |
134 int th_strncasecmp(char * str1, char * str2, size_t n) | 108 int th_strncasecmp(char * str1, char * str2, size_t n) |
135 { | 109 { |
136 char *s1 = str1, *s2 = str2; | 110 char *s1 = str1, *s2 = str2; |
137 assert(str1); | 111 assert(str1 != NULL); |
138 assert(str2); | 112 assert(str2 != NULL); |
139 | 113 |
140 /* Check the string pointers */ | 114 /* Check the string pointers */ |
141 if (str1 == str2) | 115 if (str1 == str2) |
142 return 0; | 116 return 0; |
143 | 117 |
159 * Resulting string is always shorter or same length than original. | 133 * Resulting string is always shorter or same length than original. |
160 */ | 134 */ |
161 void th_strip_ctrlchars(char * str) | 135 void th_strip_ctrlchars(char * str) |
162 { | 136 { |
163 char *i, *j; | 137 char *i, *j; |
164 assert(str); | 138 assert(str != NULL); |
165 | 139 |
166 i = str; | 140 i = str; |
167 j = str; | 141 j = str; |
168 while (*i) { | 142 while (*i) { |
169 if (!th_iscntrl(*i)) | 143 if (!th_iscntrl(*i)) |
177 | 151 |
178 /* Copy a given string over in *result. | 152 /* Copy a given string over in *result. |
179 */ | 153 */ |
180 int th_pstrcpy(char ** result, char * str) | 154 int th_pstrcpy(char ** result, char * str) |
181 { | 155 { |
182 assert(result); | 156 assert(result != NULL); |
183 | 157 |
184 /* Check the string pointers */ | 158 /* Check the string pointers */ |
185 if (!str) | 159 if (str == NULL) |
186 return -1; | 160 return -1; |
187 | 161 |
188 /* Allocate memory for destination */ | 162 /* Allocate memory for destination */ |
189 th_free(*result); | 163 th_free(*result); |
190 *result = th_stralloc(strlen(str) + 1); | 164 *result = th_stralloc(strlen(str) + 1); |
200 | 174 |
201 /* Concatenates a given string into string pointed by *result. | 175 /* Concatenates a given string into string pointed by *result. |
202 */ | 176 */ |
203 int th_pstrcat(char ** result, char * str) | 177 int th_pstrcat(char ** result, char * str) |
204 { | 178 { |
205 assert(result); | 179 assert(result != NULL); |
206 | 180 |
207 /* Check the string pointers */ | 181 /* Check the string pointers */ |
208 if (!str) | 182 if (str == NULL) |
209 return -1; | 183 return -1; |
210 | 184 |
211 if (*result != NULL) { | 185 if (*result != NULL) { |
212 *result = th_strrealloc(*result, strlen(*result) + strlen(str) + 1); | 186 *result = th_strrealloc(*result, strlen(*result) + strlen(str) + 1); |
213 if (*result == NULL) | 187 if (*result == NULL) |
230 * Updates iPos into the position of such character and | 204 * Updates iPos into the position of such character and |
231 * returns pointer to the string. | 205 * returns pointer to the string. |
232 */ | 206 */ |
233 char *th_findnext(char * str, size_t * iPos) | 207 char *th_findnext(char * str, size_t * iPos) |
234 { | 208 { |
235 assert(str); | 209 assert(str != NULL); |
236 | 210 |
237 /* Terminating NULL-character is not whitespace! */ | 211 /* Terminating NULL-character is not whitespace! */ |
238 while (th_isspace(str[*iPos])) | 212 while (th_isspace(str[*iPos])) |
239 (*iPos)++; | 213 (*iPos)++; |
240 return &str[*iPos]; | 214 return &str[*iPos]; |
243 | 217 |
244 /* Find next chSep-character from string | 218 /* Find next chSep-character from string |
245 */ | 219 */ |
246 char *th_findsep(char * str, size_t * iPos, char chSep) | 220 char *th_findsep(char * str, size_t * iPos, char chSep) |
247 { | 221 { |
248 assert(str); | 222 assert(str != NULL); |
249 | 223 |
250 /* Terminating NULL-character is not digit! */ | 224 /* Terminating NULL-character is not digit! */ |
251 while (str[*iPos] && (str[*iPos] != chSep)) | 225 while (str[*iPos] && (str[*iPos] != chSep)) |
252 (*iPos)++; | 226 (*iPos)++; |
253 return &str[*iPos]; | 227 return &str[*iPos]; |
256 | 230 |
257 /* Find next chSep- or whitespace from string | 231 /* Find next chSep- or whitespace from string |
258 */ | 232 */ |
259 char *th_findseporspace(char * str, size_t * iPos, char chSep) | 233 char *th_findseporspace(char * str, size_t * iPos, char chSep) |
260 { | 234 { |
261 assert(str); | 235 assert(str != NULL); |
262 | 236 |
263 /* Terminating NULL-character is not digit! */ | 237 /* Terminating NULL-character is not digit! */ |
264 while (!th_isspace(str[*iPos]) && (str[*iPos] != chSep)) | 238 while (!th_isspace(str[*iPos]) && (str[*iPos] != chSep)) |
265 (*iPos)++; | 239 (*iPos)++; |
266 return &str[*iPos]; | 240 return &str[*iPos]; |
272 * wildcards ? and *. "?" matches any character and "*" matches | 246 * wildcards ? and *. "?" matches any character and "*" matches |
273 * any number of characters. | 247 * any number of characters. |
274 */ | 248 */ |
275 BOOL th_strmatch(char * str, char * pattern) | 249 BOOL th_strmatch(char * str, char * pattern) |
276 { | 250 { |
277 BOOL didMatch, isAnyMode, isEnd; | 251 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; |
278 char *tmpPattern; | 252 char *tmpPattern = NULL; |
279 | 253 |
280 /* Check given pattern and string */ | 254 /* Check given pattern and string */ |
281 if (!str) | 255 if (str == NULL || pattern == NULL) |
282 return FALSE; | 256 return FALSE; |
283 if (!pattern) | |
284 return FALSE; | |
285 | |
286 /* Initialize */ | |
287 tmpPattern = NULL; | |
288 didMatch = TRUE; | |
289 isEnd = FALSE; | |
290 isAnyMode = FALSE; | |
291 | 257 |
292 /* Start comparision */ | 258 /* Start comparision */ |
293 do { | 259 do { |
294 didMatch = FALSE; | 260 didMatch = FALSE; |
295 switch (*pattern) { | 261 switch (*pattern) { |
328 isEnd = TRUE; | 294 isEnd = TRUE; |
329 } | 295 } |
330 break; | 296 break; |
331 default: | 297 default: |
332 if (isAnyMode) { | 298 if (isAnyMode) { |
333 if ((*pattern) == (*str)) { | 299 if (*pattern == *str) { |
334 isAnyMode = FALSE; | 300 isAnyMode = FALSE; |
335 didMatch = TRUE; | 301 didMatch = TRUE; |
336 } else { | 302 } else { |
337 if (*str) { | 303 if (*str) { |
338 didMatch = TRUE; | 304 didMatch = TRUE; |
339 str++; | 305 str++; |
340 } | 306 } |
341 } | 307 } |
342 } else { | 308 } else { |
343 if ((*pattern) == (*str)) { | 309 if (*pattern == *str) { |
344 didMatch = TRUE; | 310 didMatch = TRUE; |
345 if (*pattern) | 311 if (*pattern) |
346 pattern++; | 312 pattern++; |
347 if (*str) | 313 if (*str) |
348 str++; | 314 str++; |
359 isEnd = TRUE; | 325 isEnd = TRUE; |
360 break; | 326 break; |
361 | 327 |
362 } /* switch */ | 328 } /* switch */ |
363 | 329 |
364 } while ((didMatch) && (!isEnd)); | 330 } while (didMatch && !isEnd); |
365 | 331 |
366 return didMatch; | 332 return didMatch; |
367 } | 333 } |
368 | 334 |
369 | 335 |
370 /* Compare a string to a pattern. Case-INSENSITIVE version. | 336 /* Compare a string to a pattern. Case-INSENSITIVE version. |
371 */ | 337 */ |
372 BOOL th_strcasematch(char * str, char * pattern) | 338 BOOL th_strcasematch(char * str, char * pattern) |
373 { | 339 { |
374 BOOL didMatch, isAnyMode, isEnd; | 340 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; |
375 char *tmpPattern; | 341 char *tmpPattern = NULL; |
376 | 342 |
377 /* Check given pattern and string */ | 343 /* Check given pattern and string */ |
378 if (!str) | 344 if (str == NULL || pattern == NULL) |
379 return FALSE; | 345 return FALSE; |
380 if (!pattern) | |
381 return FALSE; | |
382 | |
383 /* Initialize */ | |
384 tmpPattern = NULL; | |
385 didMatch = TRUE; | |
386 isEnd = FALSE; | |
387 isAnyMode = FALSE; | |
388 | 346 |
389 /* Start comparision */ | 347 /* Start comparision */ |
390 do { | 348 do { |
391 switch (*pattern) { | 349 switch (*pattern) { |
392 case '?': | 350 case '?': |
398 didMatch = FALSE; | 356 didMatch = FALSE; |
399 break; | 357 break; |
400 | 358 |
401 case '*': | 359 case '*': |
402 pattern++; | 360 pattern++; |
403 if (!*pattern || (*pattern == '?')) | 361 if (!*pattern || *pattern == '?') |
404 isEnd = TRUE; | 362 isEnd = TRUE; |
405 isAnyMode = TRUE; | 363 isAnyMode = TRUE; |
406 tmpPattern = pattern; | 364 tmpPattern = pattern; |
407 break; | 365 break; |
408 | 366 |
453 isEnd = TRUE; | 411 isEnd = TRUE; |
454 break; | 412 break; |
455 | 413 |
456 } /* switch */ | 414 } /* switch */ |
457 | 415 |
458 } while ((didMatch) && (!isEnd)); | 416 } while (didMatch && !isEnd); |
459 | 417 |
460 return didMatch; | 418 return didMatch; |
461 } | 419 } |
462 | 420 |