Changeset 82bb9c1 in mainline
- Timestamp:
- 2009-04-02T20:38:51Z (16 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 58d5a7e7
- Parents:
- d09f84e6
- Location:
- kernel/generic
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/include/string.h
rd09f84e6 r82bb9c1 43 43 44 44 extern wchar_t chr_decode(const char *, size_t *, size_t); 45 extern int chr_encode(const wchar_t, char *, size_t *, size_t); 45 extern int chr_encode(wchar_t, char *, size_t *, size_t); 46 extern count_t chr_width(wchar_t); 46 47 47 extern size_t str_size(const char * str);48 extern size_t str_size(const char *); 48 49 extern size_t str_lsize(const char *, count_t); 49 extern count_t str_length(const char *str); 50 extern count_t wstr_length(const wchar_t *str); 50 extern size_t str_wsize(const char *, count_t); 51 52 extern count_t str_length(const char *); 53 extern count_t wstr_length(const wchar_t *); 54 extern count_t wstr_wlength(const wchar_t *, count_t); 51 55 52 56 extern bool ascii_check(const wchar_t ch); -
kernel/generic/src/lib/string.c
rd09f84e6 r82bb9c1 33 33 /** 34 34 * @file 35 * @brief Miscellaneous functions. 35 * @brief String functions. 36 * 37 * Strings and characters use the Universal Character Set (UCS). The standard 38 * strings, called just strings are encoded in UTF-8. Wide strings (encoded 39 * in UTF-32) are supported to a limited degree. A single character is 40 * represented as wchar_t. 41 * 42 * Strings have the following metrics: 43 * 44 * Metric Abbrev. Meaning 45 * ------ ------ ------- 46 * size n Number of bytes the string is encoded into, excluding 47 * the null terminator. 48 * length l The number of characters in the string, excluding 49 * the null terminator. 50 * width w The number of character cells the string takes up on a 51 * monospace display. 52 * 53 * Naming scheme: 54 * 55 * chr_xxx operate on characters 56 * str_xxx operate on strings 57 * wstr_xxx operate on wide strings 58 * 59 * [w]str_[n|l|w]xxx operate on a prefix limited by size, length 60 * or width. 36 61 */ 37 62 … … 146 171 * code was invalid. 147 172 */ 148 int chr_encode( constwchar_t ch, char *str, size_t *offset, size_t sz)173 int chr_encode(wchar_t ch, char *str, size_t *offset, size_t sz) 149 174 { 150 175 uint32_t cc; /* Unsigned version of ch. */ … … 200 225 } 201 226 227 /** Get display width of character. 228 * 229 * @param ch The character. 230 * @return Character width in display cells. 231 */ 232 count_t chr_width(wchar_t ch) 233 { 234 return 1; 235 } 236 202 237 /** Get size of string, with length limit. 203 238 * … … 206 241 * the length of @a str, the entire string is measured. 207 242 * 208 * @param str 209 * @param count 210 * 211 * @return 243 * @param str String to consider. 244 * @param count Maximum number of characters to measure. 245 * 246 * @return Number of bytes used by the characters. 212 247 */ 213 248 size_t str_lsize(const char *str, count_t max_len) … … 231 266 } 232 267 268 /** Get size of string, with width limit. 269 * 270 * Get the number of bytes which are used by the longest prefix of @a str 271 * that can fit into @a max_width display cells. 272 * 273 * @param str String to consider. 274 * @param count Maximum number of display cells. 275 * 276 * @return Number of bytes used by the characters that fit. 277 */ 278 size_t str_wsize(const char *str, count_t max_width) 279 { 280 count_t width = 0; 281 size_t cur = 0; 282 size_t prev; 283 wchar_t ch; 284 285 while (true) { 286 prev = cur; 287 if (width >= max_width) 288 break; 289 ch = chr_decode(str, &cur, UTF8_NO_LIMIT); 290 if (ch == '\0') break; 291 292 width += chr_width(ch); 293 } 294 295 return prev; 296 } 297 298 299 /** Get length of wide string, with width limit. 300 * 301 * Get the number of characters in a wide string that can fit into @a max_width 302 * display cells. 303 * 304 * @param wstr Wide string to consider. 305 * @param count Maximum number of display cells. 306 * 307 * @return Number of bytes used by the characters that fit. 308 */ 309 count_t wstr_wlength(const wchar_t *wstr, count_t max_width) 310 { 311 count_t width = 0; 312 index_t cur = 0; 313 314 while (true) { 315 if (width >= max_width) 316 break; 317 if (wstr[cur] == '\0') break; 318 319 width += chr_width(wstr[cur]); 320 ++cur; 321 } 322 323 return (count_t) cur; 324 } 325 233 326 /** Check whether character is plain ASCII. 234 327 * … … 291 384 /** Return number of characters in a wide string. 292 385 * 293 * @param 294 * @return 386 * @param str NULL-terminated wide string. 387 * @return Number of characters in @a str. 295 388 */ 296 389 count_t wstr_length(const wchar_t *wstr) -
kernel/generic/src/printf/printf_core.c
rd09f84e6 r82bb9c1 269 269 270 270 int retval; 271 size_t size = str_ lsize(str, precision);271 size_t size = str_wsize(str, precision); 272 272 if ((retval = printf_putnchars_utf8(str, size, ps)) < 0) 273 273 return -counter; … … 314 314 315 315 int retval; 316 size_t size = min(strw, precision) * sizeof(wchar_t);316 size_t size = wstr_wlength(wstr, precision) * sizeof(wchar_t); 317 317 if ((retval = printf_putnchars_utf32(wstr, size, ps)) < 0) 318 318 return -counter;
Note:
See TracChangeset
for help on using the changeset viewer.