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