Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset f25b2819 in mainline


Ignore:
Timestamp:
2009-04-01T18:24:31Z (13 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master
Children:
1b0b48e0
Parents:
b4c4666
Message:

str_lsize(), str_length(), wstr_length().

Location:
kernel/generic
Files:
3 edited

Legend:

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

    rb4c4666 rf25b2819  
    4343
    4444extern wchar_t chr_decode(const char *, size_t *, size_t);
    45 extern bool chr_encode(const wchar_t, char *, size_t *, size_t limit);
    46 extern size_t utf8_count_bytes(const char *str, count_t count);
     45extern bool chr_encode(const wchar_t, char *, size_t *, size_t);
     46extern size_t str_lsize(const char *, count_t);
    4747extern bool ascii_check(const wchar_t ch);
    4848extern bool unicode_check(const wchar_t ch);
    4949
    5050extern size_t strlen(const char *str);
    51 extern size_t strlen_utf8(const char *str);
    52 extern size_t strlen_utf32(const wchar_t *str);
     51extern count_t str_length(const char *str);
     52extern count_t wstr_length(const wchar_t *str);
    5353
    5454extern int strcmp(const char *src, const char *dst);
  • kernel/generic/src/lib/string.c

    rb4c4666 rf25b2819  
    200200}
    201201
    202 /** Get bytes used by UTF-8 characters.
    203  *
    204  * Get the number of bytes (count of plain characters) which
    205  * are used by a given count of UTF-8 characters in a string.
    206  * As UTF-8 encoding is multibyte, there is no constant
    207  * correspondence between number of characters and used bytes.
    208  *
    209  * @param str   UTF-8 string to consider.
    210  * @param count Number of UTF-8 characters to count.
     202/** Get size of string, with length limit.
     203 *
     204 * Get the number of bytes which are used by up to @a max_len first
     205 * characters in the string @a str. If @a max_len is greater than
     206 * the length of @a str, the entire string is measured.
     207 *
     208 * @param str   String to consider.
     209 * @param count Maximum number of characters to measure.
    211210 *
    212211 * @return Number of bytes used by the characters.
    213  *
    214  */
    215 size_t utf8_count_bytes(const char *str, count_t count)
    216 {
    217         size_t size = 0;
    218         index_t index = 0;
    219         index_t iprev;
     212 */
     213size_t str_lsize(const char *str, count_t max_len)
     214{
     215        count_t len = 0;
     216        size_t cur = 0;
     217        size_t prev;
    220218        wchar_t ch;
    221        
     219
    222220        while (true) {
    223                 iprev = index;
    224                 if (size >= count)
     221                prev = cur;
     222                if (len >= max_len)
    225223                        break;
    226                 ch = chr_decode(str, &index, UTF8_NO_LIMIT);
     224                ch = chr_decode(str, &cur, UTF8_NO_LIMIT);
    227225                if (ch == '\0') break;
    228226
    229                 size++;
    230         }
    231        
    232         return iprev;
     227                len++;
     228        }
     229
     230        return prev;
    233231}
    234232
     
    263261 * @param str NULL-terminated string.
    264262 *
    265  * @return Number of characters in str.
     263 * @return Number of characters in @a str.
    266264 *
    267265 */
     
    274272}
    275273
    276 /** Return number of UTF-8 characters in a string.
    277  *
    278  * @param str NULL-terminated UTF-8 string.
    279  *
    280  * @return Number of UTF-8 characters in str.
    281  *
    282  */
    283 size_t strlen_utf8(const char *str)
    284 {
    285         size_t size = 0;
    286         index_t index = 0;
    287        
    288         while (chr_decode(str, &index, UTF8_NO_LIMIT) != 0) {
    289                 size++;
    290         }
    291        
    292         return size;
    293 }
    294 
    295 /** Return number of UTF-32 characters in a string.
    296  *
    297  * @param str NULL-terminated UTF-32 string.
    298  *
    299  * @return Number of UTF-32 characters in str.
    300  *
    301  */
    302 size_t strlen_utf32(const wchar_t *str)
    303 {
    304         size_t size;
    305         for (size = 0; str[size]; size++);
    306        
    307         return size;
     274/** Return number of characters in a string.
     275 *
     276 * @param str NULL-terminated string.
     277 * @return Number of characters in string.
     278 */
     279count_t str_length(const char *str)
     280{
     281        count_t len = 0;
     282        size_t offset = 0;
     283
     284        while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) {
     285                len++;
     286        }
     287
     288        return len;
     289}
     290
     291/** Return number of characters in a wide string.
     292 *
     293 * @param str NULL-terminated wide string.
     294 * @return Number of characters in @a str.
     295 */
     296count_t wstr_length(const wchar_t *wstr)
     297{
     298        count_t len;
     299
     300        len = 0;
     301        while (*wstr++ != '\0')
     302                ++len;
     303
     304        return len;
    308305}
    309306
  • kernel/generic/src/printf/printf_core.c

    rb4c4666 rf25b2819  
    255255       
    256256        /* Print leading spaces */
    257         size_t size = strlen_utf8(str);
     257        size_t size = str_length(str);
    258258        if (precision == 0)
    259259                precision = size;
     
    269269
    270270        int retval;
    271         size_t bytes = utf8_count_bytes(str, min(size, precision));
     271        size_t bytes = str_lsize(str, min(size, precision));
    272272        if ((retval = printf_putnchars_utf8(str, bytes, ps)) < 0)
    273273                return -counter;
     
    299299       
    300300        /* Print leading spaces */
    301         size_t size = strlen_utf32(str);
     301        size_t size = wstr_length(str);
    302302        if (precision == 0)
    303303                precision = size;
Note: See TracChangeset for help on using the changeset viewer.