Changeset b54d2f1 in mainline


Ignore:
Timestamp:
2009-03-31T22:11:11Z (15 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.

Location:
kernel/generic/src
Files:
4 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       
  • kernel/generic/src/printf/printf_core.c

    r32704cb rb54d2f1  
    258258        if (precision == 0)
    259259                precision = size;
    260        
     260
    261261        count_t counter = 0;
    262262        width -= precision;
     
    267267                }
    268268        }
    269        
     269
    270270        int retval;
    271271        size_t bytes = utf8_count_bytes(str, min(size, precision));
     
    279279                        counter++;
    280280        }
    281        
     281
    282282        return ((int) counter);
    283283}
     
    586586{
    587587        index_t i = 0;  /* Index of the currently processed character from fmt */
     588        index_t nxt = 0;
    588589        index_t j = 0;  /* Index to the first not printed nonformating character */
    589590       
     
    592593        int retval;           /* Return values from nested functions */
    593594       
    594         while ((uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT)) != 0) {
     595        while (true) {
     596                i = nxt;
     597                uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     598
     599                if (uc == '\0') break;
     600
    595601                /* Control character */
    596602                if (uc == '%') {
     
    612618                       
    613619                        do {
    614                                 i++;
    615                                 uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT);
     620                                i = nxt;
     621                                uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
    616622                                switch (uc) {
    617623                                case '#':
     
    638644                        int width = 0;
    639645                        if (isdigit(uc)) {
    640                                 while ((uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT)) != 0) {
     646                                while (true) {
     647                                        width *= 10;
     648                                        width += uc - '0';
     649
     650                                        i = nxt;
     651                                        uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     652                                        if (uc == '\0')
     653                                                break;
    641654                                        if (!isdigit(uc))
    642655                                                break;
    643                                        
    644                                         width *= 10;
    645                                         width += uc - '0';
    646                                         i++;
    647656                                }
    648657                        } else if (uc == '*') {
    649658                                /* Get width value from argument list */
    650                                 i++;
    651                                 uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT);
     659                                i = nxt;
     660                                uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
    652661                                width = (int) va_arg(ap, int);
    653662                                if (width < 0) {
     
    661670                        int precision = 0;
    662671                        if (uc == '.') {
    663                                 i++;
    664                                 uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT);
     672                                i = nxt;
     673                                uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
    665674                                if (isdigit(uc)) {
    666                                         while ((uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT)) != 0) {
     675                                        while (true) {
     676                                                precision *= 10;
     677                                                precision += uc - '0';
     678
     679                                                i = nxt;
     680                                                uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
     681                                                if (uc == '\0')
     682                                                        break;
    667683                                                if (!isdigit(uc))
    668684                                                        break;
    669                                                
    670                                                 precision *= 10;
    671                                                 precision += uc - '0';
    672                                                 i++;
    673685                                        }
    674                                 } else if (fmt[i] == '*') {
     686                                } else if (uc == '*') {
    675687                                        /* Get precision value from the argument list */
    676                                         i++;
    677                                         uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT);
     688                                        i = nxt;
     689                                        uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
    678690                                        precision = (int) va_arg(ap, int);
    679691                                        if (precision < 0) {
     
    693705                                /* Char or short */
    694706                                qualifier = PrintfQualifierShort;
    695                                 i++;
    696                                 uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT);
     707                                i = nxt;
     708                                uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
    697709                                if (uc == 'h') {
    698                                         i++;
    699                                         uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT);
     710                                        i = nxt;
     711                                        uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
    700712                                        qualifier = PrintfQualifierByte;
    701713                                }
     
    704716                                /* Long or long long */
    705717                                qualifier = PrintfQualifierLong;
    706                                 i++;
    707                                 uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT);
     718                                i = nxt;
     719                                uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
    708720                                if (uc == 'l') {
    709                                         i++;
    710                                         uc = utf8_decode(fmt, &i, UTF8_NO_LIMIT);
     721                                        i = nxt;
     722                                        uc = utf8_decode(fmt, &nxt, UTF8_NO_LIMIT);
    711723                                        qualifier = PrintfQualifierLongLong;
    712724                                }
     
    735747                               
    736748                                counter += retval;
    737                                 j = i + 1;
     749                                j = nxt;
    738750                                goto next_char;
    739751                        case 'c':
     
    749761                               
    750762                                counter += retval;
    751                                 j = i + 1;
     763                                j = nxt;
    752764                                goto next_char;
    753765                       
     
    853865                       
    854866                        counter += retval;
    855                         j = i + 1;
     867                        j = nxt;
    856868                }
    857869next_char:
    858                
    859                 i++;
     870                ;
    860871        }
    861872       
  • kernel/generic/src/printf/vprintf.c

    r32704cb rb54d2f1  
    5050       
    5151        while (index < size) {
    52                 putchar(utf8_decode(str, &index, size - 1));
    53                 index++;
     52                putchar(utf8_decode(str, &index, size));
    5453                chars++;
    5554        }
     
    7877        while ((uc = utf8_decode(str, &index, UTF8_NO_LIMIT)) != 0) {
    7978                putchar(uc);
    80                 index++;
    8179                chars++;
    8280        }
  • kernel/generic/src/printf/vsnprintf.c

    r32704cb rb54d2f1  
    8585               
    8686                while (index < size) {
    87                         wchar_t uc = utf8_decode(str, &index, size - 1);
    88                        
    89                         if (!utf8_encode(uc, data->dst, &data->len, data->size - 2))
     87                        wchar_t uc = utf8_decode(str, &index, size);
     88
     89                        if (!utf8_encode(uc, data->dst, &data->len, data->size - 1))
    9090                                break;
    91                        
    92                         data->len++;
    93                         index++;
    9491                }
    9592               
     
    150147                }
    151148               
    152                 if (!utf8_encode(str[index], data->dst, &data->len, data->size - 2))
     149                if (!utf8_encode(str[index], data->dst, &data->len, data->size - 1))
    153150                        break;
    154151               
    155                 data->len++;
    156152                index++;
    157153        }
Note: See TracChangeset for help on using the changeset viewer.