Changeset beb9336 in mainline for uspace/lib/c/generic/str.c


Ignore:
Timestamp:
2012-08-24T14:07:52Z (13 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
041ab64
Parents:
bd29f9c9 (diff), db81577 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge with mainline

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/str.c

    rbd29f9c9 rbeb9336  
    136136}
    137137
     138/** Decode a single character from a string to the left.
     139 *
     140 * Decode a single character from a string of size @a size. Decoding starts
     141 * at @a offset and this offset is moved to the beginning of the previous
     142 * character. In case of decoding error, offset generally decreases at least
     143 * by one. However, offset is never moved before 0.
     144 *
     145 * @param str    String (not necessarily NULL-terminated).
     146 * @param offset Byte offset in string where to start decoding.
     147 * @param size   Size of the string (in bytes).
     148 *
     149 * @return Value of decoded character, U_SPECIAL on decoding error or
     150 *         NULL if attempt to decode beyond @a start of str.
     151 *
     152 */
     153wchar_t str_decode_reverse(const char *str, size_t *offset, size_t size)
     154{
     155        if (*offset == 0)
     156                return 0;
     157       
     158        size_t processed = 0;
     159        /* Continue while continuation bytes found */
     160        while (*offset > 0 && processed < 4) {
     161                uint8_t b = (uint8_t) str[--(*offset)];
     162               
     163                if (processed == 0 && (b & 0x80) == 0) {
     164                        /* 0xxxxxxx (Plain ASCII) */
     165                        return b & 0x7f;
     166                }
     167                else if ((b & 0xe0) == 0xc0 || (b & 0xf0) == 0xe0 ||
     168                    (b & 0xf8) == 0xf0) {
     169                        /* Start byte */
     170                        size_t start_offset = *offset;
     171                        return str_decode(str, &start_offset, size);
     172                }
     173                else if ((b & 0xc0) != 0x80) {
     174                        /* Not a continuation byte */
     175                        return U_SPECIAL;
     176                }
     177                processed++;
     178        }
     179        /* Too many continuation bytes */
     180        return U_SPECIAL;
     181}
     182
    138183/** Encode a single character to string representation.
    139184 *
     
    399444}
    400445
     446/** Get character display width on a character cell display.
     447 *
     448 * @param ch    Character
     449 * @return      Width of character in cells.
     450 */
     451size_t chr_width(wchar_t ch)
     452{
     453        return 1;
     454}
     455
     456/** Get string display width on a character cell display.
     457 *
     458 * @param str   String
     459 * @return      Width of string in cells.
     460 */
     461size_t str_width(const char *str)
     462{
     463        size_t width = 0;
     464        size_t offset = 0;
     465        wchar_t ch;
     466       
     467        while ((ch = str_decode(str, &offset, STR_NO_LIMIT)) != 0)
     468                width += chr_width(ch);
     469       
     470        return width;
     471}
     472
    401473/** Check whether character is plain ASCII.
    402474 *
     
    431503 * and both strings consist of the same sequence of characters.
    432504 *
    433  * A string is smaller than another string iff it is shorter or
    434  * has a character with lower value at the first position where
    435  * the strings differ.
     505 * A string S1 is less than another string S2 if it has a character with
     506 * lower value at the first character position where the strings differ.
     507 * If the strings differ in length, the shorter one is treated as if
     508 * padded by characters with a value of zero.
    436509 *
    437510 * @param s1 First string to compare.
    438511 * @param s2 Second string to compare.
    439512 *
    440  * @return 0 if the strings are equal, -1 if first is smaller,
    441  *         1 if second smaller.
     513 * @return 0 if the strings are equal, -1 if the first is less than the second,
     514 *         1 if the second is less than the first.
    442515 *
    443516 */
     
    475548 * up to max_len characters.
    476549 *
    477  * A string is smaller than another string iff it is shorter or
    478  * has a character with lower value at the first position where
    479  * the strings differ, considering only first max_len characters.
     550 * A string S1 is less than another string S2 if it has a character with
     551 * lower value at the first character position where the strings differ.
     552 * If the strings differ in length, the shorter one is treated as if
     553 * padded by characters with a value of zero. Only the first max_len
     554 * characters are considered.
    480555 *
    481556 * @param s1      First string to compare.
     
    483558 * @param max_len Maximum number of characters to consider.
    484559 *
    485  * @return 0 if the strings are equal, -1 if first is smaller,
    486  *         1 if second smaller.
     560 * @return 0 if the strings are equal, -1 if the first is less than the second,
     561 *         1 if the second is less than the first.
    487562 *
    488563 */
Note: See TracChangeset for help on using the changeset viewer.