blob: 82ff6ddead2f0a327cf2063bb62c3d2525b2050b [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
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070017#include "string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018
Andreas Gampe4d0589c2014-06-10 16:10:56 -070019#include "arch/memcmp16.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "array.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"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "gc/accounting/card_table-inl.h"
Andreas Gampe508fdf32017-06-05 16:42:13 -070025#include "gc_root-inl.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080026#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "intern_table.h"
28#include "object-inl.h"
29#include "runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080030#include "string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "thread.h"
Ian Rogersa6724902013-09-23 09:23:37 -070032#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033
34namespace art {
35namespace mirror {
36
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070037// TODO: get global references for these
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070038GcRoot<Class> String::java_lang_String_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039
Ian Rogersef7d42f2014-01-06 12:55:46 -080040int32_t String::FastIndexOf(int32_t ch, int32_t start) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041 int32_t count = GetLength();
42 if (start < 0) {
43 start = 0;
44 } else if (start > count) {
45 start = count;
46 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -070047 if (IsCompressed()) {
48 return FastIndexOf<uint8_t>(GetValueCompressed(), ch, start);
49 } else {
50 return FastIndexOf<uint16_t>(GetValue(), ch, start);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
Mathieu Chartier31e88222016-10-14 18:43:19 -070054void String::SetClass(ObjPtr<Class> java_lang_String) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070055 CHECK(java_lang_String_.IsNull());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070056 CHECK(java_lang_String != nullptr);
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -070057 CHECK(java_lang_String->IsStringClass());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070058 java_lang_String_ = GcRoot<Class>(java_lang_String);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059}
60
61void String::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070062 CHECK(!java_lang_String_.IsNull());
63 java_lang_String_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064}
65
Jeff Hao848f70a2014-01-15 13:49:50 -080066int String::ComputeHashCode() {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070067 int32_t hash_code = 0;
68 if (IsCompressed()) {
69 hash_code = ComputeUtf16Hash(GetValueCompressed(), GetLength());
70 } else {
71 hash_code = ComputeUtf16Hash(GetValue(), GetLength());
72 }
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070073 SetHashCode(hash_code);
74 return hash_code;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075}
76
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070077int32_t String::GetUtfLength() {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070078 if (IsCompressed()) {
79 return GetLength();
80 } else {
81 return CountUtf8Bytes(GetValue(), GetLength());
82 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083}
84
Vladimir Marko92907f32017-02-20 14:08:30 +000085inline bool String::AllASCIIExcept(const uint16_t* chars, int32_t length, uint16_t non_ascii) {
86 DCHECK(!IsASCII(non_ascii));
87 for (int32_t i = 0; i < length; ++i) {
88 if (!IsASCII(chars[i]) && chars[i] != non_ascii) {
89 return false;
90 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -070091 }
Vladimir Marko92907f32017-02-20 14:08:30 +000092 return true;
93}
94
Vladimir Marko9e57aba2017-03-16 10:45:40 +000095ObjPtr<String> String::DoReplace(Thread* self, Handle<String> src, uint16_t old_c, uint16_t new_c) {
96 int32_t length = src->GetLength();
97 DCHECK(src->IsCompressed()
98 ? ContainsElement(ArrayRef<uint8_t>(src->value_compressed_, length), old_c)
99 : ContainsElement(ArrayRef<uint16_t>(src->value_, length), old_c));
Vladimir Marko92907f32017-02-20 14:08:30 +0000100 bool compressible =
101 kUseStringCompression &&
102 IsASCII(new_c) &&
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000103 (src->IsCompressed() || (!IsASCII(old_c) && AllASCIIExcept(src->value_, length, old_c)));
Vladimir Marko92907f32017-02-20 14:08:30 +0000104 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000105 const int32_t length_with_flag = String::GetFlaggedCount(length, compressible);
Vladimir Marko92907f32017-02-20 14:08:30 +0000106 SetStringCountVisitor visitor(length_with_flag);
107 ObjPtr<String> string = Alloc<true>(self, length_with_flag, allocator_type, visitor);
108 if (UNLIKELY(string == nullptr)) {
109 return nullptr;
110 }
111 if (compressible) {
112 auto replace = [old_c, new_c](uint16_t c) {
113 return dchecked_integral_cast<uint8_t>((old_c != c) ? c : new_c);
114 };
115 uint8_t* out = string->value_compressed_;
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000116 if (LIKELY(src->IsCompressed())) { // LIKELY(compressible == src->IsCompressed())
117 std::transform(src->value_compressed_, src->value_compressed_ + length, out, replace);
Vladimir Marko92907f32017-02-20 14:08:30 +0000118 } else {
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000119 std::transform(src->value_, src->value_ + length, out, replace);
Vladimir Marko92907f32017-02-20 14:08:30 +0000120 }
121 DCHECK(kUseStringCompression && AllASCII(out, length));
122 } else {
123 auto replace = [old_c, new_c](uint16_t c) {
124 return (old_c != c) ? c : new_c;
125 };
126 uint16_t* out = string->value_;
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000127 if (UNLIKELY(src->IsCompressed())) { // LIKELY(compressible == src->IsCompressed())
128 std::transform(src->value_compressed_, src->value_compressed_ + length, out, replace);
Vladimir Marko92907f32017-02-20 14:08:30 +0000129 } else {
Vladimir Marko9e57aba2017-03-16 10:45:40 +0000130 std::transform(src->value_, src->value_ + length, out, replace);
Vladimir Marko92907f32017-02-20 14:08:30 +0000131 }
132 DCHECK(!kUseStringCompression || !AllASCII(out, length));
133 }
134 return string;
Jeff Hao848f70a2014-01-15 13:49:50 -0800135}
136
137String* String::AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2) {
138 int32_t length = string->GetLength();
139 int32_t length2 = string2->GetLength();
140 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
Mathieu Chartier31e88222016-10-14 18:43:19 -0700141 const bool compressible = kUseStringCompression &&
142 (string->IsCompressed() && string2->IsCompressed());
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100143 const int32_t length_with_flag = String::GetFlaggedCount(length + length2, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700144
145 SetStringCountVisitor visitor(length_with_flag);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700146 ObjPtr<String> new_string = Alloc<true>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800147 if (UNLIKELY(new_string == nullptr)) {
148 return nullptr;
149 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700150 if (compressible) {
151 uint8_t* new_value = new_string->GetValueCompressed();
152 memcpy(new_value, string->GetValueCompressed(), length * sizeof(uint8_t));
153 memcpy(new_value + length, string2->GetValueCompressed(), length2 * sizeof(uint8_t));
154 } else {
155 uint16_t* new_value = new_string->GetValue();
156 if (string->IsCompressed()) {
157 for (int i = 0; i < length; ++i) {
158 new_value[i] = string->CharAt(i);
159 }
160 } else {
161 memcpy(new_value, string->GetValue(), length * sizeof(uint16_t));
162 }
163 if (string2->IsCompressed()) {
164 for (int i = 0; i < length2; ++i) {
165 new_value[i+length] = string2->CharAt(i);
166 }
167 } else {
168 memcpy(new_value + length, string2->GetValue(), length2 * sizeof(uint16_t));
169 }
170 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700171 return new_string.Ptr();
Jeff Hao848f70a2014-01-15 13:49:50 -0800172}
173
174String* String::AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in) {
Ian Rogers4069d332014-01-03 10:28:27 -0800175 CHECK(utf16_data_in != nullptr || utf16_length == 0);
Jeff Hao848f70a2014-01-15 13:49:50 -0800176 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700177 const bool compressible = kUseStringCompression &&
178 String::AllASCII<uint16_t>(utf16_data_in, utf16_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100179 int32_t length_with_flag = String::GetFlaggedCount(utf16_length, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700180 SetStringCountVisitor visitor(length_with_flag);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700181 ObjPtr<String> string = Alloc<true>(self, length_with_flag, allocator_type, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700182 if (UNLIKELY(string == nullptr)) {
183 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700185 if (compressible) {
186 for (int i = 0; i < utf16_length; ++i) {
187 string->GetValueCompressed()[i] = static_cast<uint8_t>(utf16_data_in[i]);
188 }
189 } else {
190 uint16_t* array = string->GetValue();
191 memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t));
192 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700193 return string.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194}
195
Ian Rogersa436fde2013-08-27 23:34:06 -0700196String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700197 DCHECK(utf != nullptr);
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300198 size_t byte_count = strlen(utf);
199 size_t char_count = CountModifiedUtf8Chars(utf, byte_count);
200 return AllocFromModifiedUtf8(self, char_count, utf, byte_count);
201}
202
Mathieu Chartier31e88222016-10-14 18:43:19 -0700203String* String::AllocFromModifiedUtf8(Thread* self,
204 int32_t utf16_length,
205 const char* utf8_data_in) {
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300206 return AllocFromModifiedUtf8(self, utf16_length, utf8_data_in, strlen(utf8_data_in));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207}
208
Mathieu Chartier31e88222016-10-14 18:43:19 -0700209String* String::AllocFromModifiedUtf8(Thread* self,
210 int32_t utf16_length,
211 const char* utf8_data_in,
212 int32_t utf8_length) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800213 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700214 const bool compressible = kUseStringCompression && (utf16_length == utf8_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100215 const int32_t utf16_length_with_flag = String::GetFlaggedCount(utf16_length, compressible);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700216 SetStringCountVisitor visitor(utf16_length_with_flag);
Mathieu Chartier31e88222016-10-14 18:43:19 -0700217 ObjPtr<String> string = Alloc<true>(self, utf16_length_with_flag, allocator_type, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700218 if (UNLIKELY(string == nullptr)) {
219 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700221 if (compressible) {
222 memcpy(string->GetValueCompressed(), utf8_data_in, utf16_length * sizeof(uint8_t));
223 } else {
224 uint16_t* utf16_data_out = string->GetValue();
225 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length);
226 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700227 return string.Ptr();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228}
229
Mathieu Chartier31e88222016-10-14 18:43:19 -0700230bool String::Equals(ObjPtr<String> that) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 if (this == that) {
232 // Quick reference equality test
233 return true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700234 } else if (that == nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 // Null isn't an instanceof anything
236 return false;
237 } else if (this->GetLength() != that->GetLength()) {
238 // Quick length inequality test
239 return false;
240 } else {
241 // Note: don't short circuit on hash code as we're presumably here as the
242 // hash code was already equal
243 for (int32_t i = 0; i < that->GetLength(); ++i) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800244 if (this->CharAt(i) != that->CharAt(i)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 return false;
246 }
247 }
248 return true;
249 }
250}
251
Ian Rogersef7d42f2014-01-06 12:55:46 -0800252bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253 if (this->GetLength() != that_length) {
254 return false;
255 } else {
256 for (int32_t i = 0; i < that_length; ++i) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800257 if (this->CharAt(i) != that_chars[that_offset + i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800258 return false;
259 }
260 }
261 return true;
262 }
263}
264
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265bool String::Equals(const char* modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000266 const int32_t length = GetLength();
267 int32_t i = 0;
268 while (i < length) {
269 const uint32_t ch = GetUtf16FromUtf8(&modified_utf8);
270 if (ch == '\0') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 return false;
272 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000273
Jeff Hao848f70a2014-01-15 13:49:50 -0800274 if (GetLeadingUtf16Char(ch) != CharAt(i++)) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000275 return false;
276 }
277
278 const uint16_t trailing = GetTrailingUtf16Char(ch);
279 if (trailing != 0) {
280 if (i == length) {
281 return false;
282 }
283
Jeff Hao848f70a2014-01-15 13:49:50 -0800284 if (CharAt(i++) != trailing) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000285 return false;
286 }
287 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800288 }
289 return *modified_utf8 == '\0';
290}
291
Ian Rogersef7d42f2014-01-06 12:55:46 -0800292bool String::Equals(const StringPiece& modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000293 const int32_t length = GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 const char* p = modified_utf8.data();
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000295 for (int32_t i = 0; i < length; ++i) {
296 uint32_t ch = GetUtf16FromUtf8(&p);
297
Jeff Hao848f70a2014-01-15 13:49:50 -0800298 if (GetLeadingUtf16Char(ch) != CharAt(i)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 return false;
300 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000301
302 const uint16_t trailing = GetTrailingUtf16Char(ch);
303 if (trailing != 0) {
304 if (i == (length - 1)) {
305 return false;
306 }
307
Jeff Hao848f70a2014-01-15 13:49:50 -0800308 if (CharAt(++i) != trailing) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000309 return false;
310 }
311 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 }
313 return true;
314}
315
316// Create a modified UTF-8 encoded std::string from a java/lang/String object.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800317std::string String::ToModifiedUtf8() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 size_t byte_count = GetUtfLength();
319 std::string result(byte_count, static_cast<char>(0));
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700320 if (IsCompressed()) {
321 for (size_t i = 0; i < byte_count; ++i) {
322 result[i] = static_cast<char>(CharAt(i));
323 }
324 } else {
325 const uint16_t* chars = GetValue();
326 ConvertUtf16ToModifiedUtf8(&result[0], byte_count, chars, GetLength());
327 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 return result;
329}
330
Mathieu Chartier31e88222016-10-14 18:43:19 -0700331int32_t String::CompareTo(ObjPtr<String> rhs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 // Quick test for comparison of a string with itself.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700333 ObjPtr<String> lhs = this;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 if (lhs == rhs) {
335 return 0;
336 }
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100337 int32_t lhs_count = lhs->GetLength();
338 int32_t rhs_count = rhs->GetLength();
339 int32_t count_diff = lhs_count - rhs_count;
340 int32_t min_count = (count_diff < 0) ? lhs_count : rhs_count;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700341 if (lhs->IsCompressed() && rhs->IsCompressed()) {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100342 const uint8_t* lhs_chars = lhs->GetValueCompressed();
343 const uint8_t* rhs_chars = rhs->GetValueCompressed();
344 for (int32_t i = 0; i < min_count; ++i) {
345 int32_t char_diff = static_cast<int32_t>(lhs_chars[i]) - static_cast<int32_t>(rhs_chars[i]);
346 if (char_diff != 0) {
347 return char_diff;
348 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700349 }
350 } else if (lhs->IsCompressed() || rhs->IsCompressed()) {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100351 const uint8_t* compressed_chars =
352 lhs->IsCompressed() ? lhs->GetValueCompressed() : rhs->GetValueCompressed();
353 const uint16_t* uncompressed_chars = lhs->IsCompressed() ? rhs->GetValue() : lhs->GetValue();
354 for (int32_t i = 0; i < min_count; ++i) {
355 int32_t char_diff =
356 static_cast<int32_t>(compressed_chars[i]) - static_cast<int32_t>(uncompressed_chars[i]);
357 if (char_diff != 0) {
358 return lhs->IsCompressed() ? char_diff : -char_diff;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700359 }
360 }
361 } else {
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100362 const uint16_t* lhs_chars = lhs->GetValue();
363 const uint16_t* rhs_chars = rhs->GetValue();
364 // FIXME: The MemCmp16() name is misleading. It returns the char difference on mismatch
365 // where memcmp() only guarantees that the returned value has the same sign.
366 int32_t char_diff = MemCmp16(lhs_chars, rhs_chars, min_count);
367 if (char_diff != 0) {
368 return char_diff;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700369 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 }
Vladimir Marko9c9883b2016-10-17 14:45:29 +0100371 return count_diff;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372}
373
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700374void String::VisitRoots(RootVisitor* visitor) {
375 java_lang_String_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800376}
377
Jeff Hao848f70a2014-01-15 13:49:50 -0800378CharArray* String::ToCharArray(Thread* self) {
379 StackHandleScope<1> hs(self);
380 Handle<String> string(hs.NewHandle(this));
Mathieu Chartier31e88222016-10-14 18:43:19 -0700381 ObjPtr<CharArray> result = CharArray::Alloc(self, GetLength());
Mathieu Chartier04e983a2015-11-13 08:36:59 -0800382 if (result != nullptr) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700383 if (string->IsCompressed()) {
384 int32_t length = string->GetLength();
385 for (int i = 0; i < length; ++i) {
386 result->GetData()[i] = string->CharAt(i);
387 }
388 } else {
389 memcpy(result->GetData(), string->GetValue(), string->GetLength() * sizeof(uint16_t));
390 }
Mathieu Chartier04e983a2015-11-13 08:36:59 -0800391 } else {
392 self->AssertPendingOOMException();
393 }
Mathieu Chartier31e88222016-10-14 18:43:19 -0700394 return result.Ptr();
Jeff Hao848f70a2014-01-15 13:49:50 -0800395}
396
397void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) {
398 uint16_t* data = array->GetData() + index;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700399 if (IsCompressed()) {
400 for (int i = start; i < end; ++i) {
401 data[i-start] = CharAt(i);
402 }
403 } else {
404 uint16_t* value = GetValue() + start;
405 memcpy(data, value, (end - start) * sizeof(uint16_t));
406 }
407}
408
409bool String::IsValueNull() {
410 return (IsCompressed()) ? (GetValueCompressed() == nullptr) : (GetValue() == nullptr);
Jeff Hao848f70a2014-01-15 13:49:50 -0800411}
412
David Sehr709b0702016-10-13 09:12:37 -0700413std::string String::PrettyStringDescriptor(ObjPtr<mirror::String> java_descriptor) {
414 if (java_descriptor == nullptr) {
415 return "null";
416 }
417 return java_descriptor->PrettyStringDescriptor();
418}
419
420std::string String::PrettyStringDescriptor() {
421 return PrettyDescriptor(ToModifiedUtf8().c_str());
422}
423
Andreas Gampeb2d18fa2017-06-06 20:46:10 -0700424ObjPtr<String> String::Intern() {
425 return Runtime::Current()->GetInternTable()->InternWeak(this);
426}
427
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800428} // namespace mirror
429} // namespace art