Changeset e1813cf in mainline


Ignore:
Timestamp:
2009-03-31T22:51:41Z (15 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ce87a8aa
Parents:
b54d2f1
Message:

Start converting string functions according to the terminology agreed upon.

Location:
kernel/generic
Files:
5 edited

Legend:

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

    rb54d2f1 re1813cf  
    3838#include <typedefs.h>
    3939
    40 #define UTF8_NO_LIMIT  ((index_t) -1)
     40#define UTF8_NO_LIMIT  ((size_t) -1)
    4141
    4242extern char invalch;
    4343
    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);
     44extern wchar_t chr_decode(const char *, size_t *, size_t);
     45extern bool chr_encode(const wchar_t, char *, size_t *, size_t limit);
    4646extern size_t utf8_count_bytes(const char *str, count_t count);
    4747extern bool ascii_check(const wchar_t ch);
  • kernel/generic/src/lib/string.c

    rb54d2f1 re1813cf  
    5757#define CONT_BITS 6
    5858
    59 /** Decode a single UTF-8 character from a NULL-terminated string.
    60  *
    61  * Decode a single UTF-8 character from a plain char NULL-terminated
    62  * string. Decoding starts at @index and this index is moved to the
    63  * 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).
    6767 * @param index Index (counted in plain characters) where to start
    6868 *              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 */
     74wchar_t chr_decode(const char *str, size_t *offset, size_t sz)
    7575{
    7676        uint8_t b0, b;          /* Bytes read from str. */
     
    8080        int cbytes;             /* Number of continuation bytes. */
    8181
    82         if (*index + 1 > limit)
     82        if (*offset + 1 > sz)
    8383                return invalch;
    8484
    85         b0 = (uint8_t) str[(*index)++];
     85        b0 = (uint8_t) str[(*offset)++];
    8686
    8787        /* Determine code length. */
     
    108108        }
    109109
    110         if (*index + cbytes > limit) {
     110        if (*offset + cbytes > sz) {
    111111                return invalch;
    112112        }
     
    116116        /* Decode continuation bytes. */
    117117        while (cbytes > 0) {
    118                 b = (uint8_t) str[(*index)++];
     118                b = (uint8_t) str[(*offset)++];
    119119
    120120                /* Must be 10xxxxxx. */
     
    131131}
    132132
    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 */
     148bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz)
    152149{
    153150        uint32_t cc;            /* Unsigned version of ch. */
     
    157154        int i;
    158155
    159         if (*index >= limit)
     156        if (*offset >= sz)
    160157                return false;
    161158
     
    185182
    186183        /* Check for available space in buffer. */
    187         if (*index + cbytes >= limit)
     184        if (*offset + cbytes >= sz)
    188185                return false;
    189186
    190187        /* Encode continuation bytes. */
    191188        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));
    193190                cc = cc >> CONT_BITS;
    194191        }
    195192
    196193        /* 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);
    201198       
    202199        return true;
     
    227224                if (size >= count)
    228225                        break;
    229                 ch = utf8_decode(str, &index, UTF8_NO_LIMIT);
     226                ch = chr_decode(str, &index, UTF8_NO_LIMIT);
    230227                if (ch == '\0') break;
    231228
     
    289286        index_t index = 0;
    290287       
    291         while (utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) {
     288        while (chr_decode(str, &index, UTF8_NO_LIMIT) != 0) {
    292289                size++;
    293290        }
  • kernel/generic/src/printf/printf_core.c

    rb54d2f1 re1813cf  
    595595        while (true) {
    596596                i = nxt;
    597                 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     597                uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    598598
    599599                if (uc == '\0') break;
     
    619619                        do {
    620620                                i = nxt;
    621                                 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     621                                uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    622622                                switch (uc) {
    623623                                case '#':
     
    649649
    650650                                        i = nxt;
    651                                         uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     651                                        uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    652652                                        if (uc == '\0')
    653653                                                break;
     
    658658                                /* Get width value from argument list */
    659659                                i = nxt;
    660                                 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     660                                uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    661661                                width = (int) va_arg(ap, int);
    662662                                if (width < 0) {
     
    671671                        if (uc == '.') {
    672672                                i = nxt;
    673                                 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     673                                uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    674674                                if (isdigit(uc)) {
    675675                                        while (true) {
     
    678678
    679679                                                i = nxt;
    680                                                 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     680                                                uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    681681                                                if (uc == '\0')
    682682                                                        break;
     
    687687                                        /* Get precision value from the argument list */
    688688                                        i = nxt;
    689                                         uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     689                                        uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    690690                                        precision = (int) va_arg(ap, int);
    691691                                        if (precision < 0) {
     
    706706                                qualifier = PrintfQualifierShort;
    707707                                i = nxt;
    708                                 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     708                                uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    709709                                if (uc == 'h') {
    710710                                        i = nxt;
    711                                         uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     711                                        uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    712712                                        qualifier = PrintfQualifierByte;
    713713                                }
     
    717717                                qualifier = PrintfQualifierLong;
    718718                                i = nxt;
    719                                 uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     719                                uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    720720                                if (uc == 'l') {
    721721                                        i = nxt;
    722                                         uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     722                                        uc = chr_decode(fmt, &nxt, UTF8_NO_LIMIT);
    723723                                        qualifier = PrintfQualifierLongLong;
    724724                                }
  • kernel/generic/src/printf/vprintf.c

    rb54d2f1 re1813cf  
    5050       
    5151        while (index < size) {
    52                 putchar(utf8_decode(str, &index, size));
     52                putchar(chr_decode(str, &index, size));
    5353                chars++;
    5454        }
     
    7575        wchar_t uc;
    7676       
    77         while ((uc = utf8_decode(str, &index, UTF8_NO_LIMIT)) != 0) {
     77        while ((uc = chr_decode(str, &index, UTF8_NO_LIMIT)) != 0) {
    7878                putchar(uc);
    7979                chars++;
  • kernel/generic/src/printf/vsnprintf.c

    rb54d2f1 re1813cf  
    8585               
    8686                while (index < size) {
    87                         wchar_t uc = utf8_decode(str, &index, size);
     87                        wchar_t uc = chr_decode(str, &index, size);
    8888
    89                         if (!utf8_encode(uc, data->dst, &data->len, data->size - 1))
     89                        if (!chr_encode(uc, data->dst, &data->len, data->size - 1))
    9090                                break;
    9191                }
     
    147147                }
    148148               
    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))
    150150                        break;
    151151               
Note: See TracChangeset for help on using the changeset viewer.