blob: 46caa4d73f8894b5f7f6798c869872d3737df3e7 [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"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070021#include "class-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "gc/accounting/card_table-inl.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080023#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "intern_table.h"
25#include "object-inl.h"
26#include "runtime.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080027#include "string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "thread.h"
Ian Rogersa6724902013-09-23 09:23:37 -070029#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030
31namespace art {
32namespace mirror {
33
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070034// TODO: get global references for these
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070035GcRoot<Class> String::java_lang_String_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036
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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051void String::SetClass(Class* java_lang_String) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070052 CHECK(java_lang_String_.IsNull());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070053 CHECK(java_lang_String != nullptr);
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -070054 CHECK(java_lang_String->IsStringClass());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070055 java_lang_String_ = GcRoot<Class>(java_lang_String);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056}
57
58void String::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070059 CHECK(!java_lang_String_.IsNull());
60 java_lang_String_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061}
62
Jeff Hao848f70a2014-01-15 13:49:50 -080063int String::ComputeHashCode() {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070064 int32_t hash_code = 0;
65 if (IsCompressed()) {
66 hash_code = ComputeUtf16Hash(GetValueCompressed(), GetLength());
67 } else {
68 hash_code = ComputeUtf16Hash(GetValue(), GetLength());
69 }
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070070 SetHashCode(hash_code);
71 return hash_code;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072}
73
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070074int32_t String::GetUtfLength() {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070075 if (IsCompressed()) {
76 return GetLength();
77 } else {
78 return CountUtf8Bytes(GetValue(), GetLength());
79 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080}
81
Jeff Hao848f70a2014-01-15 13:49:50 -080082void String::SetCharAt(int32_t index, uint16_t c) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -070083 DCHECK((index >= 0) && (index < GetLength()));
84 if (IsCompressed()) {
85 // TODO: Handle the case where String is compressed and c is non-ASCII
86 GetValueCompressed()[index] = static_cast<uint8_t>(c);
87 } else {
88 GetValue()[index] = c;
89 }
Jeff Hao848f70a2014-01-15 13:49:50 -080090}
91
92String* String::AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2) {
93 int32_t length = string->GetLength();
94 int32_t length2 = string2->GetLength();
95 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -070096 const bool compressible = kUseStringCompression && (string->IsCompressed() && string2->IsCompressed());
97 const int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(length + length2)
98 : (length + length2);
99
100 SetStringCountVisitor visitor(length_with_flag);
101 String* new_string = Alloc<true>(self, length_with_flag, allocator_type, visitor);
Jeff Hao848f70a2014-01-15 13:49:50 -0800102 if (UNLIKELY(new_string == nullptr)) {
103 return nullptr;
104 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700105 if (compressible) {
106 uint8_t* new_value = new_string->GetValueCompressed();
107 memcpy(new_value, string->GetValueCompressed(), length * sizeof(uint8_t));
108 memcpy(new_value + length, string2->GetValueCompressed(), length2 * sizeof(uint8_t));
109 } else {
110 uint16_t* new_value = new_string->GetValue();
111 if (string->IsCompressed()) {
112 for (int i = 0; i < length; ++i) {
113 new_value[i] = string->CharAt(i);
114 }
115 } else {
116 memcpy(new_value, string->GetValue(), length * sizeof(uint16_t));
117 }
118 if (string2->IsCompressed()) {
119 for (int i = 0; i < length2; ++i) {
120 new_value[i+length] = string2->CharAt(i);
121 }
122 } else {
123 memcpy(new_value + length, string2->GetValue(), length2 * sizeof(uint16_t));
124 }
125 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800126 return new_string;
127}
128
129String* String::AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in) {
Ian Rogers4069d332014-01-03 10:28:27 -0800130 CHECK(utf16_data_in != nullptr || utf16_length == 0);
Jeff Hao848f70a2014-01-15 13:49:50 -0800131 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700132 const bool compressible = kUseStringCompression &&
133 String::AllASCII<uint16_t>(utf16_data_in, utf16_length);
134 int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(utf16_length)
135 : utf16_length;
136 SetStringCountVisitor visitor(length_with_flag);
137 String* string = Alloc<true>(self, length_with_flag, allocator_type, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700138 if (UNLIKELY(string == nullptr)) {
139 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 }
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700141 if (compressible) {
142 for (int i = 0; i < utf16_length; ++i) {
143 string->GetValueCompressed()[i] = static_cast<uint8_t>(utf16_data_in[i]);
144 }
145 } else {
146 uint16_t* array = string->GetValue();
147 memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t));
148 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 return string;
150}
151
Ian Rogersa436fde2013-08-27 23:34:06 -0700152String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700153 DCHECK(utf != nullptr);
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300154 size_t byte_count = strlen(utf);
155 size_t char_count = CountModifiedUtf8Chars(utf, byte_count);
156 return AllocFromModifiedUtf8(self, char_count, utf, byte_count);
157}
158
159String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, const char* utf8_data_in) {
160 return AllocFromModifiedUtf8(self, utf16_length, utf8_data_in, strlen(utf8_data_in));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161}
162
163String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
Bruce Hoult1646d7a2015-10-28 15:06:12 +0300164 const char* utf8_data_in, int32_t utf8_length) {
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 && (utf16_length == utf8_length);
167 const int32_t utf16_length_with_flag = (compressible) ? String::GetFlaggedCount(utf16_length)
168 : utf16_length;
169 SetStringCountVisitor visitor(utf16_length_with_flag);
170 String* string = Alloc<true>(self, utf16_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 memcpy(string->GetValueCompressed(), utf8_data_in, utf16_length * sizeof(uint8_t));
176 } else {
177 uint16_t* utf16_data_out = string->GetValue();
178 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length);
179 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 return string;
181}
182
Ian Rogersef7d42f2014-01-06 12:55:46 -0800183bool String::Equals(String* that) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 if (this == that) {
185 // Quick reference equality test
186 return true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700187 } else if (that == nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 // Null isn't an instanceof anything
189 return false;
190 } else if (this->GetLength() != that->GetLength()) {
191 // Quick length inequality test
192 return false;
193 } else {
194 // Note: don't short circuit on hash code as we're presumably here as the
195 // hash code was already equal
196 for (int32_t i = 0; i < that->GetLength(); ++i) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800197 if (this->CharAt(i) != that->CharAt(i)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 return false;
199 }
200 }
201 return true;
202 }
203}
204
Ian Rogersef7d42f2014-01-06 12:55:46 -0800205bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206 if (this->GetLength() != that_length) {
207 return false;
208 } else {
209 for (int32_t i = 0; i < that_length; ++i) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800210 if (this->CharAt(i) != that_chars[that_offset + i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 return false;
212 }
213 }
214 return true;
215 }
216}
217
Ian Rogersef7d42f2014-01-06 12:55:46 -0800218bool String::Equals(const char* modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000219 const int32_t length = GetLength();
220 int32_t i = 0;
221 while (i < length) {
222 const uint32_t ch = GetUtf16FromUtf8(&modified_utf8);
223 if (ch == '\0') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 return false;
225 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000226
Jeff Hao848f70a2014-01-15 13:49:50 -0800227 if (GetLeadingUtf16Char(ch) != CharAt(i++)) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000228 return false;
229 }
230
231 const uint16_t trailing = GetTrailingUtf16Char(ch);
232 if (trailing != 0) {
233 if (i == length) {
234 return false;
235 }
236
Jeff Hao848f70a2014-01-15 13:49:50 -0800237 if (CharAt(i++) != trailing) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000238 return false;
239 }
240 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800241 }
242 return *modified_utf8 == '\0';
243}
244
Ian Rogersef7d42f2014-01-06 12:55:46 -0800245bool String::Equals(const StringPiece& modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000246 const int32_t length = GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 const char* p = modified_utf8.data();
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000248 for (int32_t i = 0; i < length; ++i) {
249 uint32_t ch = GetUtf16FromUtf8(&p);
250
Jeff Hao848f70a2014-01-15 13:49:50 -0800251 if (GetLeadingUtf16Char(ch) != CharAt(i)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 return false;
253 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000254
255 const uint16_t trailing = GetTrailingUtf16Char(ch);
256 if (trailing != 0) {
257 if (i == (length - 1)) {
258 return false;
259 }
260
Jeff Hao848f70a2014-01-15 13:49:50 -0800261 if (CharAt(++i) != trailing) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000262 return false;
263 }
264 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 }
266 return true;
267}
268
269// Create a modified UTF-8 encoded std::string from a java/lang/String object.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800270std::string String::ToModifiedUtf8() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 size_t byte_count = GetUtfLength();
272 std::string result(byte_count, static_cast<char>(0));
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700273 if (IsCompressed()) {
274 for (size_t i = 0; i < byte_count; ++i) {
275 result[i] = static_cast<char>(CharAt(i));
276 }
277 } else {
278 const uint16_t* chars = GetValue();
279 ConvertUtf16ToModifiedUtf8(&result[0], byte_count, chars, GetLength());
280 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800281 return result;
282}
283
Ian Rogersef7d42f2014-01-06 12:55:46 -0800284int32_t String::CompareTo(String* rhs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285 // Quick test for comparison of a string with itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800286 String* lhs = this;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 if (lhs == rhs) {
288 return 0;
289 }
290 // TODO: is this still true?
291 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
292 // because the interpreter converts the characters to 32-bit integers
293 // *without* sign extension before it subtracts them (which makes some
294 // sense since "char" is unsigned). So what we get is the result of
295 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
Andreas Gampe4d0589c2014-06-10 16:10:56 -0700296 int32_t lhsCount = lhs->GetLength();
297 int32_t rhsCount = rhs->GetLength();
298 int32_t countDiff = lhsCount - rhsCount;
299 int32_t minCount = (countDiff < 0) ? lhsCount : rhsCount;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700300 if (lhs->IsCompressed() && rhs->IsCompressed()) {
301 int32_t comparison = memcmp(lhs->GetValueCompressed(), rhs->GetValueCompressed(), minCount * sizeof(uint8_t));
302 if (comparison != 0) {
303 return comparison;
304 }
305 } else if (lhs->IsCompressed() || rhs->IsCompressed()) {
306 for (int32_t i = 0; i < minCount; ++i) {
307 if (lhs->CharAt(i) != rhs->CharAt(i)) {
308 return static_cast<int32_t>(lhs->CharAt(i)) - static_cast<int32_t>(rhs->CharAt(i));
309 }
310 }
311 } else {
312 const uint16_t* lhsChars = lhs->GetValue();
313 const uint16_t* rhsChars = rhs->GetValue();
314 int32_t otherRes = MemCmp16(lhsChars, rhsChars, minCount);
315 if (otherRes != 0) {
316 return otherRes;
317 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 }
319 return countDiff;
320}
321
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700322void String::VisitRoots(RootVisitor* visitor) {
323 java_lang_String_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800324}
325
Jeff Hao848f70a2014-01-15 13:49:50 -0800326CharArray* String::ToCharArray(Thread* self) {
327 StackHandleScope<1> hs(self);
328 Handle<String> string(hs.NewHandle(this));
329 CharArray* result = CharArray::Alloc(self, GetLength());
Mathieu Chartier04e983a2015-11-13 08:36:59 -0800330 if (result != nullptr) {
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700331 if (string->IsCompressed()) {
332 int32_t length = string->GetLength();
333 for (int i = 0; i < length; ++i) {
334 result->GetData()[i] = string->CharAt(i);
335 }
336 } else {
337 memcpy(result->GetData(), string->GetValue(), string->GetLength() * sizeof(uint16_t));
338 }
Mathieu Chartier04e983a2015-11-13 08:36:59 -0800339 } else {
340 self->AssertPendingOOMException();
341 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800342 return result;
343}
344
345void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) {
346 uint16_t* data = array->GetData() + index;
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700347 if (IsCompressed()) {
348 for (int i = start; i < end; ++i) {
349 data[i-start] = CharAt(i);
350 }
351 } else {
352 uint16_t* value = GetValue() + start;
353 memcpy(data, value, (end - start) * sizeof(uint16_t));
354 }
355}
356
357bool String::IsValueNull() {
358 return (IsCompressed()) ? (GetValueCompressed() == nullptr) : (GetValue() == nullptr);
Jeff Hao848f70a2014-01-15 13:49:50 -0800359}
360
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361} // namespace mirror
362} // namespace art