Changeset 82374b2 in mainline


Ignore:
Timestamp:
2011-06-28T07:32:48Z (13 years ago)
Author:
Oleg Romanenko <romanenko.oleg@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
fcc3cd8
Parents:
298a6ce
Message:

Add utf16_to_str function doing conversion between UTF16 string and UTF8
with surrogate pairs support

Location:
uspace/lib/c
Files:
2 edited

Legend:

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

    r298a6ce r82374b2  
    587587        }
    588588
     589        dest[dest_off] = '\0';
     590        return rc;
     591}
     592
     593/** Convert UTF16 string to string.
     594 *
     595 * Convert utf16 string @a src to string. The output is written to the buffer
     596 * specified by @a dest and @a size. @a size must be non-zero and the string
     597 * written will always be well-formed. Surrogate pairs also supported.
     598 *
     599 * @param dest  Destination buffer.
     600 * @param size  Size of the destination buffer.
     601 * @param src   Source utf16 string.
     602 *
     603 * @return EOK, if success, negative otherwise.
     604 */
     605int utf16_to_str(char *dest, size_t size, const uint16_t *src)
     606{
     607        size_t idx=0, dest_off=0;
     608        wchar_t ch;
     609        int rc = EOK;
     610
     611        /* There must be space for a null terminator in the buffer. */
     612        assert(size > 0);
     613
     614        while (src[idx]) {
     615                if ((src[idx] & 0xfc00) == 0xd800) {
     616                        if (src[idx+1] && (src[idx+1] & 0xfc00) == 0xdc00) {
     617                                ch = 0x10000;
     618                                ch += (src[idx] & 0x03FF) << 10;
     619                                ch += (src[idx+1] & 0x03FF);
     620                                idx += 2;
     621                        }
     622                        else
     623                                break;
     624                } else {
     625                        ch = src[idx];
     626                        idx++;
     627                }
     628                rc = chr_encode(ch, dest, &dest_off, size-1);
     629                if (rc != EOK)
     630                        break;
     631        }
    589632        dest[dest_off] = '\0';
    590633        return rc;
  • uspace/lib/c/include/str.h

    r298a6ce r82374b2  
    7878extern char *wstr_to_astr(const wchar_t *src);
    7979extern int str_to_wstr(wchar_t *dest, size_t dlen, const char *src);
     80extern int utf16_to_str(char *dest, size_t size, const uint16_t *src);
    8081
    8182extern char *str_chr(const char *str, wchar_t ch);
Note: See TracChangeset for help on using the changeset viewer.