michael@0: /* michael@0: * Copyright (C) 2005 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef ANDROID_UNICODE_H michael@0: #define ANDROID_UNICODE_H michael@0: michael@0: #include michael@0: #include michael@0: michael@0: extern "C" { michael@0: michael@0: #if !defined(__cplusplus) || __cplusplus == 199711L // C or C++98 michael@0: typedef uint32_t char32_t; michael@0: typedef uint16_t char16_t; michael@0: #endif michael@0: michael@0: // Standard string functions on char16_t strings. michael@0: int strcmp16(const char16_t *, const char16_t *); michael@0: int strncmp16(const char16_t *s1, const char16_t *s2, size_t n); michael@0: size_t strlen16(const char16_t *); michael@0: size_t strnlen16(const char16_t *, size_t); michael@0: char16_t *strcpy16(char16_t *, const char16_t *); michael@0: char16_t *strncpy16(char16_t *, const char16_t *, size_t); michael@0: michael@0: // Version of comparison that supports embedded nulls. michael@0: // This is different than strncmp() because we don't stop michael@0: // at a nul character and consider the strings to be different michael@0: // if the lengths are different (thus we need to supply the michael@0: // lengths of both strings). This can also be used when michael@0: // your string is not nul-terminated as it will have the michael@0: // equivalent result as strcmp16 (unlike strncmp16). michael@0: int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2); michael@0: michael@0: // Version of strzcmp16 for comparing strings in different endianness. michael@0: int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2); michael@0: michael@0: // Standard string functions on char32_t strings. michael@0: size_t strlen32(const char32_t *); michael@0: size_t strnlen32(const char32_t *, size_t); michael@0: michael@0: /** michael@0: * Measure the length of a UTF-32 string in UTF-8. If the string is invalid michael@0: * such as containing a surrogate character, -1 will be returned. michael@0: */ michael@0: ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len); michael@0: michael@0: /** michael@0: * Stores a UTF-8 string converted from "src" in "dst", if "dst_length" is not michael@0: * large enough to store the string, the part of the "src" string is stored michael@0: * into "dst" as much as possible. See the examples for more detail. michael@0: * Returns the size actually used for storing the string. michael@0: * dst" is not null-terminated when dst_len is fully used (like strncpy). michael@0: * michael@0: * Example 1 michael@0: * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) michael@0: * "src_len" == 2 michael@0: * "dst_len" >= 7 michael@0: * -> michael@0: * Returned value == 6 michael@0: * "dst" becomes \xE3\x81\x82\xE3\x81\x84\0 michael@0: * (note that "dst" is null-terminated) michael@0: * michael@0: * Example 2 michael@0: * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) michael@0: * "src_len" == 2 michael@0: * "dst_len" == 5 michael@0: * -> michael@0: * Returned value == 3 michael@0: * "dst" becomes \xE3\x81\x82\0 michael@0: * (note that "dst" is null-terminated, but \u3044 is not stored in "dst" michael@0: * since "dst" does not have enough size to store the character) michael@0: * michael@0: * Example 3 michael@0: * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) michael@0: * "src_len" == 2 michael@0: * "dst_len" == 6 michael@0: * -> michael@0: * Returned value == 6 michael@0: * "dst" becomes \xE3\x81\x82\xE3\x81\x84 michael@0: * (note that "dst" is NOT null-terminated, like strncpy) michael@0: */ michael@0: void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst); michael@0: michael@0: /** michael@0: * Returns the unicode value at "index". michael@0: * Returns -1 when the index is invalid (equals to or more than "src_len"). michael@0: * If returned value is positive, it is able to be converted to char32_t, which michael@0: * is unsigned. Then, if "next_index" is not NULL, the next index to be used is michael@0: * stored in "next_index". "next_index" can be NULL. michael@0: */ michael@0: int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index); michael@0: michael@0: michael@0: /** michael@0: * Returns the UTF-8 length of UTF-16 string "src". michael@0: */ michael@0: ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len); michael@0: michael@0: /** michael@0: * Converts a UTF-16 string to UTF-8. The destination buffer must be large michael@0: * enough to fit the UTF-16 as measured by utf16_to_utf8_length with an added michael@0: * NULL terminator. michael@0: */ michael@0: void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst); michael@0: michael@0: /** michael@0: * Returns the length of "src" when "src" is valid UTF-8 string. michael@0: * Returns 0 if src is NULL or 0-length string. Returns -1 when the source michael@0: * is an invalid string. michael@0: * michael@0: * This function should be used to determine whether "src" is valid UTF-8 michael@0: * characters with valid unicode codepoints. "src" must be null-terminated. michael@0: * michael@0: * If you are going to use other utf8_to_... functions defined in this header michael@0: * with string which may not be valid UTF-8 with valid codepoint (form 0 to michael@0: * 0x10FFFF), you should use this function before calling others, since the michael@0: * other functions do not check whether the string is valid UTF-8 or not. michael@0: * michael@0: * If you do not care whether "src" is valid UTF-8 or not, you should use michael@0: * strlen() as usual, which should be much faster. michael@0: */ michael@0: ssize_t utf8_length(const char *src); michael@0: michael@0: /** michael@0: * Measure the length of a UTF-32 string. michael@0: */ michael@0: size_t utf8_to_utf32_length(const char *src, size_t src_len); michael@0: michael@0: /** michael@0: * Stores a UTF-32 string converted from "src" in "dst". "dst" must be large michael@0: * enough to store the entire converted string as measured by michael@0: * utf8_to_utf32_length plus space for a NULL terminator. michael@0: */ michael@0: void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst); michael@0: michael@0: /** michael@0: * Returns the UTF-16 length of UTF-8 string "src". michael@0: */ michael@0: ssize_t utf8_to_utf16_length(const uint8_t* src, size_t srcLen); michael@0: michael@0: /** michael@0: * Convert UTF-8 to UTF-16 including surrogate pairs. michael@0: * Returns a pointer to the end of the string (where a null terminator might go michael@0: * if you wanted to add one). michael@0: */ michael@0: char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* src, size_t srcLen, char16_t* dst); michael@0: michael@0: /** michael@0: * Convert UTF-8 to UTF-16 including surrogate pairs. The destination buffer michael@0: * must be large enough to hold the result as measured by utf8_to_utf16_length michael@0: * plus an added NULL terminator. michael@0: */ michael@0: void utf8_to_utf16(const uint8_t* src, size_t srcLen, char16_t* dst); michael@0: michael@0: } michael@0: michael@0: #endif