|
1 /* |
|
2 * Copyright (C) 2005 The Android Open Source Project |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifndef ANDROID_UNICODE_H |
|
18 #define ANDROID_UNICODE_H |
|
19 |
|
20 #include <sys/types.h> |
|
21 #include <stdint.h> |
|
22 |
|
23 extern "C" { |
|
24 |
|
25 #if !defined(__cplusplus) || __cplusplus == 199711L // C or C++98 |
|
26 typedef uint32_t char32_t; |
|
27 typedef uint16_t char16_t; |
|
28 #endif |
|
29 |
|
30 // Standard string functions on char16_t strings. |
|
31 int strcmp16(const char16_t *, const char16_t *); |
|
32 int strncmp16(const char16_t *s1, const char16_t *s2, size_t n); |
|
33 size_t strlen16(const char16_t *); |
|
34 size_t strnlen16(const char16_t *, size_t); |
|
35 char16_t *strcpy16(char16_t *, const char16_t *); |
|
36 char16_t *strncpy16(char16_t *, const char16_t *, size_t); |
|
37 |
|
38 // Version of comparison that supports embedded nulls. |
|
39 // This is different than strncmp() because we don't stop |
|
40 // at a nul character and consider the strings to be different |
|
41 // if the lengths are different (thus we need to supply the |
|
42 // lengths of both strings). This can also be used when |
|
43 // your string is not nul-terminated as it will have the |
|
44 // equivalent result as strcmp16 (unlike strncmp16). |
|
45 int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2); |
|
46 |
|
47 // Version of strzcmp16 for comparing strings in different endianness. |
|
48 int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2); |
|
49 |
|
50 // Standard string functions on char32_t strings. |
|
51 size_t strlen32(const char32_t *); |
|
52 size_t strnlen32(const char32_t *, size_t); |
|
53 |
|
54 /** |
|
55 * Measure the length of a UTF-32 string in UTF-8. If the string is invalid |
|
56 * such as containing a surrogate character, -1 will be returned. |
|
57 */ |
|
58 ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len); |
|
59 |
|
60 /** |
|
61 * Stores a UTF-8 string converted from "src" in "dst", if "dst_length" is not |
|
62 * large enough to store the string, the part of the "src" string is stored |
|
63 * into "dst" as much as possible. See the examples for more detail. |
|
64 * Returns the size actually used for storing the string. |
|
65 * dst" is not null-terminated when dst_len is fully used (like strncpy). |
|
66 * |
|
67 * Example 1 |
|
68 * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) |
|
69 * "src_len" == 2 |
|
70 * "dst_len" >= 7 |
|
71 * -> |
|
72 * Returned value == 6 |
|
73 * "dst" becomes \xE3\x81\x82\xE3\x81\x84\0 |
|
74 * (note that "dst" is null-terminated) |
|
75 * |
|
76 * Example 2 |
|
77 * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) |
|
78 * "src_len" == 2 |
|
79 * "dst_len" == 5 |
|
80 * -> |
|
81 * Returned value == 3 |
|
82 * "dst" becomes \xE3\x81\x82\0 |
|
83 * (note that "dst" is null-terminated, but \u3044 is not stored in "dst" |
|
84 * since "dst" does not have enough size to store the character) |
|
85 * |
|
86 * Example 3 |
|
87 * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) |
|
88 * "src_len" == 2 |
|
89 * "dst_len" == 6 |
|
90 * -> |
|
91 * Returned value == 6 |
|
92 * "dst" becomes \xE3\x81\x82\xE3\x81\x84 |
|
93 * (note that "dst" is NOT null-terminated, like strncpy) |
|
94 */ |
|
95 void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst); |
|
96 |
|
97 /** |
|
98 * Returns the unicode value at "index". |
|
99 * Returns -1 when the index is invalid (equals to or more than "src_len"). |
|
100 * If returned value is positive, it is able to be converted to char32_t, which |
|
101 * is unsigned. Then, if "next_index" is not NULL, the next index to be used is |
|
102 * stored in "next_index". "next_index" can be NULL. |
|
103 */ |
|
104 int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index); |
|
105 |
|
106 |
|
107 /** |
|
108 * Returns the UTF-8 length of UTF-16 string "src". |
|
109 */ |
|
110 ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len); |
|
111 |
|
112 /** |
|
113 * Converts a UTF-16 string to UTF-8. The destination buffer must be large |
|
114 * enough to fit the UTF-16 as measured by utf16_to_utf8_length with an added |
|
115 * NULL terminator. |
|
116 */ |
|
117 void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst); |
|
118 |
|
119 /** |
|
120 * Returns the length of "src" when "src" is valid UTF-8 string. |
|
121 * Returns 0 if src is NULL or 0-length string. Returns -1 when the source |
|
122 * is an invalid string. |
|
123 * |
|
124 * This function should be used to determine whether "src" is valid UTF-8 |
|
125 * characters with valid unicode codepoints. "src" must be null-terminated. |
|
126 * |
|
127 * If you are going to use other utf8_to_... functions defined in this header |
|
128 * with string which may not be valid UTF-8 with valid codepoint (form 0 to |
|
129 * 0x10FFFF), you should use this function before calling others, since the |
|
130 * other functions do not check whether the string is valid UTF-8 or not. |
|
131 * |
|
132 * If you do not care whether "src" is valid UTF-8 or not, you should use |
|
133 * strlen() as usual, which should be much faster. |
|
134 */ |
|
135 ssize_t utf8_length(const char *src); |
|
136 |
|
137 /** |
|
138 * Measure the length of a UTF-32 string. |
|
139 */ |
|
140 size_t utf8_to_utf32_length(const char *src, size_t src_len); |
|
141 |
|
142 /** |
|
143 * Stores a UTF-32 string converted from "src" in "dst". "dst" must be large |
|
144 * enough to store the entire converted string as measured by |
|
145 * utf8_to_utf32_length plus space for a NULL terminator. |
|
146 */ |
|
147 void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst); |
|
148 |
|
149 /** |
|
150 * Returns the UTF-16 length of UTF-8 string "src". |
|
151 */ |
|
152 ssize_t utf8_to_utf16_length(const uint8_t* src, size_t srcLen); |
|
153 |
|
154 /** |
|
155 * Convert UTF-8 to UTF-16 including surrogate pairs. |
|
156 * Returns a pointer to the end of the string (where a null terminator might go |
|
157 * if you wanted to add one). |
|
158 */ |
|
159 char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* src, size_t srcLen, char16_t* dst); |
|
160 |
|
161 /** |
|
162 * Convert UTF-8 to UTF-16 including surrogate pairs. The destination buffer |
|
163 * must be large enough to hold the result as measured by utf8_to_utf16_length |
|
164 * plus an added NULL terminator. |
|
165 */ |
|
166 void utf8_to_utf16(const uint8_t* src, size_t srcLen, char16_t* dst); |
|
167 |
|
168 } |
|
169 |
|
170 #endif |