Changes in kernel/generic/src/lib/str.c [1b20da0:a35b458] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/lib/str.c
r1b20da0 ra35b458 151 151 if (*offset + 1 > size) 152 152 return 0; 153 153 154 154 /* First byte read from string */ 155 155 uint8_t b0 = (uint8_t) str[(*offset)++]; 156 156 157 157 /* Determine code length */ 158 158 159 159 unsigned int b0_bits; /* Data bits in first byte */ 160 160 unsigned int cbytes; /* Number of continuation bytes */ 161 161 162 162 if ((b0 & 0x80) == 0) { 163 163 /* 0xxxxxxx (Plain ASCII) */ … … 180 180 return U_SPECIAL; 181 181 } 182 182 183 183 if (*offset + cbytes > size) 184 184 return U_SPECIAL; 185 185 186 186 wchar_t ch = b0 & LO_MASK_8(b0_bits); 187 187 188 188 /* Decode continuation bytes */ 189 189 while (cbytes > 0) { 190 190 uint8_t b = (uint8_t) str[(*offset)++]; 191 191 192 192 /* Must be 10xxxxxx */ 193 193 if ((b & 0xc0) != 0x80) 194 194 return U_SPECIAL; 195 195 196 196 /* Shift data bits to ch */ 197 197 ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS)); 198 198 cbytes--; 199 199 } 200 200 201 201 return ch; 202 202 } … … 221 221 if (*offset >= size) 222 222 return EOVERFLOW; 223 223 224 224 if (!chr_check(ch)) 225 225 return EINVAL; 226 226 227 227 /* Unsigned version of ch (bit operations should only be done 228 228 on unsigned types). */ 229 229 uint32_t cc = (uint32_t) ch; 230 230 231 231 /* Determine how many continuation bytes are needed */ 232 232 233 233 unsigned int b0_bits; /* Data bits in first byte */ 234 234 unsigned int cbytes; /* Number of continuation bytes */ 235 235 236 236 if ((cc & ~LO_MASK_32(7)) == 0) { 237 237 b0_bits = 7; … … 250 250 return EINVAL; 251 251 } 252 252 253 253 /* Check for available space in buffer */ 254 254 if (*offset + cbytes >= size) 255 255 return EOVERFLOW; 256 256 257 257 /* Encode continuation bytes */ 258 258 unsigned int i; … … 261 261 cc = cc >> CONT_BITS; 262 262 } 263 263 264 264 /* Encode first byte */ 265 265 str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); 266 266 267 267 /* Advance offset */ 268 268 *offset += cbytes + 1; 269 269 270 270 return EOK; 271 271 } … … 284 284 { 285 285 size_t size = 0; 286 286 287 287 while (*str++ != 0) 288 288 size++; 289 289 290 290 return size; 291 291 } … … 323 323 size_t len = 0; 324 324 size_t offset = 0; 325 325 326 326 while (len < max_len) { 327 327 if (str_decode(str, &offset, STR_NO_LIMIT) == 0) 328 328 break; 329 329 330 330 len++; 331 331 } 332 332 333 333 return offset; 334 334 } … … 363 363 size_t len = 0; 364 364 size_t offset = 0; 365 365 366 366 while (str_decode(str, &offset, STR_NO_LIMIT) != 0) 367 367 len++; 368 368 369 369 return len; 370 370 } … … 380 380 { 381 381 size_t len = 0; 382 382 383 383 while (*wstr++ != 0) 384 384 len++; 385 385 386 386 return len; 387 387 } … … 399 399 size_t len = 0; 400 400 size_t offset = 0; 401 401 402 402 while (str_decode(str, &offset, size) != 0) 403 403 len++; 404 404 405 405 return len; 406 406 } … … 419 419 size_t limit = ALIGN_DOWN(size, sizeof(wchar_t)); 420 420 size_t offset = 0; 421 421 422 422 while ((offset < limit) && (*str++ != 0)) { 423 423 len++; 424 424 offset += sizeof(wchar_t); 425 425 } 426 426 427 427 return len; 428 428 } … … 437 437 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127)) 438 438 return true; 439 439 440 440 return false; 441 441 } … … 450 450 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111)) 451 451 return true; 452 452 453 453 return false; 454 454 } … … 476 476 wchar_t c1 = 0; 477 477 wchar_t c2 = 0; 478 478 479 479 size_t off1 = 0; 480 480 size_t off2 = 0; … … 486 486 if (c1 < c2) 487 487 return -1; 488 488 489 489 if (c1 > c2) 490 490 return 1; … … 523 523 wchar_t c1 = 0; 524 524 wchar_t c2 = 0; 525 525 526 526 size_t off1 = 0; 527 527 size_t off2 = 0; 528 528 529 529 size_t len = 0; 530 530 … … 569 569 assert(size > 0); 570 570 assert(src != NULL); 571 571 572 572 size_t src_off = 0; 573 573 size_t dest_off = 0; 574 574 575 575 wchar_t ch; 576 576 while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) { … … 578 578 break; 579 579 } 580 580 581 581 dest[dest_off] = '\0'; 582 582 } … … 602 602 /* There must be space for a null terminator in the buffer. */ 603 603 assert(size > 0); 604 604 605 605 size_t src_off = 0; 606 606 size_t dest_off = 0; 607 607 608 608 wchar_t ch; 609 609 while ((ch = str_decode(src, &src_off, n)) != 0) { … … 611 611 break; 612 612 } 613 613 614 614 dest[dest_off] = '\0'; 615 615 } … … 636 636 char *dest = malloc(size, 0); 637 637 assert(dest); 638 638 639 639 str_cpy(dest, size, src); 640 640 return dest; … … 666 666 if (size > n) 667 667 size = n; 668 668 669 669 char *dest = malloc(size + 1, 0); 670 670 assert(dest); 671 671 672 672 str_ncpy(dest, size + 1, src, size); 673 673 return dest; … … 695 695 src_idx = 0; 696 696 dest_off = 0; 697 697 698 698 while ((ch = src[src_idx++]) != 0) { 699 699 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) … … 717 717 size_t off = 0; 718 718 size_t last = 0; 719 719 720 720 while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) { 721 721 if (acc == ch) … … 723 723 last = off; 724 724 } 725 725 726 726 return NULL; 727 727 } … … 744 744 { 745 745 size_t len = wstr_length(str); 746 746 747 747 if ((pos > len) || (pos + 1 > max_pos)) 748 748 return false; 749 749 750 750 size_t i; 751 751 for (i = len; i + 1 > pos; i--) 752 752 str[i + 1] = str[i]; 753 753 754 754 str[pos] = ch; 755 755 756 756 return true; 757 757 } … … 772 772 { 773 773 size_t len = wstr_length(str); 774 774 775 775 if (pos >= len) 776 776 return false; 777 777 778 778 size_t i; 779 779 for (i = pos + 1; i <= len; i++) 780 780 str[i - 1] = str[i]; 781 781 782 782 return true; 783 783 } … … 800 800 assert(neg != NULL); 801 801 assert(result != NULL); 802 802 803 803 *neg = false; 804 804 const char *str = nptr; 805 805 806 806 /* Ignore leading whitespace */ 807 807 while (isspace(*str)) 808 808 str++; 809 809 810 810 if (*str == '-') { 811 811 *neg = true; … … 813 813 } else if (*str == '+') 814 814 str++; 815 815 816 816 if (base == 0) { 817 817 /* Decode base if not specified */ 818 818 base = 10; 819 819 820 820 if (*str == '0') { 821 821 base = 8; 822 822 str++; 823 823 824 824 switch (*str) { 825 825 case 'b': … … 856 856 } 857 857 } 858 858 859 859 *result = 0; 860 860 const char *startstr = str; 861 861 862 862 while (*str != 0) { 863 863 unsigned int digit; 864 864 865 865 if ((*str >= 'a') && (*str <= 'z')) 866 866 digit = *str - 'a' + 10; … … 871 871 else 872 872 break; 873 873 874 874 if (digit >= base) 875 875 break; 876 876 877 877 uint64_t prev = *result; 878 878 *result = (*result) * base + digit; 879 879 880 880 if (*result < prev) { 881 881 /* Overflow */ … … 883 883 return EOVERFLOW; 884 884 } 885 885 886 886 str++; 887 887 } 888 888 889 889 if (str == startstr) { 890 890 /* … … 894 894 str = nptr; 895 895 } 896 896 897 897 *endptr = (char *) str; 898 898 899 899 if (str == nptr) 900 900 return EINVAL; 901 901 902 902 return EOK; 903 903 } … … 919 919 { 920 920 assert(result != NULL); 921 921 922 922 bool neg; 923 923 char *lendptr; 924 924 errno_t ret = str_uint(nptr, &lendptr, base, &neg, result); 925 925 926 926 if (endptr != NULL) 927 927 *endptr = (char *) lendptr; 928 928 929 929 if (ret != EOK) 930 930 return ret; 931 931 932 932 /* Do not allow negative values */ 933 933 if (neg) 934 934 return EINVAL; 935 935 936 936 /* Check whether we are at the end of 937 937 the string in strict mode */ 938 938 if ((strict) && (*lendptr != 0)) 939 939 return EINVAL; 940 940 941 941 return EOK; 942 942 }
Note:
See TracChangeset
for help on using the changeset viewer.