Changes in kernel/generic/src/lib/str.c [28a5ebd:42e91ae] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/lib/str.c
r28a5ebd r42e91ae 42 42 * strings, called just strings are encoded in UTF-8. Wide strings (encoded 43 43 * in UTF-32) are supported to a limited degree. A single character is 44 * represented as char32_t.@n44 * represented as wchar_t.@n 45 45 * 46 46 * Overview of the terminology:@n … … 50 50 * byte 8 bits stored in uint8_t (unsigned 8 bit integer) 51 51 * 52 * character UTF-32 encoded Unicode character, stored in char32_t53 * ( unsigned 32 bit integer), code points 0 .. 111411152 * character UTF-32 encoded Unicode character, stored in wchar_t 53 * (signed 32 bit integer), code points 0 .. 1114111 54 54 * are valid 55 55 * … … 61 61 * 62 62 * wide string UTF-32 encoded NULL-terminated Unicode string, 63 * char32_t *63 * wchar_t * 64 64 * 65 65 * [wide] string size number of BYTES in a [wide] string (excluding … … 100 100 * A specific character inside a [wide] string can be referred to by:@n 101 101 * 102 * pointer (char *, char32_t *)102 * pointer (char *, wchar_t *) 103 103 * byte offset (size_t) 104 104 * character index (size_t) … … 118 118 #include <macros.h> 119 119 120 /** Check the condition if wchar_t is signed */ 121 #ifdef __WCHAR_UNSIGNED__ 122 #define WCHAR_SIGNED_CHECK(cond) (true) 123 #else 124 #define WCHAR_SIGNED_CHECK(cond) (cond) 125 #endif 126 120 127 /** Byte mask consisting of lowest @n bits (out of 8) */ 121 128 #define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1)) … … 145 152 * 146 153 */ 147 char32_t str_decode(const char *str, size_t *offset, size_t size)154 wchar_t str_decode(const char *str, size_t *offset, size_t size) 148 155 { 149 156 if (*offset + 1 > size) … … 182 189 return U_SPECIAL; 183 190 184 char32_t ch = b0 & LO_MASK_8(b0_bits);191 wchar_t ch = b0 & LO_MASK_8(b0_bits); 185 192 186 193 /* Decode continuation bytes */ … … 193 200 194 201 /* Shift data bits to ch */ 195 ch = (ch << CONT_BITS) | ( char32_t) (b & LO_MASK_8(CONT_BITS));202 ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS)); 196 203 cbytes--; 197 204 } … … 215 222 * code was invalid. 216 223 */ 217 errno_t chr_encode(const char32_t ch, char *str, size_t *offset, size_t size)224 errno_t chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size) 218 225 { 219 226 if (*offset >= size) … … 301 308 * 302 309 */ 303 size_t wstr_size(const char32_t *str)304 { 305 return (wstr_length(str) * sizeof( char32_t));310 size_t wstr_size(const wchar_t *str) 311 { 312 return (wstr_length(str) * sizeof(wchar_t)); 306 313 } 307 314 … … 347 354 * 348 355 */ 349 size_t wstr_lsize(const char32_t *str, size_t max_len)350 { 351 return (wstr_nlength(str, max_len * sizeof( char32_t)) * sizeof(char32_t));356 size_t wstr_lsize(const wchar_t *str, size_t max_len) 357 { 358 return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t)); 352 359 } 353 360 … … 377 384 * 378 385 */ 379 size_t wstr_length(const char32_t *wstr)386 size_t wstr_length(const wchar_t *wstr) 380 387 { 381 388 size_t len = 0; … … 414 421 * 415 422 */ 416 size_t wstr_nlength(const char32_t *str, size_t size)423 size_t wstr_nlength(const wchar_t *str, size_t size) 417 424 { 418 425 size_t len = 0; 419 size_t limit = ALIGN_DOWN(size, sizeof( char32_t));426 size_t limit = ALIGN_DOWN(size, sizeof(wchar_t)); 420 427 size_t offset = 0; 421 428 422 429 while ((offset < limit) && (*str++ != 0)) { 423 430 len++; 424 offset += sizeof( char32_t);431 offset += sizeof(wchar_t); 425 432 } 426 433 … … 433 440 * 434 441 */ 435 bool ascii_check( char32_t ch)436 { 437 if ( ch <= 127)442 bool ascii_check(wchar_t ch) 443 { 444 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127)) 438 445 return true; 439 446 … … 446 453 * 447 454 */ 448 bool chr_check( char32_t ch)449 { 450 if ( ch <= 1114111)455 bool chr_check(wchar_t ch) 456 { 457 if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111)) 451 458 return true; 452 459 … … 474 481 int str_cmp(const char *s1, const char *s2) 475 482 { 476 char32_t c1 = 0;477 char32_t c2 = 0;483 wchar_t c1 = 0; 484 wchar_t c2 = 0; 478 485 479 486 size_t off1 = 0; … … 521 528 int str_lcmp(const char *s1, const char *s2, size_t max_len) 522 529 { 523 char32_t c1 = 0;524 char32_t c2 = 0;530 wchar_t c1 = 0; 531 wchar_t c2 = 0; 525 532 526 533 size_t off1 = 0; … … 573 580 size_t dest_off = 0; 574 581 575 char32_t ch;582 wchar_t ch; 576 583 while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) { 577 584 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) … … 606 613 size_t dest_off = 0; 607 614 608 char32_t ch;615 wchar_t ch; 609 616 while ((ch = str_decode(src, &src_off, n)) != 0) { 610 617 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) … … 621 628 * written will always be well-formed. 622 629 * 623 * @param dest 624 * @param size 625 * @param src 626 */ 627 void wstr_to_str(char *dest, size_t size, const char32_t *src)628 { 629 char32_t ch;630 * @param dest Destination buffer. 631 * @param size Size of the destination buffer. 632 * @param src Source wide string. 633 */ 634 void wstr_to_str(char *dest, size_t size, const wchar_t *src) 635 { 636 wchar_t ch; 630 637 size_t src_idx; 631 638 size_t dest_off; … … 652 659 * @return Pointer to character in @a str or NULL if not found. 653 660 */ 654 char *str_chr(const char *str, char32_t ch)655 { 656 char32_t acc;661 char *str_chr(const char *str, wchar_t ch) 662 { 663 wchar_t acc; 657 664 size_t off = 0; 658 665 size_t last = 0; … … 681 688 * 682 689 */ 683 bool wstr_linsert( char32_t *str, char32_t ch, size_t pos, size_t max_pos)690 bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos) 684 691 { 685 692 size_t len = wstr_length(str); … … 709 716 * 710 717 */ 711 bool wstr_remove( char32_t *str, size_t pos)718 bool wstr_remove(wchar_t *str, size_t pos) 712 719 { 713 720 size_t len = wstr_length(str);
Note:
See TracChangeset
for help on using the changeset viewer.