blob: b76a5e2685359db0d4b75dc267f2d6076f8146f2 [file] [log] [blame]
Kenny Rootba0165b2010-11-09 14:37:23 -08001/*
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
23extern "C" {
24
Kenny Rootba0165b2010-11-09 14:37:23 -080025// Standard string functions on char16_t strings.
26int strcmp16(const char16_t *, const char16_t *);
27int strncmp16(const char16_t *s1, const char16_t *s2, size_t n);
28size_t strlen16(const char16_t *);
29size_t strnlen16(const char16_t *, size_t);
30char16_t *strcpy16(char16_t *, const char16_t *);
31char16_t *strncpy16(char16_t *, const char16_t *, size_t);
32
33// Version of comparison that supports embedded nulls.
34// This is different than strncmp() because we don't stop
35// at a nul character and consider the strings to be different
36// if the lengths are different (thus we need to supply the
37// lengths of both strings). This can also be used when
38// your string is not nul-terminated as it will have the
39// equivalent result as strcmp16 (unlike strncmp16).
40int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2);
41
42// Version of strzcmp16 for comparing strings in different endianness.
43int strzcmp16_h_n(const char16_t *s1H, size_t n1, const char16_t *s2N, size_t n2);
44
45// Standard string functions on char32_t strings.
46size_t strlen32(const char32_t *);
47size_t strnlen32(const char32_t *, size_t);
48
49/**
50 * Measure the length of a UTF-32 string in UTF-8. If the string is invalid
51 * such as containing a surrogate character, -1 will be returned.
52 */
53ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len);
54
55/**
56 * Stores a UTF-8 string converted from "src" in "dst", if "dst_length" is not
57 * large enough to store the string, the part of the "src" string is stored
58 * into "dst" as much as possible. See the examples for more detail.
59 * Returns the size actually used for storing the string.
60 * dst" is not null-terminated when dst_len is fully used (like strncpy).
61 *
62 * Example 1
63 * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84)
64 * "src_len" == 2
65 * "dst_len" >= 7
66 * ->
67 * Returned value == 6
68 * "dst" becomes \xE3\x81\x82\xE3\x81\x84\0
69 * (note that "dst" is null-terminated)
70 *
71 * Example 2
72 * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84)
73 * "src_len" == 2
74 * "dst_len" == 5
75 * ->
76 * Returned value == 3
77 * "dst" becomes \xE3\x81\x82\0
78 * (note that "dst" is null-terminated, but \u3044 is not stored in "dst"
79 * since "dst" does not have enough size to store the character)
80 *
81 * Example 3
82 * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84)
83 * "src_len" == 2
84 * "dst_len" == 6
85 * ->
86 * Returned value == 6
87 * "dst" becomes \xE3\x81\x82\xE3\x81\x84
88 * (note that "dst" is NOT null-terminated, like strncpy)
89 */
90void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst);
91
92/**
93 * Returns the unicode value at "index".
94 * Returns -1 when the index is invalid (equals to or more than "src_len").
95 * If returned value is positive, it is able to be converted to char32_t, which
96 * is unsigned. Then, if "next_index" is not NULL, the next index to be used is
97 * stored in "next_index". "next_index" can be NULL.
98 */
99int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index);
100
101
102/**
103 * Returns the UTF-8 length of UTF-16 string "src".
104 */
105ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len);
106
107/**
108 * Converts a UTF-16 string to UTF-8. The destination buffer must be large
109 * enough to fit the UTF-16 as measured by utf16_to_utf8_length with an added
110 * NULL terminator.
111 */
112void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst);
113
114/**
115 * Returns the length of "src" when "src" is valid UTF-8 string.
116 * Returns 0 if src is NULL or 0-length string. Returns -1 when the source
117 * is an invalid string.
118 *
119 * This function should be used to determine whether "src" is valid UTF-8
120 * characters with valid unicode codepoints. "src" must be null-terminated.
121 *
122 * If you are going to use other utf8_to_... functions defined in this header
123 * with string which may not be valid UTF-8 with valid codepoint (form 0 to
124 * 0x10FFFF), you should use this function before calling others, since the
125 * other functions do not check whether the string is valid UTF-8 or not.
126 *
127 * If you do not care whether "src" is valid UTF-8 or not, you should use
128 * strlen() as usual, which should be much faster.
129 */
130ssize_t utf8_length(const char *src);
131
132/**
133 * Measure the length of a UTF-32 string.
134 */
135size_t utf8_to_utf32_length(const char *src, size_t src_len);
136
137/**
138 * Stores a UTF-32 string converted from "src" in "dst". "dst" must be large
139 * enough to store the entire converted string as measured by
140 * utf8_to_utf32_length plus space for a NULL terminator.
141 */
142void utf8_to_utf32(const char* src, size_t src_len, char32_t* dst);
143
144/**
145 * Returns the UTF-16 length of UTF-8 string "src".
146 */
147ssize_t utf8_to_utf16_length(const uint8_t* src, size_t srcLen);
148
149/**
Jeff Brownaa983c92011-10-07 13:28:18 -0700150 * Convert UTF-8 to UTF-16 including surrogate pairs.
151 * Returns a pointer to the end of the string (where a null terminator might go
152 * if you wanted to add one).
153 */
154char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* src, size_t srcLen, char16_t* dst);
155
156/**
Kenny Rootba0165b2010-11-09 14:37:23 -0800157 * Convert UTF-8 to UTF-16 including surrogate pairs. The destination buffer
158 * must be large enough to hold the result as measured by utf8_to_utf16_length
159 * plus an added NULL terminator.
160 */
161void utf8_to_utf16(const uint8_t* src, size_t srcLen, char16_t* dst);
162
Dianne Hackborn0f10d0a2013-07-31 16:04:39 -0700163/**
164 * Like utf8_to_utf16_no_null_terminator, but you can supply a maximum length of the
165 * decoded string. The decoded string will fill up to that length; if it is longer
166 * the returned pointer will be to the character after dstLen.
167 */
168char16_t* utf8_to_utf16_n(const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen);
169
Kenny Rootba0165b2010-11-09 14:37:23 -0800170}
171
172#endif