Changeset 375ab5e in mainline for uspace/lib/c/generic/str.c


Ignore:
Timestamp:
2011-08-24T20:10:43Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
eb660787
Parents:
7fadb65 (diff), 842a2d2 (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 from lp:~romanenko-oleg/helenos/fat.

File:
1 edited

Legend:

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

    r7fadb65 r375ab5e  
    33 * Copyright (c) 2008 Jiri Svoboda
    44 * Copyright (c) 2011 Martin Sucha
     5 * Copyright (c) 2011 Oleg Romanenko
    56 * All rights reserved.
    67 *
     
    367368}
    368369
     370/** Check whether wide string is plain ASCII.
     371 *
     372 * @return True if wide string is plain ASCII.
     373 *
     374 */
     375bool wstr_is_ascii(const wchar_t *wstr)
     376{
     377        while (*wstr && ascii_check(*wstr))
     378                wstr++;
     379        return *wstr == 0;
     380}
     381
    369382/** Check whether character is valid
    370383 *
     
    619632 * @param size  Size of the destination buffer.
    620633 * @param src   Source wide string.
    621  */
    622 void wstr_to_str(char *dest, size_t size, const wchar_t *src)
    623 {
     634 *
     635 * @return EOK, if success, negative otherwise.
     636 */
     637int wstr_to_str(char *dest, size_t size, const wchar_t *src)
     638{
     639        int rc;
    624640        wchar_t ch;
    625641        size_t src_idx;
     
    633649
    634650        while ((ch = src[src_idx++]) != 0) {
    635                 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
     651                rc = chr_encode(ch, dest, &dest_off, size - 1);
     652                if (rc != EOK)
    636653                        break;
    637654        }
    638655
    639656        dest[dest_off] = '\0';
    640 }
     657        return rc;
     658}
     659
     660/** Convert UTF16 string to string.
     661 *
     662 * Convert utf16 string @a src to string. The output is written to the buffer
     663 * specified by @a dest and @a size. @a size must be non-zero and the string
     664 * written will always be well-formed. Surrogate pairs also supported.
     665 *
     666 * @param dest  Destination buffer.
     667 * @param size  Size of the destination buffer.
     668 * @param src   Source utf16 string.
     669 *
     670 * @return EOK, if success, negative otherwise.
     671 */
     672int utf16_to_str(char *dest, size_t size, const uint16_t *src)
     673{
     674        size_t idx=0, dest_off=0;
     675        wchar_t ch;
     676        int rc = EOK;
     677
     678        /* There must be space for a null terminator in the buffer. */
     679        assert(size > 0);
     680
     681        while (src[idx]) {
     682                if ((src[idx] & 0xfc00) == 0xd800) {
     683                        if (src[idx+1] && (src[idx+1] & 0xfc00) == 0xdc00) {
     684                                ch = 0x10000;
     685                                ch += (src[idx] & 0x03FF) << 10;
     686                                ch += (src[idx+1] & 0x03FF);
     687                                idx += 2;
     688                        }
     689                        else
     690                                break;
     691                } else {
     692                        ch = src[idx];
     693                        idx++;
     694                }
     695                rc = chr_encode(ch, dest, &dest_off, size-1);
     696                if (rc != EOK)
     697                        break;
     698        }
     699        dest[dest_off] = '\0';
     700        return rc;
     701}
     702
     703int str_to_utf16(uint16_t *dest, size_t size, const char *src)
     704{
     705        int rc=EOK;
     706        size_t offset=0;
     707        size_t idx=0;
     708        wchar_t c;
     709
     710        assert(size > 0);
     711       
     712        while ((c = str_decode(src, &offset, STR_NO_LIMIT)) != 0) {
     713                if (c > 0x10000) {
     714                        if (idx+2 >= size-1) {
     715                                rc=EOVERFLOW;
     716                                break;
     717                        }
     718                        c = (c - 0x10000);
     719                        dest[idx] = 0xD800 | (c >> 10);
     720                        dest[idx+1] = 0xDC00 | (c & 0x3FF);
     721                        idx++;
     722                } else {
     723                         dest[idx] = c;
     724                }
     725
     726                idx++;
     727                if (idx >= size-1) {
     728                        rc=EOVERFLOW;
     729                        break;
     730                }
     731        }
     732
     733        dest[idx] = '\0';
     734        return rc;
     735}
     736
    641737
    642738/** Convert wide string to new string.
     
    698794 * @param dlen  Length of destination buffer (number of wchars).
    699795 * @param src   Source string.
    700  */
    701 void str_to_wstr(wchar_t *dest, size_t dlen, const char *src)
    702 {
     796 *
     797 * @return EOK, if success, negative otherwise.
     798 */
     799int str_to_wstr(wchar_t *dest, size_t dlen, const char *src)
     800{
     801        int rc=EOK;
    703802        size_t offset;
    704803        size_t di;
     
    711810
    712811        do {
    713                 if (di >= dlen - 1)
     812                if (di >= dlen - 1) {
     813                        rc = EOVERFLOW;
    714814                        break;
     815                }
    715816
    716817                c = str_decode(src, &offset, STR_NO_LIMIT);
     
    719820
    720821        dest[dlen - 1] = '\0';
     822        return rc;
    721823}
    722824
     
    783885       
    784886        return (char *) res;
     887}
     888
     889/** Find first occurence of character in wide string.
     890 *
     891 * @param wstr String to search.
     892 * @param ch  Character to look for.
     893 *
     894 * @return Pointer to character in @a wstr or NULL if not found.
     895 */
     896wchar_t *wstr_chr(const wchar_t *wstr, wchar_t ch)
     897{
     898        while (*wstr && *wstr != ch)
     899                wstr++;
     900        if (*wstr)
     901                return (wchar_t *) wstr;
     902        else
     903                return NULL;
     904}
     905
     906/** Find last occurence of character in wide string.
     907 *
     908 * @param wstr String to search.
     909 * @param ch  Character to look for.
     910 *
     911 * @return Pointer to character in @a wstr or NULL if not found.
     912 */
     913wchar_t *wstr_rchr(const wchar_t *wstr, wchar_t ch)
     914{
     915        const wchar_t *res = NULL;
     916        while (*wstr) {
     917                if (*wstr == ch)
     918                        res = wstr;
     919                wstr++;
     920        }
     921        return (wchar_t *) res;
    785922}
    786923
     
    10371174}
    10381175
     1176void str_reverse(char* begin, char* end)
     1177{
     1178    char aux;
     1179    while(end>begin)
     1180        aux=*end, *end--=*begin, *begin++=aux;
     1181}
     1182
     1183int size_t_str(size_t value, int base, char* str, size_t size)
     1184{
     1185    static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
     1186    char* wstr=str;
     1187       
     1188        if (size == 0)
     1189                return EINVAL;
     1190    if (base<2 || base>35) {
     1191        *str='\0';
     1192        return EINVAL;
     1193    }
     1194
     1195    do {
     1196        *wstr++ = num[value % base];
     1197                if (--size == 0)
     1198                        return EOVERFLOW;
     1199    } while(value /= base);
     1200    *wstr='\0';
     1201
     1202    // Reverse string
     1203    str_reverse(str,wstr-1);
     1204        return EOK;
     1205}
    10391206
    10401207/** Convert initial part of string to unsigned long according to given base.
Note: See TracChangeset for help on using the changeset viewer.