Changeset a35b458 in mainline for uspace/lib/posix/src/string.c
- Timestamp:
- 2018-03-02T20:10:49Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/src/string.c
r3061bc1 ra35b458 62 62 ++s1; 63 63 } 64 64 65 65 return (char *) s1; 66 66 } … … 107 107 for (size_t i = 0; ; ++i) { 108 108 dest[i] = src[i]; 109 109 110 110 if (src[i] == '\0') { 111 111 /* pointer to the terminating nul character */ … … 113 113 } 114 114 } 115 115 116 116 /* unreachable */ 117 117 return NULL; … … 133 133 for (size_t i = 0; i < n; ++i) { 134 134 dest[i] = src[i]; 135 135 136 136 /* the standard requires that nul characters 137 137 * are appended to the length of n, in case src is shorter … … 145 145 } 146 146 } 147 147 148 148 return &dest[n]; 149 149 } … … 197 197 assert(dest != NULL); 198 198 assert(src != NULL); 199 199 200 200 unsigned char* bdest = dest; 201 201 const unsigned char* bsrc = src; 202 202 203 203 for (size_t i = 0; i < n; ++i) { 204 204 bdest[i] = bsrc[i]; 205 205 206 206 if (bsrc[i] == (unsigned char) c) { 207 207 /* pointer to the next byte */ … … 209 209 } 210 210 } 211 211 212 212 return NULL; 213 213 } … … 301 301 { 302 302 assert(mem != NULL); 303 303 304 304 const unsigned char *s = mem; 305 305 306 306 for (size_t i = 0; i < n; ++i) { 307 307 if (s[i] == (unsigned char) c) { … … 323 323 { 324 324 assert(s != NULL); 325 325 326 326 char *res = gnu_strchrnul(s, c); 327 327 return (*res == c) ? res : NULL; … … 339 339 { 340 340 assert(s != NULL); 341 341 342 342 const char *ptr = strchr(s, '\0'); 343 343 344 344 /* the same as in strchr, except it loops in reverse direction */ 345 345 while (*ptr != (char) c) { … … 365 365 { 366 366 assert(s != NULL); 367 367 368 368 while (*s != c && *s != '\0') { 369 369 s++; 370 370 } 371 371 372 372 return (char *) s; 373 373 } … … 439 439 assert(haystack != NULL); 440 440 assert(needle != NULL); 441 441 442 442 /* Special case - needle is an empty string. */ 443 443 if (needle[0] == '\0') { 444 444 return (char *) haystack; 445 445 } 446 446 447 447 /* Preprocess needle. */ 448 448 size_t nlen = strlen(needle); 449 449 size_t prefix_table[nlen + 1]; 450 450 451 451 { 452 452 size_t i = 0; 453 453 ssize_t j = -1; 454 454 455 455 prefix_table[i] = j; 456 456 457 457 while (i < nlen) { 458 458 while (j >= 0 && needle[i] != needle[j]) { … … 463 463 } 464 464 } 465 465 466 466 /* Search needle using the precomputed table. */ 467 467 size_t npos = 0; 468 468 469 469 for (size_t hpos = 0; haystack[hpos] != '\0'; ++hpos) { 470 470 while (npos != 0 && haystack[hpos] != needle[npos]) { 471 471 npos = prefix_table[npos]; 472 472 } 473 473 474 474 if (haystack[hpos] == needle[npos]) { 475 475 npos++; 476 476 477 477 if (npos == nlen) { 478 478 return (char *) (haystack + hpos - nlen + 1); … … 480 480 } 481 481 } 482 482 483 483 return NULL; 484 484 } … … 604 604 { 605 605 assert(buf != NULL); 606 606 607 607 char *errstr = strerror(errnum); 608 608 609 609 if (strlen(errstr) + 1 > bufsz) { 610 610 return ERANGE; … … 625 625 { 626 626 assert(s != NULL); 627 627 628 628 return (size_t) (strchr(s, '\0') - s); 629 629 } … … 639 639 { 640 640 assert(s != NULL); 641 641 642 642 for (size_t sz = 0; sz < n; ++sz) { 643 643 644 644 if (s[sz] == '\0') { 645 645 return sz; 646 646 } 647 647 } 648 648 649 649 return n; 650 650 }
Note:
See TracChangeset
for help on using the changeset viewer.