Changes in uspace/lib/posix/string.c [087c4c56:27eddb52] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/string.c
r087c4c56 r27eddb52 31 31 * @{ 32 32 */ 33 /** @file String manipulation.33 /** @file 34 34 */ 35 35 … … 48 48 49 49 /** 50 * Decides whethers2 is a prefix of s1.51 * 52 * @param s1 String in which to look for a prefix.53 * @param s2 Prefix string to look for.54 * @return True if s2 is a prefix of s1, false otherwise.50 * Returns true if s2 is a prefix of s1. 51 * 52 * @param s1 53 * @param s2 54 * @return 55 55 */ 56 56 static bool begins_with(const char *s1, const char *s2) … … 69 69 * if no occurence is found. 70 70 * 71 * @param s1 String in which to look for the bytes. 72 * @param s2 String of bytes to look for. 73 * @return Pointer to the found byte on success, pointer to the 74 * string terminator otherwise. 71 * @param s1 72 * @param s2 73 * @return 75 74 */ 76 75 static char *strpbrk_null(const char *s1, const char *s2) … … 84 83 85 84 /** 86 * Copy a string. 87 * 88 * @param dest Destination pre-allocated buffer. 89 * @param src Source string to be copied. 90 * @return Pointer to the destination buffer. 91 */ 92 char *posix_strcpy(char *restrict dest, const char *restrict src) 85 * 86 * @param dest 87 * @param src 88 * @return 89 */ 90 char *posix_strcpy(char *dest, const char *src) 93 91 { 94 92 posix_stpcpy(dest, src); … … 97 95 98 96 /** 99 * Copy fixed length string. 100 * 101 * @param dest Destination pre-allocated buffer. 102 * @param src Source string to be copied. 103 * @param n Number of bytes to be stored into destination buffer. 104 * @return Pointer to the destination buffer. 105 */ 106 char *posix_strncpy(char *restrict dest, const char *restrict src, size_t n) 97 * 98 * @param dest 99 * @param src 100 * @param n 101 * @return 102 */ 103 char *posix_strncpy(char *dest, const char *src, size_t n) 107 104 { 108 105 posix_stpncpy(dest, src, n); … … 111 108 112 109 /** 113 * Copy a string. 114 * 115 * @param dest Destination pre-allocated buffer. 116 * @param src Source string to be copied. 117 * @return Pointer to the nul character in the destination string. 110 * 111 * @param dest 112 * @param src 113 * @return Pointer to the nul character in the dest string 118 114 */ 119 115 char *posix_stpcpy(char *restrict dest, const char *restrict src) … … 136 132 137 133 /** 138 * Copy fixed length string. 139 * 140 * @param dest Destination pre-allocated buffer. 141 * @param src Source string to be copied. 142 * @param n Number of bytes to be stored into destination buffer. 143 * @return Pointer to the first written nul character or &dest[n]. 134 * 135 * @param dest 136 * @param src 137 * @param n 138 * @return Pointer to the first written nul character or &dest[n] 144 139 */ 145 140 char *posix_stpncpy(char *restrict dest, const char *restrict src, size_t n) … … 167 162 168 163 /** 169 * Concatenate two strings. 170 * 171 * @param dest String to which src shall be appended. 172 * @param src String to be appended after dest. 173 * @return Pointer to destination buffer. 174 */ 175 char *posix_strcat(char *restrict dest, const char *restrict src) 164 * 165 * @param dest 166 * @param src 167 * @return 168 */ 169 char *posix_strcat(char *dest, const char *src) 176 170 { 177 171 assert(dest != NULL); … … 183 177 184 178 /** 185 * Concatenate a string with part of another. 186 * 187 * @param dest String to which part of src shall be appended. 188 * @param src String whose part shall be appended after dest. 189 * @param n Number of bytes to append after dest. 190 * @return Pointer to destination buffer. 191 */ 192 char *posix_strncat(char *restrict dest, const char *restrict src, size_t n) 179 * 180 * @param dest 181 * @param src 182 * @param n 183 * @return 184 */ 185 char *posix_strncat(char *dest, const char *src, size_t n) 193 186 { 194 187 assert(dest != NULL); … … 202 195 203 196 /** 204 * Copy limited number of bytes in memory. 205 * 206 * @param dest Destination buffer. 207 * @param src Source buffer. 208 * @param c Character after which the copying shall stop. 209 * @param n Number of bytes that shall be copied if not stopped earlier by c. 197 * 198 * @param dest 199 * @param src 200 * @param c 201 * @param n 210 202 * @return Pointer to the first byte after c in dest if found, NULL otherwise. 211 203 */ 212 void *posix_memccpy(void * restrict dest, const void *restrictsrc, int c, size_t n)204 void *posix_memccpy(void *dest, const void *src, int c, size_t n) 213 205 { 214 206 assert(dest != NULL); … … 231 223 232 224 /** 233 * Duplicate a string. 234 * 235 * @param s String to be duplicated. 236 * @return Newly allocated copy of the string. 225 * 226 * @param s 227 * @return Newly allocated string 237 228 */ 238 229 char *posix_strdup(const char *s) … … 242 233 243 234 /** 244 * Duplicate a specific number of bytes from a string. 245 * 246 * @param s String to be duplicated. 247 * @param n Maximum length of the resulting string.. 248 * @return Newly allocated string copy of length at most n. 235 * 236 * @param s 237 * @param n 238 * @return Newly allocated string of length at most n 249 239 */ 250 240 char *posix_strndup(const char *s, size_t n) … … 265 255 266 256 /** 267 * Compare bytes in memory. 268 * 269 * @param mem1 First area of memory to be compared. 270 * @param mem2 Second area of memory to be compared. 271 * @param n Maximum number of bytes to be compared. 257 * 258 * @param mem1 259 * @param mem2 260 * @param n 272 261 * @return Difference of the first pair of inequal bytes, 273 * or 0 if areas have the same content .262 * or 0 if areas have the same content 274 263 */ 275 264 int posix_memcmp(const void *mem1, const void *mem2, size_t n) … … 283 272 for (size_t i = 0; i < n; ++i) { 284 273 if (s1[i] != s2[i]) { 285 return s 1[i] - s2[i];274 return s2[i] - s1[i]; 286 275 } 287 276 } … … 291 280 292 281 /** 293 * Compare two strings. 294 * 295 * @param s1 First string to be compared. 296 * @param s2 Second string to be compared. 297 * @return Difference of the first pair of inequal characters, 298 * or 0 if strings have the same content. 282 * 283 * @param s1 284 * @param s2 285 * @return 299 286 */ 300 287 int posix_strcmp(const char *s1, const char *s2) … … 307 294 308 295 /** 309 * Compare part of two strings. 310 * 311 * @param s1 First string to be compared. 312 * @param s2 Second string to be compared. 313 * @param n Maximum number of characters to be compared. 314 * @return Difference of the first pair of inequal characters, 315 * or 0 if strings have the same content. 296 * 297 * @param s1 298 * @param s2 299 * @param n 300 * @return 316 301 */ 317 302 int posix_strncmp(const char *s1, const char *s2, size_t n) … … 322 307 for (size_t i = 0; i < n; ++i) { 323 308 if (s1[i] != s2[i]) { 324 return s 1[i] - s2[i];309 return s2[i] - s1[i]; 325 310 } 326 311 if (s1[i] == '\0') { … … 333 318 334 319 /** 335 * Find byte in memory. 336 * 337 * @param mem Memory area in which to look for the byte. 338 * @param c Byte to look for. 339 * @param n Maximum number of bytes to be inspected. 340 * @return Pointer to the specified byte on success, 341 * NULL pointer otherwise. 320 * 321 * @param mem 322 * @param c 323 * @param n 324 * @return 342 325 */ 343 326 void *posix_memchr(const void *mem, int c, size_t n) … … 356 339 357 340 /** 358 * Scan string for a first occurence of a character. 359 * 360 * @param s String in which to look for the character. 361 * @param c Character to look for. 362 * @return Pointer to the specified character on success, 363 * NULL pointer otherwise. 341 * 342 * @param s 343 * @param c 344 * @return 364 345 */ 365 346 char *posix_strchr(const char *s, int c) … … 372 353 373 354 /** 374 * Scan string for a last occurence of a character. 375 * 376 * @param s String in which to look for the character. 377 * @param c Character to look for. 378 * @return Pointer to the specified character on success, 379 * NULL pointer otherwise. 355 * 356 * @param s 357 * @param c 358 * @return 380 359 */ 381 360 char *posix_strrchr(const char *s, int c) … … 397 376 } 398 377 399 /**400 * Scan string for a first occurence of a character.401 *402 * @param s String in which to look for the character.403 * @param c Character to look for.404 * @return Pointer to the specified character on success, pointer to the405 * string terminator otherwise.406 */407 378 char *gnu_strchrnul(const char *s, int c) 408 379 { … … 417 388 418 389 /** 419 * Scan a string for a first occurence of one of provided bytes. 420 * 421 * @param s1 String in which to look for the bytes. 422 * @param s2 String of bytes to look for. 423 * @return Pointer to the found byte on success, 424 * NULL pointer otherwise. 390 * 391 * @param s1 392 * @param s2 393 * @return 425 394 */ 426 395 char *posix_strpbrk(const char *s1, const char *s2) … … 434 403 435 404 /** 436 * Get the length of a complementary substring. 437 * 438 * @param s1 String that shall be searched for complementary prefix. 439 * @param s2 String of bytes that shall not occur in the prefix. 440 * @return Length of the prefix. 405 * 406 * @param s1 407 * @param s2 408 * @return 441 409 */ 442 410 size_t posix_strcspn(const char *s1, const char *s2) … … 450 418 451 419 /** 452 * Get length of a substring. 453 * 454 * @param s1 String that shall be searched for prefix. 455 * @param s2 String of bytes that the prefix must consist of. 456 * @return Length of the prefix. 420 * 421 * @param s1 422 * @param s2 423 * @return 457 424 */ 458 425 size_t posix_strspn(const char *s1, const char *s2) … … 471 438 472 439 /** 473 * Find a substring. 474 * 475 * @param s1 String in which to look for a substring. 476 * @param s2 Substring to look for. 477 * @return Pointer to the first character of the substring in s1, or NULL if 478 * not found. 440 * 441 * @param s1 442 * @param s2 443 * @return 479 444 */ 480 445 char *posix_strstr(const char *s1, const char *s2) … … 502 467 503 468 /** 504 * String comparison using collating information.505 *506 469 * Currently ignores locale and just calls strcmp. 507 470 * 508 * @param s1 First string to be compared. 509 * @param s2 Second string to be compared. 510 * @return Difference of the first pair of inequal characters, 511 * or 0 if strings have the same content. 471 * @param s1 472 * @param s2 473 * @return 512 474 */ 513 475 int posix_strcoll(const char *s1, const char *s2) … … 520 482 521 483 /** 522 * Transform a string in such a way that the resulting string yields the same 523 * results when passed to the strcmp as if the original string is passed to 524 * the strcoll. 525 * 526 * Since strcoll is equal to strcmp here, this just makes a copy. 527 * 528 * @param s1 Transformed string. 529 * @param s2 Original string. 530 * @param n Maximum length of the transformed string. 531 * @return Length of the transformed string. 532 */ 533 size_t posix_strxfrm(char *restrict s1, const char *restrict s2, size_t n) 484 * strcoll is equal to strcmp here, so this just makes a copy. 485 * 486 * @param s1 487 * @param s2 488 * @param n 489 * @return 490 */ 491 size_t posix_strxfrm(char *s1, const char *s2, size_t n) 534 492 { 535 493 assert(s1 != NULL || n == 0); … … 546 504 547 505 /** 548 * Get error message string. 549 * 550 * @param errnum Error code for which to obtain human readable string. 551 * @return Error message. 506 * 507 * @param errnum 508 * @return 552 509 */ 553 510 char *posix_strerror(int errnum) … … 561 518 562 519 /** 563 * Get error message string. 564 * 565 * @param errnum Error code for which to obtain human readable string. 566 * @param buf Buffer to store a human readable string to. 567 * @param bufsz Size of buffer pointed to by buf. 568 * @return Zero on success, errno otherwise. 520 * 521 * @param errnum Error code 522 * @param buf Buffer to store a human readable string to 523 * @param bufsz Size of buffer pointed to by buf 524 * @return 569 525 */ 570 526 int posix_strerror_r(int errnum, char *buf, size_t bufsz) … … 585 541 586 542 /** 587 * Get length of the string. 588 * 589 * @param s String which length shall be determined. 590 * @return Length of the string. 543 * 544 * @param s 545 * @return 591 546 */ 592 547 size_t posix_strlen(const char *s) … … 598 553 599 554 /** 600 * Get limited length of the string. 601 * 602 * @param s String which length shall be determined. 603 * @param n Maximum number of bytes that can be examined to determine length. 604 * @return The lower of either string length or n limit. 555 * 556 * @param s 557 * @param n 558 * @return 605 559 */ 606 560 size_t posix_strnlen(const char *s, size_t n) … … 619 573 620 574 /** 621 * Get description of a signal. 622 * 623 * @param signum Signal number. 624 * @return Human readable signal description. 575 * 576 * @param signum 577 * @return 625 578 */ 626 579 char *posix_strsignal(int signum)
Note:
See TracChangeset
for help on using the changeset viewer.