blob: 10600e215382c7dd43a7cd96ac65d493a9112520 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Elliott Hughes814e4032011-08-23 12:07:56 -070016
17#include "utf.h"
18
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/array.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "mirror/object-inl.h"
Ian Rogersa6724902013-09-23 09:23:37 -070022#include "utf-inl.h"
Elliott Hughesb465ab02011-08-24 11:21:21 -070023
Elliott Hughes814e4032011-08-23 12:07:56 -070024namespace art {
25
26size_t CountModifiedUtf8Chars(const char* utf8) {
27 size_t len = 0;
28 int ic;
29 while ((ic = *utf8++) != '\0') {
30 len++;
31 if ((ic & 0x80) == 0) {
32 // one-byte encoding
33 continue;
34 }
35 // two- or three-byte encoding
36 utf8++;
37 if ((ic & 0x20) == 0) {
38 // two-byte encoding
39 continue;
40 }
Elliott Hughes814e4032011-08-23 12:07:56 -070041 utf8++;
Narayan Kamatha5afcfc2015-01-29 20:06:46 +000042 if ((ic & 0x10) == 0) {
43 // three-byte encoding
44 continue;
45 }
46
47 // four-byte encoding: needs to be converted into a surrogate
48 // pair.
49 utf8++;
50 len++;
Elliott Hughes814e4032011-08-23 12:07:56 -070051 }
52 return len;
53}
54
55void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
56 while (*utf8_data_in != '\0') {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +000057 const uint32_t ch = GetUtf16FromUtf8(&utf8_data_in);
58 const uint16_t leading = GetLeadingUtf16Char(ch);
59 const uint16_t trailing = GetTrailingUtf16Char(ch);
60
61 *utf16_data_out++ = leading;
62 if (trailing != 0) {
63 *utf16_data_out++ = trailing;
64 }
Elliott Hughes814e4032011-08-23 12:07:56 -070065 }
66}
67
Elliott Hughesb465ab02011-08-24 11:21:21 -070068void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count) {
69 while (char_count--) {
Narayan Kamathe16dad12015-02-13 11:49:22 +000070 const uint16_t ch = *utf16_in++;
Elliott Hughesb465ab02011-08-24 11:21:21 -070071 if (ch > 0 && ch <= 0x7f) {
72 *utf8_out++ = ch;
73 } else {
Narayan Kamathe16dad12015-02-13 11:49:22 +000074 // char_count == 0 here implies we've encountered an unpaired
75 // surrogate and we have no choice but to encode it as 3-byte UTF
76 // sequence. Note that unpaired surrogates can occur as a part of
77 // "normal" operation.
78 if ((ch >= 0xd800 && ch <= 0xdbff) && (char_count > 0)) {
79 const uint16_t ch2 = *utf16_in;
80
81 // Check if the other half of the pair is within the expected
82 // range. If it isn't, we will have to emit both "halves" as
83 // separate 3 byte sequences.
84 if (ch2 >= 0xdc00 && ch2 <= 0xdfff) {
85 utf16_in++;
86 char_count--;
87 const uint32_t code_point = (ch << 10) + ch2 - 0x035fdc00;
88 *utf8_out++ = (code_point >> 18) | 0xf0;
89 *utf8_out++ = ((code_point >> 12) & 0x3f) | 0x80;
90 *utf8_out++ = ((code_point >> 6) & 0x3f) | 0x80;
91 *utf8_out++ = (code_point & 0x3f) | 0x80;
92 continue;
93 }
94 }
95
Elliott Hughesb465ab02011-08-24 11:21:21 -070096 if (ch > 0x07ff) {
Narayan Kamathe16dad12015-02-13 11:49:22 +000097 // Three byte encoding.
Elliott Hughesb465ab02011-08-24 11:21:21 -070098 *utf8_out++ = (ch >> 12) | 0xe0;
99 *utf8_out++ = ((ch >> 6) & 0x3f) | 0x80;
100 *utf8_out++ = (ch & 0x3f) | 0x80;
101 } else /*(ch > 0x7f || ch == 0)*/ {
Narayan Kamathe16dad12015-02-13 11:49:22 +0000102 // Two byte encoding.
Elliott Hughesb465ab02011-08-24 11:21:21 -0700103 *utf8_out++ = (ch >> 6) | 0xc0;
104 *utf8_out++ = (ch & 0x3f) | 0x80;
105 }
106 }
107 }
108}
109
Elliott Hughes814e4032011-08-23 12:07:56 -0700110int32_t ComputeUtf16Hash(const uint16_t* chars, size_t char_count) {
Ian Rogers8f41dc32014-10-30 15:16:16 -0700111 uint32_t hash = 0;
Elliott Hughes814e4032011-08-23 12:07:56 -0700112 while (char_count--) {
113 hash = hash * 31 + *chars++;
114 }
Ian Rogers8f41dc32014-10-30 15:16:16 -0700115 return static_cast<int32_t>(hash);
Elliott Hughes814e4032011-08-23 12:07:56 -0700116}
117
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800118size_t ComputeModifiedUtf8Hash(const char* chars) {
119 size_t hash = 0;
Ian Rogers68b56852014-08-29 20:19:11 -0700120 while (*chars != '\0') {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800121 hash = hash * 31 + *chars++;
Ian Rogers68b56852014-08-29 20:19:11 -0700122 }
Ian Rogers8f41dc32014-10-30 15:16:16 -0700123 return static_cast<int32_t>(hash);
Ian Rogers68b56852014-08-29 20:19:11 -0700124}
125
Vladimir Markoa48aef42014-12-03 17:53:53 +0000126int CompareModifiedUtf8ToUtf16AsCodePointValues(const char* utf8, const uint16_t* utf16,
127 size_t utf16_length) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700128 for (;;) {
Vladimir Markoa48aef42014-12-03 17:53:53 +0000129 if (*utf8 == '\0') {
130 return (utf16_length == 0) ? 0 : -1;
131 } else if (utf16_length == 0) {
Ian Rogers637c65b2013-05-31 11:46:00 -0700132 return 1;
133 }
134
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000135 const uint32_t pair = GetUtf16FromUtf8(&utf8);
Ian Rogers637c65b2013-05-31 11:46:00 -0700136
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000137 // First compare the leading utf16 char.
138 const uint16_t lhs = GetLeadingUtf16Char(pair);
139 const uint16_t rhs = *utf16++;
140 --utf16_length;
141 if (lhs != rhs) {
142 return lhs > rhs ? 1 : -1;
143 }
144
145 // Then compare the trailing utf16 char. First check if there
146 // are any characters left to consume.
147 const uint16_t lhs2 = GetTrailingUtf16Char(pair);
148 if (lhs2 != 0) {
149 if (utf16_length == 0) {
150 return 1;
151 }
152
153 const uint16_t rhs2 = *utf16++;
154 --utf16_length;
155 if (lhs2 != rhs2) {
156 return lhs2 > rhs2 ? 1 : -1;
157 }
Ian Rogers637c65b2013-05-31 11:46:00 -0700158 }
159 }
160}
161
Elliott Hughes814e4032011-08-23 12:07:56 -0700162size_t CountUtf8Bytes(const uint16_t* chars, size_t char_count) {
163 size_t result = 0;
164 while (char_count--) {
Narayan Kamathe16dad12015-02-13 11:49:22 +0000165 const uint16_t ch = *chars++;
Elliott Hughes814e4032011-08-23 12:07:56 -0700166 if (ch > 0 && ch <= 0x7f) {
167 ++result;
Narayan Kamathe16dad12015-02-13 11:49:22 +0000168 } else if (ch >= 0xd800 && ch <= 0xdbff) {
169 if (char_count > 0) {
170 const uint16_t ch2 = *chars;
171 // If we find a properly paired surrogate, we emit it as a 4 byte
172 // UTF sequence. If we find an unpaired leading or trailing surrogate,
173 // we emit it as a 3 byte sequence like would have done earlier.
174 if (ch2 >= 0xdc00 && ch2 <= 0xdfff) {
175 chars++;
176 char_count--;
177
178 result += 4;
179 } else {
180 result += 3;
181 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700182 } else {
Narayan Kamathe16dad12015-02-13 11:49:22 +0000183 // This implies we found an unpaired trailing surrogate at the end
184 // of a string.
185 result += 3;
Elliott Hughes814e4032011-08-23 12:07:56 -0700186 }
Narayan Kamathe16dad12015-02-13 11:49:22 +0000187 } else if (ch > 0x7ff) {
188 result += 3;
189 } else {
190 result += 2;
Elliott Hughes814e4032011-08-23 12:07:56 -0700191 }
192 }
193 return result;
194}
195
196} // namespace art