blob: d60d5d6ba634599ded38e60a8fd6ac582680678a [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);
Michael Wright5bacef32016-05-09 14:43:31 +010030char16_t *strstr16(const char16_t*, const char16_t*);
Kenny Rootba0165b2010-11-09 14:37:23 -080031
Sergio Giro9de67762016-07-20 20:01:33 +010032// Version of comparison that supports embedded NULs.
Kenny Rootba0165b2010-11-09 14:37:23 -080033// This is different than strncmp() because we don't stop
34// at a nul character and consider the strings to be different
35// if the lengths are different (thus we need to supply the
36// lengths of both strings). This can also be used when
37// your string is not nul-terminated as it will have the
38// equivalent result as strcmp16 (unlike strncmp16).
39int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2);
40
Kenny Rootba0165b2010-11-09 14:37:23 -080041/**
42 * Measure the length of a UTF-32 string in UTF-8. If the string is invalid
43 * such as containing a surrogate character, -1 will be returned.
44 */
45ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len);
46
47/**
48 * Stores a UTF-8 string converted from "src" in "dst", if "dst_length" is not
49 * large enough to store the string, the part of the "src" string is stored
50 * into "dst" as much as possible. See the examples for more detail.
51 * Returns the size actually used for storing the string.
Sergio Giro9de67762016-07-20 20:01:33 +010052 * dst" is not nul-terminated when dst_len is fully used (like strncpy).
Kenny Rootba0165b2010-11-09 14:37:23 -080053 *
Colin Cross17b5b822016-09-15 18:15:37 -070054 * \code
Kenny Rootba0165b2010-11-09 14:37:23 -080055 * Example 1
56 * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84)
57 * "src_len" == 2
58 * "dst_len" >= 7
59 * ->
60 * Returned value == 6
61 * "dst" becomes \xE3\x81\x82\xE3\x81\x84\0
Sergio Giro9de67762016-07-20 20:01:33 +010062 * (note that "dst" is nul-terminated)
Kenny Rootba0165b2010-11-09 14:37:23 -080063 *
64 * Example 2
65 * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84)
66 * "src_len" == 2
67 * "dst_len" == 5
68 * ->
69 * Returned value == 3
70 * "dst" becomes \xE3\x81\x82\0
Sergio Giro9de67762016-07-20 20:01:33 +010071 * (note that "dst" is nul-terminated, but \u3044 is not stored in "dst"
Kenny Rootba0165b2010-11-09 14:37:23 -080072 * since "dst" does not have enough size to store the character)
73 *
74 * Example 3
75 * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84)
76 * "src_len" == 2
77 * "dst_len" == 6
78 * ->
79 * Returned value == 6
80 * "dst" becomes \xE3\x81\x82\xE3\x81\x84
Sergio Giro9de67762016-07-20 20:01:33 +010081 * (note that "dst" is NOT nul-terminated, like strncpy)
Colin Cross17b5b822016-09-15 18:15:37 -070082 * \endcode
Kenny Rootba0165b2010-11-09 14:37:23 -080083 */
Sergio Giro1cfa56d2016-06-28 18:02:29 +010084void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst, size_t dst_len);
Kenny Rootba0165b2010-11-09 14:37:23 -080085
86/**
87 * Returns the unicode value at "index".
88 * Returns -1 when the index is invalid (equals to or more than "src_len").
89 * If returned value is positive, it is able to be converted to char32_t, which
90 * is unsigned. Then, if "next_index" is not NULL, the next index to be used is
91 * stored in "next_index". "next_index" can be NULL.
92 */
93int32_t utf32_from_utf8_at(const char *src, size_t src_len, size_t index, size_t *next_index);
94
95
96/**
97 * Returns the UTF-8 length of UTF-16 string "src".
98 */
99ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len);
100
101/**
102 * Converts a UTF-16 string to UTF-8. The destination buffer must be large
103 * enough to fit the UTF-16 as measured by utf16_to_utf8_length with an added
Sergio Giro9de67762016-07-20 20:01:33 +0100104 * NUL terminator.
Kenny Rootba0165b2010-11-09 14:37:23 -0800105 */
Sergio Giro1cfa56d2016-06-28 18:02:29 +0100106void utf16_to_utf8(const char16_t* src, size_t src_len, char* dst, size_t dst_len);
Kenny Rootba0165b2010-11-09 14:37:23 -0800107
108/**
Sergio Giro9de67762016-07-20 20:01:33 +0100109 * Returns the UTF-16 length of UTF-8 string "src". Returns -1 in case
110 * it's invalid utf8. No buffer over-read occurs because of bound checks. Using overreadIsFatal you
111 * can ask to log a message and fail in case the invalid utf8 could have caused an override if no
112 * bound checks were used (otherwise -1 is returned).
Kenny Rootba0165b2010-11-09 14:37:23 -0800113 */
Sergio Giro9de67762016-07-20 20:01:33 +0100114ssize_t utf8_to_utf16_length(const uint8_t* src, size_t srcLen, bool overreadIsFatal = false);
Kenny Rootba0165b2010-11-09 14:37:23 -0800115
116/**
Jeff Brownaa983c92011-10-07 13:28:18 -0700117 * Convert UTF-8 to UTF-16 including surrogate pairs.
Sergio Giro9de67762016-07-20 20:01:33 +0100118 * Returns a pointer to the end of the string (where a NUL terminator might go
119 * if you wanted to add one). At most dstLen characters are written; it won't emit half a surrogate
120 * pair. If dstLen == 0 nothing is written and dst is returned. If dstLen > SSIZE_MAX it aborts
121 * (this being probably a negative number returned as an error and casted to unsigned).
Jeff Brownaa983c92011-10-07 13:28:18 -0700122 */
Sergio Giro9de67762016-07-20 20:01:33 +0100123char16_t* utf8_to_utf16_no_null_terminator(
124 const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen);
Jeff Brownaa983c92011-10-07 13:28:18 -0700125
126/**
Sergio Giro9de67762016-07-20 20:01:33 +0100127 * Convert UTF-8 to UTF-16 including surrogate pairs. At most dstLen - 1
128 * characters are written; it won't emit half a surrogate pair; and a NUL terminator is appended
129 * after. dstLen - 1 can be measured beforehand using utf8_to_utf16_length. Aborts if dstLen == 0
130 * (at least one character is needed for the NUL terminator) or dstLen > SSIZE_MAX (the latter
131 * case being likely a negative number returned as an error and casted to unsigned) . Returns a
132 * pointer to the NUL terminator.
Kenny Rootba0165b2010-11-09 14:37:23 -0800133 */
Sergio Giro9de67762016-07-20 20:01:33 +0100134char16_t *utf8_to_utf16(
135 const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen);
Dianne Hackborn0f10d0a2013-07-31 16:04:39 -0700136
Kenny Rootba0165b2010-11-09 14:37:23 -0800137}
138
139#endif