Changeset e1813cf in mainline
- Timestamp:
- 2009-03-31T22:51:41Z (16 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ce87a8aa
- Parents:
- b54d2f1
- Location:
- kernel/generic
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/include/string.h
rb54d2f1 re1813cf 38 38 #include <typedefs.h> 39 39 40 #define UTF8_NO_LIMIT (( index_t) -1)40 #define UTF8_NO_LIMIT ((size_t) -1) 41 41 42 42 extern char invalch; 43 43 44 extern wchar_t utf8_decode(const char *str, index_t *index, index_t limit);45 extern bool utf8_encode(const wchar_t ch, char *str, index_t *index, index_t limit);44 extern wchar_t chr_decode(const char *, size_t *, size_t); 45 extern bool chr_encode(const wchar_t, char *, size_t *, size_t limit); 46 46 extern size_t utf8_count_bytes(const char *str, count_t count); 47 47 extern bool ascii_check(const wchar_t ch); -
kernel/generic/src/lib/string.c
rb54d2f1 re1813cf 57 57 #define CONT_BITS 6 58 58 59 /** Decode a single UTF-8 character from a NULL-terminatedstring.60 * 61 * Decode a single UTF-8 character from a plain char NULL-terminated62 * string. Decoding starts at @index and this index is moved to the63 * beginning of the next character. In case of decoding error,64 * index advances. However, index is never moved beyond (str+limit).65 * 66 * @param str Plain character NULL-terminated string.59 /** Decode a single character from a substring. 60 * 61 * Decode a single character from a substring of size @a sz. Decoding starts 62 * at @a offset and this offset is moved to the beginning of the next 63 * character. In case of decoding error, offset generally advances at least 64 * by one. However, offset is never moved beyond (str + sz). 65 * 66 * @param str String (not necessarily NULL-terminated). 67 67 * @param index Index (counted in plain characters) where to start 68 68 * the decoding. 69 * @param limit Maximal allowed value of index.70 * 71 * @return Decoded character in UTF-32 or '?' if the encoding is wrong.72 * 73 */ 74 wchar_t utf8_decode(const char *str, index_t *index, index_t limit)69 * @param limit Size of the substring. 70 * 71 * @return Value of decoded character or '?' on decoding error. 72 * 73 */ 74 wchar_t chr_decode(const char *str, size_t *offset, size_t sz) 75 75 { 76 76 uint8_t b0, b; /* Bytes read from str. */ … … 80 80 int cbytes; /* Number of continuation bytes. */ 81 81 82 if (* index + 1 > limit)82 if (*offset + 1 > sz) 83 83 return invalch; 84 84 85 b0 = (uint8_t) str[(* index)++];85 b0 = (uint8_t) str[(*offset)++]; 86 86 87 87 /* Determine code length. */ … … 108 108 } 109 109 110 if (* index + cbytes > limit) {110 if (*offset + cbytes > sz) { 111 111 return invalch; 112 112 } … … 116 116 /* Decode continuation bytes. */ 117 117 while (cbytes > 0) { 118 b = (uint8_t) str[(* index)++];118 b = (uint8_t) str[(*offset)++]; 119 119 120 120 /* Must be 10xxxxxx. */ … … 131 131 } 132 132 133 /** Encode a single UTF-32 character as UTF-8 134 * 135 * Encode a single UTF-32 character as UTF-8 and store it into 136 * the given buffer at @index. Encoding starts at @index and 137 * this index is moved at the position where the next character 138 * can be written to. 139 * 140 * @param ch Input UTF-32 character. 141 * @param str Output buffer. 142 * @param index Index (counted in plain characters) where to start 143 * the encoding 144 * @param limit Maximal allowed value of index. 145 * 146 * @return True if the character was encoded or false if there is not 147 * enought space in the output buffer or the character is invalid 148 * Unicode code point. 149 * 150 */ 151 bool utf8_encode(const wchar_t ch, char *str, index_t *index, index_t limit) 133 /** Encode a single character to string representation. 134 * 135 * Encode a single character to string representation (i.e. UTF-8) and store 136 * it into a buffer at @a offset. Encoding starts at @a offset and this offset 137 * is moved to the position where the next character can be written to. 138 * 139 * @param ch Input character. 140 * @param str Output buffer. 141 * @param offset Offset (in bytes) where to start writing. 142 * @param sz Size of the output buffer. 143 * 144 * @return True if the character was encoded successfully or false if there 145 * was not enough space in the output buffer or the character code 146 * was invalid. 147 */ 148 bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz) 152 149 { 153 150 uint32_t cc; /* Unsigned version of ch. */ … … 157 154 int i; 158 155 159 if (* index >= limit)156 if (*offset >= sz) 160 157 return false; 161 158 … … 185 182 186 183 /* Check for available space in buffer. */ 187 if (* index + cbytes >= limit)184 if (*offset + cbytes >= sz) 188 185 return false; 189 186 190 187 /* Encode continuation bytes. */ 191 188 for (i = cbytes; i > 0; --i) { 192 str[* index+ i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));189 str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS)); 193 190 cc = cc >> CONT_BITS; 194 191 } 195 192 196 193 /* Encode first byte. */ 197 str[* index] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);198 199 /* Advance index. */200 * index+= (1 + cbytes);194 str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); 195 196 /* Advance offset. */ 197 *offset += (1 + cbytes); 201 198 202 199 return true; … … 227 224 if (size >= count) 228 225 break; 229 ch = utf8_decode(str, &index, UTF8_NO_LIMIT);226 ch = chr_decode(str, &index, UTF8_NO_LIMIT); 230 227 if (ch == '\0') break; 231 228 … … 289 286 index_t index = 0; 290 287 291 while ( utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) {288 while (chr_decode(str, &index, UTF8_NO_LIMIT) != 0) { 292 289 size++; 293 290 } -
kernel/generic/src/printf/printf_core.c
rb54d2f1 re1813cf 595 595 while (true) { 596 596 i = nxt; 597 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);597 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 598 598 599 599 if (uc == '\0') break; … … 619 619 do { 620 620 i = nxt; 621 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);621 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 622 622 switch (uc) { 623 623 case '#': … … 649 649 650 650 i = nxt; 651 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);651 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 652 652 if (uc == '\0') 653 653 break; … … 658 658 /* Get width value from argument list */ 659 659 i = nxt; 660 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);660 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 661 661 width = (int) va_arg(ap, int); 662 662 if (width < 0) { … … 671 671 if (uc == '.') { 672 672 i = nxt; 673 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);673 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 674 674 if (isdigit(uc)) { 675 675 while (true) { … … 678 678 679 679 i = nxt; 680 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);680 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 681 681 if (uc == '\0') 682 682 break; … … 687 687 /* Get precision value from the argument list */ 688 688 i = nxt; 689 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);689 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 690 690 precision = (int) va_arg(ap, int); 691 691 if (precision < 0) { … … 706 706 qualifier = PrintfQualifierShort; 707 707 i = nxt; 708 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);708 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 709 709 if (uc == 'h') { 710 710 i = nxt; 711 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);711 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 712 712 qualifier = PrintfQualifierByte; 713 713 } … … 717 717 qualifier = PrintfQualifierLong; 718 718 i = nxt; 719 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);719 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 720 720 if (uc == 'l') { 721 721 i = nxt; 722 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);722 uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT); 723 723 qualifier = PrintfQualifierLongLong; 724 724 } -
kernel/generic/src/printf/vprintf.c
rb54d2f1 re1813cf 50 50 51 51 while (index < size) { 52 putchar( utf8_decode(str, &index, size));52 putchar(chr_decode(str, &index, size)); 53 53 chars++; 54 54 } … … 75 75 wchar_t uc; 76 76 77 while ((uc = utf8_decode(str, &index, UTF8_NO_LIMIT)) != 0) {77 while ((uc = chr_decode(str, &index, UTF8_NO_LIMIT)) != 0) { 78 78 putchar(uc); 79 79 chars++; -
kernel/generic/src/printf/vsnprintf.c
rb54d2f1 re1813cf 85 85 86 86 while (index < size) { 87 wchar_t uc = utf8_decode(str, &index, size);87 wchar_t uc = chr_decode(str, &index, size); 88 88 89 if (! utf8_encode(uc, data->dst, &data->len, data->size - 1))89 if (!chr_encode(uc, data->dst, &data->len, data->size - 1)) 90 90 break; 91 91 } … … 147 147 } 148 148 149 if (! utf8_encode(str[index], data->dst, &data->len, data->size - 1))149 if (!chr_encode(str[index], data->dst, &data->len, data->size - 1)) 150 150 break; 151 151
Note:
See TracChangeset
for help on using the changeset viewer.