blob: 704433aca13763d3486af1515d57b8bf4c90b150 [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
Vladimir Marko92907f32017-02-20 14:08:30 +000062inline bool String::AllASCIIExcept(const uint16_t* chars, int32_t length, uint16_t non_ascii) {
63 DCHECK(!IsASCII(non_ascii));
64 for (int32_t i = 0; i < length; ++i) {
65 if (!IsASCII(chars[i]) && chars[i] != non_ascii) {
66 return false;
67 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -070068 }
Vladimir Marko92907f32017-02-20 14:08:30 +000069 return true;
70}
71
Vladimir Marko9e57aba2017-03-16 10:45:40 +000072ObjPtr<String> String::DoReplace(Thread* self, Handle<String> src, uint16_t old_c, uint16_t new_c) {
73 int32_t length = src->GetLength();
74 DCHECK(src->IsCompressed()
75 ? ContainsElement(ArrayRef<uint8_t>(src->value_compressed_, length), old_c)
76 : ContainsElement(ArrayRef<uint16_t>(src->value_, length), old_c));
Vladimir Marko92907f32017-02-20 14:08:30 +000077 bool compressible =
78 kUseStringCompression &&
79 IsASCII(new_c) &&
Vladimir Marko9e57aba2017-03-16 10:45:40 +000080 (src->IsCompressed() || (!IsASCII(old_c) && AllASCIIExcept(src->value_, length, old_c)));
Vladimir Marko92907f32017-02-20 14:08:30 +000081 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Vladimir Marko9e57aba2017-03-16 10:45:40 +000082 const int32_t length_with_flag = String::GetFlaggedCount(length, compressible);
Vladimir Marko76295482020-10-26 12:28:37 +000083
84 auto visitor = [=](ObjPtr<Object> obj, size_t usable_size) REQUIRES_SHARED(Locks::mutator_lock_) {
85 SetStringCountVisitor set_string_count_visitor(length_with_flag);
86 set_string_count_visitor(obj, usable_size);
87 ObjPtr<String> new_string = obj->AsString();
88 if (compressible) {
89 auto replace = [old_c, new_c](uint16_t c) {
90 return dchecked_integral_cast<uint8_t>((old_c != c) ? c : new_c);
91 };
92 uint8_t* out = new_string->value_compressed_;
93 if (LIKELY(src->IsCompressed())) { // LIKELY(compressible == src->IsCompressed())
94 std::transform(src->value_compressed_, src->value_compressed_ + length, out, replace);
95 } else {
96 std::transform(src->value_, src->value_ + length, out, replace);
97 }
98 DCHECK(kUseStringCompression && AllASCII(out, length));
Vladimir Marko92907f32017-02-20 14:08:30 +000099 } else {
Vladimir Marko76295482020-10-26 12:28:37 +0000100 auto replace = [old_c, new_c](uint16_t c) {
101 return (old_c != c) ? c : new_c;
102 };
103 uint16_t* out = new_string->value_;
104 if (UNLIKELY(src->IsCompressed())) { // LIKELY(compressible == src->IsCompressed())
105 std::transform(src->value_compressed_, src->value_compressed_ + length, out, replace);
106 } else {
107 std::transform(src->value_, src->value_ + length, out, replace);
108 }
109 DCHECK(!kUseStringCompression || !AllASCII(out, length));
Vladimir Marko92907f32017-02-20 14:08:30 +0000110 }
Vladimir Marko76295482020-10-26 12:28:37 +0000111 };
112 return Alloc(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800113}
114
Vladimir Marko036b0702020-10-27 10:36:06 +0000115ObjPtr<String> String::DoConcat(Thread* self, Handle<String> h_this, Handle<String> h_arg) {
116 int32_t length_this = h_this->GetLength();
117 int32_t length_arg = h_arg->GetLength();
Jeff Hao848f70a2014-01-15 13:49:50 -0800118 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Vladimir Marko036b0702020-10-27 10:36:06 +0000119 const bool compressible =
120 kUseStringCompression && (h_this->IsCompressed() && h_arg->IsCompressed());
121 const int32_t length_with_flag = String::GetFlaggedCount(length_this + length_arg, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700122
Vladimir Marko76295482020-10-26 12:28:37 +0000123 auto visitor = [=](ObjPtr<Object> obj, size_t usable_size) REQUIRES_SHARED(Locks::mutator_lock_) {
124 SetStringCountVisitor set_string_count_visitor(length_with_flag);
125 set_string_count_visitor(obj, usable_size);
126 ObjPtr<String> new_string = obj->AsString();
127 if (compressible) {
128 uint8_t* new_value = new_string->GetValueCompressed();
Vladimir Marko036b0702020-10-27 10:36:06 +0000129 memcpy(new_value, h_this->GetValueCompressed(), length_this * sizeof(uint8_t));
130 memcpy(new_value + length_this, h_arg->GetValueCompressed(), length_arg * sizeof(uint8_t));
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700131 } else {
Vladimir Marko76295482020-10-26 12:28:37 +0000132 uint16_t* new_value = new_string->GetValue();
Vladimir Marko036b0702020-10-27 10:36:06 +0000133 if (h_this->IsCompressed()) {
134 const uint8_t* value_this = h_this->GetValueCompressed();
135 for (int i = 0; i < length_this; ++i) {
136 new_value[i] = value_this[i];
Vladimir Marko76295482020-10-26 12:28:37 +0000137 }
138 } else {
Vladimir Marko036b0702020-10-27 10:36:06 +0000139 memcpy(new_value, h_this->GetValue(), length_this * sizeof(uint16_t));
chapin15efe162020-10-23 19:20:31 +0000140 }
Vladimir Marko036b0702020-10-27 10:36:06 +0000141 if (h_arg->IsCompressed()) {
142 const uint8_t* value_arg = h_arg->GetValueCompressed();
143 for (int i = 0; i < length_arg; ++i) {
144 new_value[i + length_this] = value_arg[i];
Vladimir Marko76295482020-10-26 12:28:37 +0000145 }
146 } else {
Vladimir Marko036b0702020-10-27 10:36:06 +0000147 memcpy(new_value + length_this, h_arg->GetValue(), length_arg * sizeof(uint16_t));
Vladimir Marko76295482020-10-26 12:28:37 +0000148 }
chapin15efe162020-10-23 19:20:31 +0000149 }
Vladimir Marko76295482020-10-26 12:28:37 +0000150 };
151 return Alloc(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800152}
153
Orion Hodsoncdf6c492021-11-02 15:35:05 +0000154template<typename T>
155static void RepeatCharacters(ObjPtr<String> new_string, Handle<String> h_this, int32_t count)
156 REQUIRES_SHARED(Locks::mutator_lock_) {
157 T *new_value, *h_this_value;
158 if constexpr (std::is_same_v<T, uint8_t>) {
159 new_value = new_string->GetValueCompressed();
160 h_this_value = h_this->GetValueCompressed();
161 } else {
162 new_value = new_string->GetValue();
163 h_this_value = h_this->GetValue();
164 }
165 int32_t length_this = h_this->GetLength();
166 if (length_this == 1) {
167 // compiler is smart enough to use memset for uint8_t
168 std::fill(new_value, new_value + count, h_this_value[0]);
169 } else {
170 memcpy(new_value, h_this_value, length_this * sizeof(T));
171 int32_t copied = length_this;
172 int32_t limit = length_this * count;
173 for (; copied < limit - copied; copied <<= 1) {
174 memcpy(new_value + copied, new_value, copied * sizeof(T));
175 }
176 memcpy(new_value + copied, new_value, (limit - copied) * sizeof(T));
177 }
178}
179
180ObjPtr<String> String::DoRepeat(Thread* self, Handle<String> h_this, int32_t count) {
181 int32_t length_this = h_this->GetLength();
182 DCHECK_GT(count, 1);
183 DCHECK_LE(length_this, std::numeric_limits<int32_t>::max() / count);
184 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
185 const bool compressible = kUseStringCompression && (h_this->IsCompressed());
186 const int32_t length_with_flag = String::GetFlaggedCount(length_this * count, compressible);
187
188 auto visitor = [=](ObjPtr<Object> obj, size_t usable_size) REQUIRES_SHARED(Locks::mutator_lock_) {
189 SetStringCountVisitor set_string_count_visitor(length_with_flag);
190 set_string_count_visitor(obj, usable_size);
191 ObjPtr<String> new_string = obj->AsString();
192
193 if (compressible) {
194 RepeatCharacters<uint8_t>(new_string, h_this, count);
195 } else {
196 RepeatCharacters<uint16_t>(new_string, h_this, count);
197 }
198 };
199 return Alloc(self, length_with_flag, allocator_type, visitor);
200}
201
Vladimir Marko179b7c62019-03-22 13:38:57 +0000202ObjPtr<String> String::AllocFromUtf16(Thread* self,
203 int32_t utf16_length,
204 const uint16_t* utf16_data_in) {
Ian Rogers4069d332014-01-03 10:28:27 -0800205 CHECK(utf16_data_in != nullptr || utf16_length == 0);
Jeff Hao848f70a2014-01-15 13:49:50 -0800206 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700207 const bool compressible = kUseStringCompression &&
208 String::AllASCII<uint16_t>(utf16_data_in, utf16_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100209 int32_t length_with_flag = String::GetFlaggedCount(utf16_length, compressible);
Vladimir Marko76295482020-10-26 12:28:37 +0000210
211 auto visitor = [=](ObjPtr<Object> obj, size_t usable_size) REQUIRES_SHARED(Locks::mutator_lock_) {
212 SetStringCountVisitor set_string_count_visitor(length_with_flag);
213 set_string_count_visitor(obj, usable_size);
214 ObjPtr<String> new_string = obj->AsString();
215 if (compressible) {
216 uint8_t* value = new_string->GetValueCompressed();
217 for (int i = 0; i < utf16_length; ++i) {
218 value[i] = static_cast<uint8_t>(utf16_data_in[i]);
219 }
220 } else {
221 memcpy(new_string->GetValue(), utf16_data_in, utf16_length * sizeof(uint16_t));
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700222 }
Vladimir Marko76295482020-10-26 12:28:37 +0000223 };
224 return Alloc(self, length_with_flag, allocator_type, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225}
226
Vladimir Marko179b7c62019-03-22 13:38:57 +0000227ObjPtr<String> String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700228 DCHECK(utf != nullptr);
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300229 size_t byte_count = strlen(utf);
230 size_t char_count = CountModifiedUtf8Chars(utf, byte_count);
231 return AllocFromModifiedUtf8(self, char_count, utf, byte_count);
232}
233
Vladimir Marko179b7c62019-03-22 13:38:57 +0000234ObjPtr<String> String::AllocFromModifiedUtf8(Thread* self,
235 int32_t utf16_length,
236 const char* utf8_data_in) {
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300237 return AllocFromModifiedUtf8(self, utf16_length, utf8_data_in, strlen(utf8_data_in));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238}
239
Vladimir Marko179b7c62019-03-22 13:38:57 +0000240ObjPtr<String> String::AllocFromModifiedUtf8(Thread* self,
241 int32_t utf16_length,
242 const char* utf8_data_in,
243 int32_t utf8_length) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800244 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700245 const bool compressible = kUseStringCompression && (utf16_length == utf8_length);
Vladimir Marko76295482020-10-26 12:28:37 +0000246 const int32_t length_with_flag = String::GetFlaggedCount(utf16_length, compressible);
247
248 auto visitor = [=](ObjPtr<Object> obj, size_t usable_size) REQUIRES_SHARED(Locks::mutator_lock_) {
249 SetStringCountVisitor set_string_count_visitor(length_with_flag);
250 set_string_count_visitor(obj, usable_size);
251 ObjPtr<String> new_string = obj->AsString();
252 if (compressible) {
253 memcpy(new_string->GetValueCompressed(), utf8_data_in, utf16_length * sizeof(uint8_t));
254 } else {
255 uint16_t* utf16_data_out = new_string->GetValue();
256 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length);
257 }
258 };
259 return Alloc(self, length_with_flag, allocator_type, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260}
261
Mathieu Chartier31e88222016-10-14 18:43:19 -0700262bool String::Equals(ObjPtr<String> that) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 if (this == that) {
264 // Quick reference equality test
265 return true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700266 } else if (that == nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 // Null isn't an instanceof anything
268 return false;
Vladimir Marko76295482020-10-26 12:28:37 +0000269 } else if (this->GetCount() != that->GetCount()) {
270 // Quick length and compression inequality test
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 return false;
272 } else {
273 // Note: don't short circuit on hash code as we're presumably here as the
274 // hash code was already equal
Vladimir Marko76295482020-10-26 12:28:37 +0000275 if (this->IsCompressed()) {
276 return memcmp(this->GetValueCompressed(), that->GetValueCompressed(), this->GetLength()) == 0;
277 } else {
278 return memcmp(this->GetValue(), that->GetValue(), sizeof(uint16_t) * this->GetLength()) == 0;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 }
281}
282
Ian Rogersef7d42f2014-01-06 12:55:46 -0800283bool String::Equals(const char* modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000284 const int32_t length = GetLength();
Vladimir Marko76295482020-10-26 12:28:37 +0000285 if (IsCompressed()) {
286 return strlen(modified_utf8) == dchecked_integral_cast<uint32_t>(length) &&
287 memcmp(modified_utf8, GetValueCompressed(), length) == 0;
288 }
289 const uint16_t* value = GetValue();
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000290 int32_t i = 0;
291 while (i < length) {
292 const uint32_t ch = GetUtf16FromUtf8(&modified_utf8);
293 if (ch == '\0') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 return false;
295 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000296
Vladimir Marko76295482020-10-26 12:28:37 +0000297 if (GetLeadingUtf16Char(ch) != value[i++]) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000298 return false;
299 }
300
301 const uint16_t trailing = GetTrailingUtf16Char(ch);
302 if (trailing != 0) {
303 if (i == length) {
304 return false;
305 }
306
Vladimir Marko76295482020-10-26 12:28:37 +0000307 if (value[i++] != trailing) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000308 return false;
309 }
310 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311 }
312 return *modified_utf8 == '\0';
313}
314
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315// Create a modified UTF-8 encoded std::string from a java/lang/String object.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800316std::string String::ToModifiedUtf8() {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700317 if (IsCompressed()) {
Vladimir Marko76295482020-10-26 12:28:37 +0000318 return std::string(reinterpret_cast<const char*>(GetValueCompressed()), GetLength());
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700319 } else {
Vladimir Marko76295482020-10-26 12:28:37 +0000320 size_t byte_count = GetUtfLength();
321 std::string result(byte_count, static_cast<char>(0));
322 ConvertUtf16ToModifiedUtf8(&result[0], byte_count, GetValue(), GetLength());
323 return result;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700324 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800325}
326
Mathieu Chartier31e88222016-10-14 18:43:19 -0700327int32_t String::CompareTo(ObjPtr<String> rhs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 // Quick test for comparison of a string with itself.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700329 ObjPtr<String> lhs = this;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 if (lhs == rhs) {
331 return 0;
332 }
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100333 int32_t lhs_count = lhs->GetLength();
334 int32_t rhs_count = rhs->GetLength();
335 int32_t count_diff = lhs_count - rhs_count;
336 int32_t min_count = (count_diff < 0) ? lhs_count : rhs_count;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700337 if (lhs->IsCompressed() && rhs->IsCompressed()) {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100338 const uint8_t* lhs_chars = lhs->GetValueCompressed();
339 const uint8_t* rhs_chars = rhs->GetValueCompressed();
340 for (int32_t i = 0; i < min_count; ++i) {
341 int32_t char_diff = static_cast<int32_t>(lhs_chars[i]) - static_cast<int32_t>(rhs_chars[i]);
342 if (char_diff != 0) {
343 return char_diff;
344 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700345 }
346 } else if (lhs->IsCompressed() || rhs->IsCompressed()) {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100347 const uint8_t* compressed_chars =
348 lhs->IsCompressed() ? lhs->GetValueCompressed() : rhs->GetValueCompressed();
349 const uint16_t* uncompressed_chars = lhs->IsCompressed() ? rhs->GetValue() : lhs->GetValue();
350 for (int32_t i = 0; i < min_count; ++i) {
351 int32_t char_diff =
352 static_cast<int32_t>(compressed_chars[i]) - static_cast<int32_t>(uncompressed_chars[i]);
353 if (char_diff != 0) {
354 return lhs->IsCompressed() ? char_diff : -char_diff;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700355 }
356 }
357 } else {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100358 const uint16_t* lhs_chars = lhs->GetValue();
359 const uint16_t* rhs_chars = rhs->GetValue();
360 // FIXME: The MemCmp16() name is misleading. It returns the char difference on mismatch
361 // where memcmp() only guarantees that the returned value has the same sign.
362 int32_t char_diff = MemCmp16(lhs_chars, rhs_chars, min_count);
363 if (char_diff != 0) {
364 return char_diff;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700365 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 }
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100367 return count_diff;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368}
369
Vladimir Marko3068d582019-05-28 16:39:29 +0100370ObjPtr<CharArray> String::ToCharArray(Handle<String> h_this, Thread* self) {
371 ObjPtr<CharArray> result = CharArray::Alloc(self, h_this->GetLength());
Mathieu Chartier04e983a2015-11-13 08:36:59 -0800372 if (result != nullptr) {
Vladimir Marko3068d582019-05-28 16:39:29 +0100373 if (h_this->IsCompressed()) {
374 int32_t length = h_this->GetLength();
Vladimir Marko76295482020-10-26 12:28:37 +0000375 const uint8_t* src = h_this->GetValueCompressed();
376 uint16_t* dest = result->GetData();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700377 for (int i = 0; i < length; ++i) {
Vladimir Marko76295482020-10-26 12:28:37 +0000378 dest[i] = src[i];
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700379 }
380 } else {
Vladimir Marko3068d582019-05-28 16:39:29 +0100381 memcpy(result->GetData(), h_this->GetValue(), h_this->GetLength() * sizeof(uint16_t));
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700382 }
Mathieu Chartier04e983a2015-11-13 08:36:59 -0800383 } else {
384 self->AssertPendingOOMException();
385 }
Vladimir Marko179b7c62019-03-22 13:38:57 +0000386 return result;
Jeff Hao848f70a2014-01-15 13:49:50 -0800387}
388
389void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) {
390 uint16_t* data = array->GetData() + index;
Vladimir Marko76295482020-10-26 12:28:37 +0000391 DCHECK_LE(start, end);
392 int32_t length = end - start;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700393 if (IsCompressed()) {
Vladimir Marko76295482020-10-26 12:28:37 +0000394 const uint8_t* value = GetValueCompressed() + start;
395 for (int i = 0; i < length; ++i) {
396 data[i] = value[i];
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700397 }
398 } else {
399 uint16_t* value = GetValue() + start;
Vladimir Marko76295482020-10-26 12:28:37 +0000400 memcpy(data, value, length * sizeof(uint16_t));
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700401 }
402}
403
404bool String::IsValueNull() {
405 return (IsCompressed()) ? (GetValueCompressed() == nullptr) : (GetValue() == nullptr);
Jeff Hao848f70a2014-01-15 13:49:50 -0800406}
407
David Sehr709b0702016-10-13 09:12:37 -0700408std::string String::PrettyStringDescriptor(ObjPtr<mirror::String> java_descriptor) {
409 if (java_descriptor == nullptr) {
410 return "null";
411 }
412 return java_descriptor->PrettyStringDescriptor();
413}
414
415std::string String::PrettyStringDescriptor() {
416 return PrettyDescriptor(ToModifiedUtf8().c_str());
417}
418
Andreas Gampeb2d18fa2017-06-06 20:46:10 -0700419ObjPtr<String> String::Intern() {
420 return Runtime::Current()->GetInternTable()->InternWeak(this);
421}
422
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423} // namespace mirror
424} // namespace art