Changeset b54d2f1 in mainline for kernel/generic/src/lib/string.c


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

Semantics for 'index' parameter of utf8_encode/decode() should be more logical.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/lib/string.c

    r32704cb rb54d2f1  
    6060 *
    6161 * Decode a single UTF-8 character from a plain char NULL-terminated
    62  * string. Decoding starts at @index and this index is incremented
    63  * if the current UTF-8 string is encoded in more than a single byte.
     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).
    6465 *
    6566 * @param str   Plain character NULL-terminated string.
     
    7980        int cbytes;             /* Number of continuation bytes. */
    8081
    81         if (*index > limit)
     82        if (*index + 1 > limit)
    8283                return invalch;
    8384
    84         b0 = (uint8_t) str[*index];
     85        b0 = (uint8_t) str[(*index)++];
    8586
    8687        /* Determine code length. */
     
    115116        /* Decode continuation bytes. */
    116117        while (cbytes > 0) {
    117                 b = (uint8_t) str[*index + 1];
    118                 ++(*index);
     118                b = (uint8_t) str[(*index)++];
    119119
    120120                /* Must be 10xxxxxx. */
     
    135135 * Encode a single UTF-32 character as UTF-8 and store it into
    136136 * the given buffer at @index. Encoding starts at @index and
    137  * this index is incremented if the UTF-8 character takes
    138  * more than a single byte.
     137 * this index is moved at the position where the next character
     138 * can be written to.
    139139 *
    140140 * @param ch    Input UTF-32 character.
     
    157157        int i;
    158158
    159         if (*index > limit)
     159        if (*index >= limit)
    160160                return false;
    161161
     
    185185
    186186        /* Check for available space in buffer. */
    187         if (*index + cbytes > limit)
     187        if (*index + cbytes >= limit)
    188188                return false;
    189189
     
    198198
    199199        /* Advance index. */
    200         *index += cbytes;
     200        *index += (1 + cbytes);
    201201       
    202202        return true;
     
    220220        size_t size = 0;
    221221        index_t index = 0;
    222        
    223         while ((utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) && (size < count)) {
     222        index_t iprev;
     223        wchar_t ch;
     224       
     225        while (true) {
     226                iprev = index;
     227                if (size >= count)
     228                        break;
     229                ch = utf8_decode(str, &index, UTF8_NO_LIMIT);
     230                if (ch == '\0') break;
     231
    224232                size++;
    225                 index++;
    226         }
    227        
    228         return index;
     233        }
     234       
     235        return iprev;
    229236}
    230237
     
    284291        while (utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) {
    285292                size++;
    286                 index++;
    287293        }
    288294       
Note: See TracChangeset for help on using the changeset viewer.