Changeset 82bb9c1 in mainline


Ignore:
Timestamp:
2009-04-02T20:38:51Z (16 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
58d5a7e7
Parents:
d09f84e6
Message:

Consider character display width somewhat. Explain naming scheme.

Location:
kernel/generic
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/string.h

    rd09f84e6 r82bb9c1  
    4343
    4444extern wchar_t chr_decode(const char *, size_t *, size_t);
    45 extern int chr_encode(const wchar_t, char *, size_t *, size_t);
     45extern int chr_encode(wchar_t, char *, size_t *, size_t);
     46extern count_t chr_width(wchar_t);
    4647
    47 extern size_t str_size(const char *str);
     48extern size_t str_size(const char *);
    4849extern 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);
     50extern size_t str_wsize(const char *, count_t);
     51
     52extern count_t str_length(const char *);
     53extern count_t wstr_length(const wchar_t *);
     54extern count_t wstr_wlength(const wchar_t *, count_t);
    5155
    5256extern bool ascii_check(const wchar_t ch);
  • kernel/generic/src/lib/string.c

    rd09f84e6 r82bb9c1  
    3333/**
    3434 * @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.
    3661 */
    3762
     
    146171 *         code was invalid.
    147172 */
    148 int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz)
     173int chr_encode(wchar_t ch, char *str, size_t *offset, size_t sz)
    149174{
    150175        uint32_t cc;            /* Unsigned version of ch. */
     
    200225}
    201226
     227/** Get display width of character.
     228 *
     229 * @param ch    The character.
     230 * @return      Character width in display cells.
     231 */
     232count_t chr_width(wchar_t ch)
     233{
     234        return 1;
     235}
     236
    202237/** Get size of string, with length limit.
    203238 *
     
    206241 * the length of @a str, the entire string is measured.
    207242 *
    208  * @param str   String to consider.
    209  * @param count Maximum number of characters to measure.
    210  *
    211  * @return Number of bytes used by the characters.
     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.
    212247 */
    213248size_t str_lsize(const char *str, count_t max_len)
     
    231266}
    232267
     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 */
     278size_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 */
     309count_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
    233326/** Check whether character is plain ASCII.
    234327 *
     
    291384/** Return number of characters in a wide string.
    292385 *
    293  * @param str NULL-terminated wide string.
    294  * @return Number of characters in @a str.
     386 * @param       str NULL-terminated wide string.
     387 * @return      Number of characters in @a str.
    295388 */
    296389count_t wstr_length(const wchar_t *wstr)
  • kernel/generic/src/printf/printf_core.c

    rd09f84e6 r82bb9c1  
    269269
    270270        int retval;
    271         size_t size = str_lsize(str, precision);
     271        size_t size = str_wsize(str, precision);
    272272        if ((retval = printf_putnchars_utf8(str, size, ps)) < 0)
    273273                return -counter;
     
    314314
    315315        int retval;
    316         size_t size = min(strw, precision) * sizeof(wchar_t);
     316        size_t size = wstr_wlength(wstr, precision) * sizeof(wchar_t);
    317317        if ((retval = printf_putnchars_utf32(wstr, size, ps)) < 0)
    318318                return -counter;
Note: See TracChangeset for help on using the changeset viewer.