blob: bf99c37ec75b305a92fc50ce4336385b60f0ad96 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -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 */
16
Andreas Gampefd63bbf2018-10-29 12:55:35 -070017#include "string-alloc-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018
Andreas Gampe4d0589c2014-06-10 16:10:56 -070019#include "arch/memcmp16.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070020#include "array-alloc-inl.h"
Andreas Gampe2ff3b972017-06-05 18:14:53 -070021#include "base/array_ref.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070022#include "base/stl_util.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070023#include "class-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080024#include "dex/descriptors_names.h"
David Sehr0225f8e2018-01-31 08:52:24 +000025#include "dex/utf-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/accounting/card_table-inl.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080027#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "intern_table.h"
29#include "object-inl.h"
30#include "runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080031#include "string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033
34namespace art {
35namespace mirror {
36
Ian Rogersef7d42f2014-01-06 12:55:46 -080037int32_t String::FastIndexOf(int32_t ch, int32_t start) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038 int32_t count = GetLength();
39 if (start < 0) {
40 start = 0;
41 } else if (start > count) {
42 start = count;
43 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -070044 if (IsCompressed()) {
45 return FastIndexOf<uint8_t>(GetValueCompressed(), ch, start);
46 } else {
47 return FastIndexOf<uint16_t>(GetValue(), ch, start);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049}
50
Jeff Hao848f70a2014-01-15 13:49:50 -080051int String::ComputeHashCode() {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070052 int32_t hash_code = 0;
53 if (IsCompressed()) {
54 hash_code = ComputeUtf16Hash(GetValueCompressed(), GetLength());
55 } else {
56 hash_code = ComputeUtf16Hash(GetValue(), GetLength());
57 }
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070058 SetHashCode(hash_code);
59 return hash_code;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060}
61
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070062int32_t String::GetUtfLength() {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070063 if (IsCompressed()) {
64 return GetLength();
65 } else {
66 return CountUtf8Bytes(GetValue(), GetLength());
67 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068}
69
Vladimir Marko92907f32017-02-20 14:08:30 +000070inline bool String::AllASCIIExcept(const uint16_t* chars, int32_t length, uint16_t non_ascii) {
71 DCHECK(!IsASCII(non_ascii));
72 for (int32_t i = 0; i < length; ++i) {
73 if (!IsASCII(chars[i]) && chars[i] != non_ascii) {
74 return false;
75 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -070076 }
Vladimir Marko92907f32017-02-20 14:08:30 +000077 return true;
78}
79
Vladimir Marko9e57aba2017-03-16 10:45:40 +000080ObjPtr<String> String::DoReplace(Thread* self, Handle<String> src, uint16_t old_c, uint16_t new_c) {
81 int32_t length = src->GetLength();
82 DCHECK(src->IsCompressed()
83 ? ContainsElement(ArrayRef<uint8_t>(src->value_compressed_, length), old_c)
84 : ContainsElement(ArrayRef<uint16_t>(src->value_, length), old_c));
Vladimir Marko92907f32017-02-20 14:08:30 +000085 bool compressible =
86 kUseStringCompression &&
87 IsASCII(new_c) &&
Vladimir Marko9e57aba2017-03-16 10:45:40 +000088 (src->IsCompressed() || (!IsASCII(old_c) && AllASCIIExcept(src->value_, length, old_c)));
Vladimir Marko92907f32017-02-20 14:08:30 +000089 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Vladimir Marko9e57aba2017-03-16 10:45:40 +000090 const int32_t length_with_flag = String::GetFlaggedCount(length, compressible);
Vladimir Marko92907f32017-02-20 14:08:30 +000091 SetStringCountVisitor visitor(length_with_flag);
92 ObjPtr<String> string = Alloc<true>(self, length_with_flag, allocator_type, visitor);
93 if (UNLIKELY(string == nullptr)) {
94 return nullptr;
95 }
96 if (compressible) {
97 auto replace = [old_c, new_c](uint16_t c) {
98 return dchecked_integral_cast<uint8_t>((old_c != c) ? c : new_c);
99 };
100 uint8_t* out = string->value_compressed_;
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000101 if (LIKELY(src->IsCompressed())) { // LIKELY(compressible == src->IsCompressed())
102 std::transform(src->value_compressed_, src->value_compressed_ + length, out, replace);
Vladimir Marko92907f32017-02-20 14:08:30 +0000103 } else {
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000104 std::transform(src->value_, src->value_ + length, out, replace);
Vladimir Marko92907f32017-02-20 14:08:30 +0000105 }
106 DCHECK(kUseStringCompression && AllASCII(out, length));
107 } else {
108 auto replace = [old_c, new_c](uint16_t c) {
109 return (old_c != c) ? c : new_c;
110 };
111 uint16_t* out = string->value_;
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000112 if (UNLIKELY(src->IsCompressed())) { // LIKELY(compressible == src->IsCompressed())
113 std::transform(src->value_compressed_, src->value_compressed_ + length, out, replace);
Vladimir Marko92907f32017-02-20 14:08:30 +0000114 } else {
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000115 std::transform(src->value_, src->value_ + length, out, replace);
Vladimir Marko92907f32017-02-20 14:08:30 +0000116 }
117 DCHECK(!kUseStringCompression || !AllASCII(out, length));
118 }
119 return string;
Jeff Hao848f70a2014-01-15 13:49:50 -0800120}
121
122String* String::AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2) {
123 int32_t length = string->GetLength();
124 int32_t length2 = string2->GetLength();
125 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Mathieu Chartier31e88222016-10-14 18:43:19 -0700126 const bool compressible = kUseStringCompression &&
127 (string->IsCompressed() && string2->IsCompressed());
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100128 const int32_t length_with_flag = String::GetFlaggedCount(length + length2, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700129
130 SetStringCountVisitor visitor(length_with_flag);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700131 ObjPtr<String> new_string = Alloc<true>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800132 if (UNLIKELY(new_string == nullptr)) {
133 return nullptr;
134 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700135 if (compressible) {
136 uint8_t* new_value = new_string->GetValueCompressed();
137 memcpy(new_value, string->GetValueCompressed(), length * sizeof(uint8_t));
138 memcpy(new_value + length, string2->GetValueCompressed(), length2 * sizeof(uint8_t));
139 } else {
140 uint16_t* new_value = new_string->GetValue();
141 if (string->IsCompressed()) {
142 for (int i = 0; i < length; ++i) {
143 new_value[i] = string->CharAt(i);
144 }
145 } else {
146 memcpy(new_value, string->GetValue(), length * sizeof(uint16_t));
147 }
148 if (string2->IsCompressed()) {
149 for (int i = 0; i < length2; ++i) {
150 new_value[i+length] = string2->CharAt(i);
151 }
152 } else {
153 memcpy(new_value + length, string2->GetValue(), length2 * sizeof(uint16_t));
154 }
155 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700156 return new_string.Ptr();
Jeff Hao848f70a2014-01-15 13:49:50 -0800157}
158
159String* String::AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in) {
Ian Rogers4069d332014-01-03 10:28:27 -0800160 CHECK(utf16_data_in != nullptr || utf16_length == 0);
Jeff Hao848f70a2014-01-15 13:49:50 -0800161 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700162 const bool compressible = kUseStringCompression &&
163 String::AllASCII<uint16_t>(utf16_data_in, utf16_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100164 int32_t length_with_flag = String::GetFlaggedCount(utf16_length, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700165 SetStringCountVisitor visitor(length_with_flag);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700166 ObjPtr<String> string = Alloc<true>(self, length_with_flag, allocator_type, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700167 if (UNLIKELY(string == nullptr)) {
168 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700170 if (compressible) {
171 for (int i = 0; i < utf16_length; ++i) {
172 string->GetValueCompressed()[i] = static_cast<uint8_t>(utf16_data_in[i]);
173 }
174 } else {
175 uint16_t* array = string->GetValue();
176 memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t));
177 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700178 return string.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179}
180
Ian Rogersa436fde2013-08-27 23:34:06 -0700181String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700182 DCHECK(utf != nullptr);
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300183 size_t byte_count = strlen(utf);
184 size_t char_count = CountModifiedUtf8Chars(utf, byte_count);
185 return AllocFromModifiedUtf8(self, char_count, utf, byte_count);
186}
187
Mathieu Chartier31e88222016-10-14 18:43:19 -0700188String* String::AllocFromModifiedUtf8(Thread* self,
189 int32_t utf16_length,
190 const char* utf8_data_in) {
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300191 return AllocFromModifiedUtf8(self, utf16_length, utf8_data_in, strlen(utf8_data_in));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192}
193
Mathieu Chartier31e88222016-10-14 18:43:19 -0700194String* String::AllocFromModifiedUtf8(Thread* self,
195 int32_t utf16_length,
196 const char* utf8_data_in,
197 int32_t utf8_length) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800198 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700199 const bool compressible = kUseStringCompression && (utf16_length == utf8_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100200 const int32_t utf16_length_with_flag = String::GetFlaggedCount(utf16_length, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700201 SetStringCountVisitor visitor(utf16_length_with_flag);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700202 ObjPtr<String> string = Alloc<true>(self, utf16_length_with_flag, allocator_type, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700203 if (UNLIKELY(string == nullptr)) {
204 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700206 if (compressible) {
207 memcpy(string->GetValueCompressed(), utf8_data_in, utf16_length * sizeof(uint8_t));
208 } else {
209 uint16_t* utf16_data_out = string->GetValue();
210 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length);
211 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700212 return string.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213}
214
Mathieu Chartier31e88222016-10-14 18:43:19 -0700215bool String::Equals(ObjPtr<String> that) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 if (this == that) {
217 // Quick reference equality test
218 return true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700219 } else if (that == nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 // Null isn't an instanceof anything
221 return false;
222 } else if (this->GetLength() != that->GetLength()) {
223 // Quick length inequality test
224 return false;
225 } else {
226 // Note: don't short circuit on hash code as we're presumably here as the
227 // hash code was already equal
228 for (int32_t i = 0; i < that->GetLength(); ++i) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800229 if (this->CharAt(i) != that->CharAt(i)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 return false;
231 }
232 }
233 return true;
234 }
235}
236
Ian Rogersef7d42f2014-01-06 12:55:46 -0800237bool String::Equals(const char* modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000238 const int32_t length = GetLength();
239 int32_t i = 0;
240 while (i < length) {
241 const uint32_t ch = GetUtf16FromUtf8(&modified_utf8);
242 if (ch == '\0') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 return false;
244 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000245
Jeff Hao848f70a2014-01-15 13:49:50 -0800246 if (GetLeadingUtf16Char(ch) != CharAt(i++)) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000247 return false;
248 }
249
250 const uint16_t trailing = GetTrailingUtf16Char(ch);
251 if (trailing != 0) {
252 if (i == length) {
253 return false;
254 }
255
Jeff Hao848f70a2014-01-15 13:49:50 -0800256 if (CharAt(i++) != trailing) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000257 return false;
258 }
259 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 }
261 return *modified_utf8 == '\0';
262}
263
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264// Create a modified UTF-8 encoded std::string from a java/lang/String object.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265std::string String::ToModifiedUtf8() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 size_t byte_count = GetUtfLength();
267 std::string result(byte_count, static_cast<char>(0));
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700268 if (IsCompressed()) {
269 for (size_t i = 0; i < byte_count; ++i) {
270 result[i] = static_cast<char>(CharAt(i));
271 }
272 } else {
273 const uint16_t* chars = GetValue();
274 ConvertUtf16ToModifiedUtf8(&result[0], byte_count, chars, GetLength());
275 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276 return result;
277}
278
Mathieu Chartier31e88222016-10-14 18:43:19 -0700279int32_t String::CompareTo(ObjPtr<String> rhs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 // Quick test for comparison of a string with itself.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700281 ObjPtr<String> lhs = this;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 if (lhs == rhs) {
283 return 0;
284 }
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100285 int32_t lhs_count = lhs->GetLength();
286 int32_t rhs_count = rhs->GetLength();
287 int32_t count_diff = lhs_count - rhs_count;
288 int32_t min_count = (count_diff < 0) ? lhs_count : rhs_count;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700289 if (lhs->IsCompressed() && rhs->IsCompressed()) {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100290 const uint8_t* lhs_chars = lhs->GetValueCompressed();
291 const uint8_t* rhs_chars = rhs->GetValueCompressed();
292 for (int32_t i = 0; i < min_count; ++i) {
293 int32_t char_diff = static_cast<int32_t>(lhs_chars[i]) - static_cast<int32_t>(rhs_chars[i]);
294 if (char_diff != 0) {
295 return char_diff;
296 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700297 }
298 } else if (lhs->IsCompressed() || rhs->IsCompressed()) {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100299 const uint8_t* compressed_chars =
300 lhs->IsCompressed() ? lhs->GetValueCompressed() : rhs->GetValueCompressed();
301 const uint16_t* uncompressed_chars = lhs->IsCompressed() ? rhs->GetValue() : lhs->GetValue();
302 for (int32_t i = 0; i < min_count; ++i) {
303 int32_t char_diff =
304 static_cast<int32_t>(compressed_chars[i]) - static_cast<int32_t>(uncompressed_chars[i]);
305 if (char_diff != 0) {
306 return lhs->IsCompressed() ? char_diff : -char_diff;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700307 }
308 }
309 } else {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100310 const uint16_t* lhs_chars = lhs->GetValue();
311 const uint16_t* rhs_chars = rhs->GetValue();
312 // FIXME: The MemCmp16() name is misleading. It returns the char difference on mismatch
313 // where memcmp() only guarantees that the returned value has the same sign.
314 int32_t char_diff = MemCmp16(lhs_chars, rhs_chars, min_count);
315 if (char_diff != 0) {
316 return char_diff;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700317 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 }
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100319 return count_diff;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320}
321
Jeff Hao848f70a2014-01-15 13:49:50 -0800322CharArray* String::ToCharArray(Thread* self) {
323 StackHandleScope<1> hs(self);
324 Handle<String> string(hs.NewHandle(this));
Mathieu Chartier31e88222016-10-14 18:43:19 -0700325 ObjPtr<CharArray> result = CharArray::Alloc(self, GetLength());
Mathieu Chartier04e983a2015-11-13 08:36:59 -0800326 if (result != nullptr) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700327 if (string->IsCompressed()) {
328 int32_t length = string->GetLength();
329 for (int i = 0; i < length; ++i) {
330 result->GetData()[i] = string->CharAt(i);
331 }
332 } else {
333 memcpy(result->GetData(), string->GetValue(), string->GetLength() * sizeof(uint16_t));
334 }
Mathieu Chartier04e983a2015-11-13 08:36:59 -0800335 } else {
336 self->AssertPendingOOMException();
337 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700338 return result.Ptr();
Jeff Hao848f70a2014-01-15 13:49:50 -0800339}
340
341void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) {
342 uint16_t* data = array->GetData() + index;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700343 if (IsCompressed()) {
344 for (int i = start; i < end; ++i) {
345 data[i-start] = CharAt(i);
346 }
347 } else {
348 uint16_t* value = GetValue() + start;
349 memcpy(data, value, (end - start) * sizeof(uint16_t));
350 }
351}
352
353bool String::IsValueNull() {
354 return (IsCompressed()) ? (GetValueCompressed() == nullptr) : (GetValue() == nullptr);
Jeff Hao848f70a2014-01-15 13:49:50 -0800355}
356
David Sehr709b0702016-10-13 09:12:37 -0700357std::string String::PrettyStringDescriptor(ObjPtr<mirror::String> java_descriptor) {
358 if (java_descriptor == nullptr) {
359 return "null";
360 }
361 return java_descriptor->PrettyStringDescriptor();
362}
363
364std::string String::PrettyStringDescriptor() {
365 return PrettyDescriptor(ToModifiedUtf8().c_str());
366}
367
Andreas Gampeb2d18fa2017-06-06 20:46:10 -0700368ObjPtr<String> String::Intern() {
369 return Runtime::Current()->GetInternTable()->InternWeak(this);
370}
371
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372} // namespace mirror
373} // namespace art