Changeset 0ffbed9 in mainline for uspace/lib/posix/string.c
- Timestamp:
- 2011-06-19T17:49:29Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5974661
- Parents:
- f48b637 (diff), 32fb6944 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/string.c
rf48b637 r0ffbed9 43 43 #include <errno.h> 44 44 45 /* Defined for convenience. Returns pointer to the terminating nul character. 45 /** 46 * Defined for convenience. Returns pointer to the terminating nul character. 47 * 48 * @param s 49 * @return 46 50 */ 47 51 static char *strzero(const char *s) 48 52 { 49 while (*s != '\0') 50 s ++; 51 52 return (char*) s; 53 } 54 55 /* Returns true if s2 is a prefix of s1. 53 while (*s != '\0') { 54 s++; 55 } 56 57 return (char *) s; 58 } 59 60 /** 61 * Returns true if s2 is a prefix of s1. 62 * 63 * @param s1 64 * @param s2 65 * @return 56 66 */ 57 67 static bool begins_with(const char *s1, const char *s2) 58 68 { 59 69 while (*s1 == *s2 && *s2 != '\0') { 60 s1 61 s2 70 s1++; 71 s2++; 62 72 } 63 73 … … 66 76 } 67 77 68 /* The same as strpbrk, except it returns pointer to the nul terminator 78 /** 79 * The same as strpbrk, except it returns pointer to the nul terminator 69 80 * if no occurence is found. 81 * 82 * @param s1 83 * @param s2 84 * @return 70 85 */ 71 86 static char *strpbrk_null(const char *s1, const char *s2) 72 87 { 73 88 while (!posix_strchr(s2, *s1)) { 74 ++ 89 ++s1; 75 90 } 76 91 … … 82 97 * @param dest 83 98 * @param src 84 * @return dest99 * @return 85 100 */ 86 101 char *posix_strcpy(char *dest, const char *src) … … 95 110 * @param src 96 111 * @param n 97 * @return dest112 * @return 98 113 */ 99 114 char *posix_strncpy(char *dest, const char *src, size_t n) … … 114 129 assert(src != NULL); 115 130 116 for (size_t i = 0; ; ++ 131 for (size_t i = 0; ; ++i) { 117 132 dest[i] = src[i]; 118 133 … … 139 154 assert(src != NULL); 140 155 141 for (size_t i = 0; i < n; ++ 156 for (size_t i = 0; i < n; ++i) { 142 157 dest[i] = src[i]; 143 158 … … 147 162 if (src[i] == '\0') { 148 163 char *result = &dest[i]; 149 for (++ i; i < n; ++i) {164 for (++i; i < n; ++i) { 150 165 dest[i] = '\0'; 151 166 } … … 161 176 * @param dest 162 177 * @param src 163 * @return dest178 * @return 164 179 */ 165 180 char *posix_strcat(char *dest, const char *src) … … 177 192 * @param src 178 193 * @param n 179 * @return dest194 * @return 180 195 */ 181 196 char *posix_strncat(char *dest, const char *src, size_t n) … … 198 213 * @return Pointer to the first byte after c in dest if found, NULL otherwise. 199 214 */ 200 void *posix_memccpy(void * restrict dest, const void *restrictsrc, int c, size_t n)215 void *posix_memccpy(void *dest, const void *src, int c, size_t n) 201 216 { 202 217 assert(dest != NULL); … … 206 221 const unsigned char* bsrc = src; 207 222 208 for (size_t i = 0; i < n; ++ 223 for (size_t i = 0; i < n; ++i) { 209 224 bdest[i] = bsrc[i]; 210 225 … … 257 272 * @param n 258 273 * @return Difference of the first pair of inequal bytes, 259 * 274 * or 0 if areas have the same content 260 275 */ 261 276 int posix_memcmp(const void *mem1, const void *mem2, size_t n) … … 267 282 const unsigned char *s2 = mem2; 268 283 269 for (size_t i = 0; i < n; ++ 284 for (size_t i = 0; i < n; ++i) { 270 285 if (s1[i] != s2[i]) { 271 286 return s2[i] - s1[i]; … … 302 317 assert(s2 != NULL); 303 318 304 for (size_t i = 0; i < n; ++ 319 for (size_t i = 0; i < n; ++i) { 305 320 if (s1[i] != s2[i]) { 306 321 return s2[i] - s1[i]; … … 327 342 const unsigned char *s = mem; 328 343 329 for (size_t i = 0; i < n; ++ 344 for (size_t i = 0; i < n; ++i) { 330 345 if (s[i] == (unsigned char) c) { 331 346 return (void *) &s[i]; … … 346 361 347 362 /* special handling for the case that zero is searched for */ 348 if (c == '\0') 363 if (c == '\0') { 349 364 return strzero(s); 365 } 350 366 351 367 /* otherwise just loop through the string until found */ 352 368 while (*s != (char) c) { 353 if (*s == '\0') 369 if (*s == '\0') { 354 370 return NULL; 355 356 s ++; 371 } 372 373 s++; 357 374 } 358 375 … … 374 391 /* the same as in strchr, except it loops in reverse direction */ 375 392 while (*ptr != (char) c) { 376 if (ptr == s) 393 if (ptr == s) { 377 394 return NULL; 378 379 ptr ++; 395 } 396 397 ptr++; 380 398 } 381 399 … … 425 443 426 444 const char *ptr; 427 for (ptr = s1; *ptr != '\0'; ++ 428 if (!posix_strchr(s2, *ptr)) 445 for (ptr = s1; *ptr != '\0'; ++ptr) { 446 if (!posix_strchr(s2, *ptr)) { 429 447 break; 448 } 430 449 } 431 450 return ptr - s1; … … 444 463 445 464 /* special case - needle is an empty string */ 446 if (*s2 == '\0') 465 if (*s2 == '\0') { 447 466 return (char *) s1; 467 } 448 468 449 469 // TODO: use faster algorithm 450 470 /* check for prefix from every position - quadratic complexity */ 451 471 while (*s1 != '\0') { 452 if (begins_with(s1, s2)) 472 if (begins_with(s1, s2)) { 453 473 return (char *) s1; 474 } 454 475 455 s1 476 s1++; 456 477 } 457 478 … … 489 510 size_t len = posix_strlen(s2); 490 511 491 if (n > len) 512 if (n > len) { 492 513 posix_strcpy(s1, s2); 514 } 493 515 494 516 return len; … … 503 525 { 504 526 /* uses function from libc, we just have to negate errno 505 (POSIX uses positive errorcodes, HelenOS has negative) */ 506 return (char *) str_error (-errnum); 527 * (POSIX uses positive errorcodes, HelenOS has negative) 528 */ 529 return (char *) str_error(-errnum); 507 530 } 508 531 … … 510 533 * 511 534 * @param errnum Error code 512 * @param buf 513 * @param bufsz 535 * @param buf Buffer to store a human readable string to 536 * @param bufsz Size of buffer pointed to by buf 514 537 * @return 515 538 */ … … 552 575 assert(s != NULL); 553 576 554 for (size_t sz = 0; sz < n; ++ 577 for (size_t sz = 0; sz < n; ++sz) { 555 578 556 579 if (s[sz] == '\0') {
Note:
See TracChangeset
for help on using the changeset viewer.