blob: 0356080353e72d23420ac9fc351c70138998fec5 [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);
Vladimir Marko9b81ac32019-05-16 16:47:08 +010092 ObjPtr<String> string = Alloc(self, length_with_flag, allocator_type, visitor);
Vladimir Marko92907f32017-02-20 14:08:30 +000093 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
Vladimir Marko179b7c62019-03-22 13:38:57 +0000122ObjPtr<String> String::AllocFromStrings(Thread* self,
123 Handle<String> string,
124 Handle<String> string2) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800125 int32_t length = string->GetLength();
126 int32_t length2 = string2->GetLength();
127 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Mathieu Chartier31e88222016-10-14 18:43:19 -0700128 const bool compressible = kUseStringCompression &&
129 (string->IsCompressed() && string2->IsCompressed());
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100130 const int32_t length_with_flag = String::GetFlaggedCount(length + length2, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700131
132 SetStringCountVisitor visitor(length_with_flag);
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100133 ObjPtr<String> new_string = Alloc(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800134 if (UNLIKELY(new_string == nullptr)) {
135 return nullptr;
136 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700137 if (compressible) {
138 uint8_t* new_value = new_string->GetValueCompressed();
139 memcpy(new_value, string->GetValueCompressed(), length * sizeof(uint8_t));
140 memcpy(new_value + length, string2->GetValueCompressed(), length2 * sizeof(uint8_t));
141 } else {
142 uint16_t* new_value = new_string->GetValue();
143 if (string->IsCompressed()) {
144 for (int i = 0; i < length; ++i) {
145 new_value[i] = string->CharAt(i);
146 }
147 } else {
148 memcpy(new_value, string->GetValue(), length * sizeof(uint16_t));
149 }
150 if (string2->IsCompressed()) {
151 for (int i = 0; i < length2; ++i) {
152 new_value[i+length] = string2->CharAt(i);
153 }
154 } else {
155 memcpy(new_value + length, string2->GetValue(), length2 * sizeof(uint16_t));
156 }
157 }
Vladimir Marko179b7c62019-03-22 13:38:57 +0000158 return new_string;
Jeff Hao848f70a2014-01-15 13:49:50 -0800159}
160
Vladimir Marko179b7c62019-03-22 13:38:57 +0000161ObjPtr<String> String::AllocFromUtf16(Thread* self,
162 int32_t utf16_length,
163 const uint16_t* utf16_data_in) {
Ian Rogers4069d332014-01-03 10:28:27 -0800164 CHECK(utf16_data_in != nullptr || utf16_length == 0);
Jeff Hao848f70a2014-01-15 13:49:50 -0800165 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700166 const bool compressible = kUseStringCompression &&
167 String::AllASCII<uint16_t>(utf16_data_in, utf16_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100168 int32_t length_with_flag = String::GetFlaggedCount(utf16_length, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700169 SetStringCountVisitor visitor(length_with_flag);
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100170 ObjPtr<String> string = Alloc(self, length_with_flag, allocator_type, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700171 if (UNLIKELY(string == nullptr)) {
172 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700174 if (compressible) {
175 for (int i = 0; i < utf16_length; ++i) {
176 string->GetValueCompressed()[i] = static_cast<uint8_t>(utf16_data_in[i]);
177 }
178 } else {
179 uint16_t* array = string->GetValue();
180 memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t));
181 }
Vladimir Marko179b7c62019-03-22 13:38:57 +0000182 return string;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183}
184
Vladimir Marko179b7c62019-03-22 13:38:57 +0000185ObjPtr<String> String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700186 DCHECK(utf != nullptr);
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300187 size_t byte_count = strlen(utf);
188 size_t char_count = CountModifiedUtf8Chars(utf, byte_count);
189 return AllocFromModifiedUtf8(self, char_count, utf, byte_count);
190}
191
Vladimir Marko179b7c62019-03-22 13:38:57 +0000192ObjPtr<String> String::AllocFromModifiedUtf8(Thread* self,
193 int32_t utf16_length,
194 const char* utf8_data_in) {
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300195 return AllocFromModifiedUtf8(self, utf16_length, utf8_data_in, strlen(utf8_data_in));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196}
197
Vladimir Marko179b7c62019-03-22 13:38:57 +0000198ObjPtr<String> String::AllocFromModifiedUtf8(Thread* self,
199 int32_t utf16_length,
200 const char* utf8_data_in,
201 int32_t utf8_length) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800202 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700203 const bool compressible = kUseStringCompression && (utf16_length == utf8_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100204 const int32_t utf16_length_with_flag = String::GetFlaggedCount(utf16_length, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700205 SetStringCountVisitor visitor(utf16_length_with_flag);
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100206 ObjPtr<String> string = Alloc(self, utf16_length_with_flag, allocator_type, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700207 if (UNLIKELY(string == nullptr)) {
208 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700210 if (compressible) {
211 memcpy(string->GetValueCompressed(), utf8_data_in, utf16_length * sizeof(uint8_t));
212 } else {
213 uint16_t* utf16_data_out = string->GetValue();
214 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length);
215 }
Vladimir Marko179b7c62019-03-22 13:38:57 +0000216 return string;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217}
218
Mathieu Chartier31e88222016-10-14 18:43:19 -0700219bool String::Equals(ObjPtr<String> that) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 if (this == that) {
221 // Quick reference equality test
222 return true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700223 } else if (that == nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 // Null isn't an instanceof anything
225 return false;
226 } else if (this->GetLength() != that->GetLength()) {
227 // Quick length inequality test
228 return false;
229 } else {
230 // Note: don't short circuit on hash code as we're presumably here as the
231 // hash code was already equal
232 for (int32_t i = 0; i < that->GetLength(); ++i) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800233 if (this->CharAt(i) != that->CharAt(i)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234 return false;
235 }
236 }
237 return true;
238 }
239}
240
Ian Rogersef7d42f2014-01-06 12:55:46 -0800241bool String::Equals(const char* modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000242 const int32_t length = GetLength();
243 int32_t i = 0;
244 while (i < length) {
245 const uint32_t ch = GetUtf16FromUtf8(&modified_utf8);
246 if (ch == '\0') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 return false;
248 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000249
Jeff Hao848f70a2014-01-15 13:49:50 -0800250 if (GetLeadingUtf16Char(ch) != CharAt(i++)) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000251 return false;
252 }
253
254 const uint16_t trailing = GetTrailingUtf16Char(ch);
255 if (trailing != 0) {
256 if (i == length) {
257 return false;
258 }
259
Jeff Hao848f70a2014-01-15 13:49:50 -0800260 if (CharAt(i++) != trailing) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000261 return false;
262 }
263 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 }
265 return *modified_utf8 == '\0';
266}
267
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268// Create a modified UTF-8 encoded std::string from a java/lang/String object.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800269std::string String::ToModifiedUtf8() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 size_t byte_count = GetUtfLength();
271 std::string result(byte_count, static_cast<char>(0));
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700272 if (IsCompressed()) {
273 for (size_t i = 0; i < byte_count; ++i) {
274 result[i] = static_cast<char>(CharAt(i));
275 }
276 } else {
277 const uint16_t* chars = GetValue();
278 ConvertUtf16ToModifiedUtf8(&result[0], byte_count, chars, GetLength());
279 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 return result;
281}
282
Mathieu Chartier31e88222016-10-14 18:43:19 -0700283int32_t String::CompareTo(ObjPtr<String> rhs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284 // Quick test for comparison of a string with itself.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700285 ObjPtr<String> lhs = this;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 if (lhs == rhs) {
287 return 0;
288 }
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100289 int32_t lhs_count = lhs->GetLength();
290 int32_t rhs_count = rhs->GetLength();
291 int32_t count_diff = lhs_count - rhs_count;
292 int32_t min_count = (count_diff < 0) ? lhs_count : rhs_count;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700293 if (lhs->IsCompressed() && rhs->IsCompressed()) {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100294 const uint8_t* lhs_chars = lhs->GetValueCompressed();
295 const uint8_t* rhs_chars = rhs->GetValueCompressed();
296 for (int32_t i = 0; i < min_count; ++i) {
297 int32_t char_diff = static_cast<int32_t>(lhs_chars[i]) - static_cast<int32_t>(rhs_chars[i]);
298 if (char_diff != 0) {
299 return char_diff;
300 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700301 }
302 } else if (lhs->IsCompressed() || rhs->IsCompressed()) {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100303 const uint8_t* compressed_chars =
304 lhs->IsCompressed() ? lhs->GetValueCompressed() : rhs->GetValueCompressed();
305 const uint16_t* uncompressed_chars = lhs->IsCompressed() ? rhs->GetValue() : lhs->GetValue();
306 for (int32_t i = 0; i < min_count; ++i) {
307 int32_t char_diff =
308 static_cast<int32_t>(compressed_chars[i]) - static_cast<int32_t>(uncompressed_chars[i]);
309 if (char_diff != 0) {
310 return lhs->IsCompressed() ? char_diff : -char_diff;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700311 }
312 }
313 } else {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100314 const uint16_t* lhs_chars = lhs->GetValue();
315 const uint16_t* rhs_chars = rhs->GetValue();
316 // FIXME: The MemCmp16() name is misleading. It returns the char difference on mismatch
317 // where memcmp() only guarantees that the returned value has the same sign.
318 int32_t char_diff = MemCmp16(lhs_chars, rhs_chars, min_count);
319 if (char_diff != 0) {
320 return char_diff;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700321 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 }
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100323 return count_diff;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324}
325
Vladimir Marko3068d582019-05-28 16:39:29 +0100326ObjPtr<CharArray> String::ToCharArray(Handle<String> h_this, Thread* self) {
327 ObjPtr<CharArray> result = CharArray::Alloc(self, h_this->GetLength());
Mathieu Chartier04e983a2015-11-13 08:36:59 -0800328 if (result != nullptr) {
Vladimir Marko3068d582019-05-28 16:39:29 +0100329 if (h_this->IsCompressed()) {
330 int32_t length = h_this->GetLength();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700331 for (int i = 0; i < length; ++i) {
Vladimir Marko3068d582019-05-28 16:39:29 +0100332 result->GetData()[i] = h_this->CharAt(i);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700333 }
334 } else {
Vladimir Marko3068d582019-05-28 16:39:29 +0100335 memcpy(result->GetData(), h_this->GetValue(), h_this->GetLength() * sizeof(uint16_t));
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700336 }
Mathieu Chartier04e983a2015-11-13 08:36:59 -0800337 } else {
338 self->AssertPendingOOMException();
339 }
Vladimir Marko179b7c62019-03-22 13:38:57 +0000340 return result;
Jeff Hao848f70a2014-01-15 13:49:50 -0800341}
342
343void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) {
344 uint16_t* data = array->GetData() + index;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700345 if (IsCompressed()) {
346 for (int i = start; i < end; ++i) {
347 data[i-start] = CharAt(i);
348 }
349 } else {
350 uint16_t* value = GetValue() + start;
351 memcpy(data, value, (end - start) * sizeof(uint16_t));
352 }
353}
354
355bool String::IsValueNull() {
356 return (IsCompressed()) ? (GetValueCompressed() == nullptr) : (GetValue() == nullptr);
Jeff Hao848f70a2014-01-15 13:49:50 -0800357}
358
David Sehr709b0702016-10-13 09:12:37 -0700359std::string String::PrettyStringDescriptor(ObjPtr<mirror::String> java_descriptor) {
360 if (java_descriptor == nullptr) {
361 return "null";
362 }
363 return java_descriptor->PrettyStringDescriptor();
364}
365
366std::string String::PrettyStringDescriptor() {
367 return PrettyDescriptor(ToModifiedUtf8().c_str());
368}
369
Andreas Gampeb2d18fa2017-06-06 20:46:10 -0700370ObjPtr<String> String::Intern() {
371 return Runtime::Current()->GetInternTable()->InternWeak(this);
372}
373
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374} // namespace mirror
375} // namespace art