blob: 45610dccc87b61ec7862347adbb81e3a7dd79ca8 [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 }
Jeff Hao848f70a2014-01-15 13:49:50 -080044 const uint16_t* chars = GetValue();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 const uint16_t* p = chars + start;
46 const uint16_t* end = chars + count;
47 while (p < end) {
48 if (*p++ == ch) {
49 return (p - 1) - chars;
50 }
51 }
52 return -1;
53}
54
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055void String::SetClass(Class* java_lang_String) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070056 CHECK(java_lang_String_.IsNull());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070057 CHECK(java_lang_String != nullptr);
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -070058 CHECK(java_lang_String->IsStringClass());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070059 java_lang_String_ = GcRoot<Class>(java_lang_String);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060}
61
62void String::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070063 CHECK(!java_lang_String_.IsNull());
64 java_lang_String_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065}
66
Jeff Hao848f70a2014-01-15 13:49:50 -080067int String::ComputeHashCode() {
68 const int32_t hash_code = ComputeUtf16Hash(GetValue(), GetLength());
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070069 SetHashCode(hash_code);
70 return hash_code;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071}
72
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070073int32_t String::GetUtfLength() {
Jeff Hao848f70a2014-01-15 13:49:50 -080074 return CountUtf8Bytes(GetValue(), GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075}
76
Jeff Hao848f70a2014-01-15 13:49:50 -080077void String::SetCharAt(int32_t index, uint16_t c) {
78 DCHECK((index >= 0) && (index < count_));
79 GetValue()[index] = c;
80}
81
82String* String::AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2) {
83 int32_t length = string->GetLength();
84 int32_t length2 = string2->GetLength();
85 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
86 SetStringCountVisitor visitor(length + length2);
87 String* new_string = Alloc<true>(self, length + length2, allocator_type, visitor);
88 if (UNLIKELY(new_string == nullptr)) {
89 return nullptr;
90 }
91 uint16_t* new_value = new_string->GetValue();
92 memcpy(new_value, string->GetValue(), length * sizeof(uint16_t));
93 memcpy(new_value + length, string2->GetValue(), length2 * sizeof(uint16_t));
94 return new_string;
95}
96
97String* String::AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in) {
Ian Rogers4069d332014-01-03 10:28:27 -080098 CHECK(utf16_data_in != nullptr || utf16_length == 0);
Jeff Hao848f70a2014-01-15 13:49:50 -080099 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
100 SetStringCountVisitor visitor(utf16_length);
101 String* string = Alloc<true>(self, utf16_length, allocator_type, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700102 if (UNLIKELY(string == nullptr)) {
103 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800105 uint16_t* array = string->GetValue();
106 memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 return string;
108}
109
Ian Rogersa436fde2013-08-27 23:34:06 -0700110String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700111 DCHECK(utf != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 size_t char_count = CountModifiedUtf8Chars(utf);
113 return AllocFromModifiedUtf8(self, char_count, utf);
114}
115
116String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
117 const char* utf8_data_in) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800118 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
119 SetStringCountVisitor visitor(utf16_length);
120 String* string = Alloc<true>(self, utf16_length, allocator_type, visitor);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700121 if (UNLIKELY(string == nullptr)) {
122 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 }
Jeff Hao848f70a2014-01-15 13:49:50 -0800124 uint16_t* utf16_data_out = string->GetValue();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 return string;
127}
128
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129bool String::Equals(String* that) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 if (this == that) {
131 // Quick reference equality test
132 return true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700133 } else if (that == nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 // Null isn't an instanceof anything
135 return false;
136 } else if (this->GetLength() != that->GetLength()) {
137 // Quick length inequality test
138 return false;
139 } else {
140 // Note: don't short circuit on hash code as we're presumably here as the
141 // hash code was already equal
142 for (int32_t i = 0; i < that->GetLength(); ++i) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800143 if (this->CharAt(i) != that->CharAt(i)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 return false;
145 }
146 }
147 return true;
148 }
149}
150
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 if (this->GetLength() != that_length) {
153 return false;
154 } else {
155 for (int32_t i = 0; i < that_length; ++i) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800156 if (this->CharAt(i) != that_chars[that_offset + i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 return false;
158 }
159 }
160 return true;
161 }
162}
163
Ian Rogersef7d42f2014-01-06 12:55:46 -0800164bool String::Equals(const char* modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000165 const int32_t length = GetLength();
166 int32_t i = 0;
167 while (i < length) {
168 const uint32_t ch = GetUtf16FromUtf8(&modified_utf8);
169 if (ch == '\0') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 return false;
171 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000172
Jeff Hao848f70a2014-01-15 13:49:50 -0800173 if (GetLeadingUtf16Char(ch) != CharAt(i++)) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000174 return false;
175 }
176
177 const uint16_t trailing = GetTrailingUtf16Char(ch);
178 if (trailing != 0) {
179 if (i == length) {
180 return false;
181 }
182
Jeff Hao848f70a2014-01-15 13:49:50 -0800183 if (CharAt(i++) != trailing) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000184 return false;
185 }
186 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 }
188 return *modified_utf8 == '\0';
189}
190
Ian Rogersef7d42f2014-01-06 12:55:46 -0800191bool String::Equals(const StringPiece& modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000192 const int32_t length = GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 const char* p = modified_utf8.data();
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000194 for (int32_t i = 0; i < length; ++i) {
195 uint32_t ch = GetUtf16FromUtf8(&p);
196
Jeff Hao848f70a2014-01-15 13:49:50 -0800197 if (GetLeadingUtf16Char(ch) != CharAt(i)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 return false;
199 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000200
201 const uint16_t trailing = GetTrailingUtf16Char(ch);
202 if (trailing != 0) {
203 if (i == (length - 1)) {
204 return false;
205 }
206
Jeff Hao848f70a2014-01-15 13:49:50 -0800207 if (CharAt(++i) != trailing) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000208 return false;
209 }
210 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 }
212 return true;
213}
214
215// Create a modified UTF-8 encoded std::string from a java/lang/String object.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800216std::string String::ToModifiedUtf8() {
Jeff Hao848f70a2014-01-15 13:49:50 -0800217 const uint16_t* chars = GetValue();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 size_t byte_count = GetUtfLength();
219 std::string result(byte_count, static_cast<char>(0));
220 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
221 return result;
222}
223
Ian Rogersef7d42f2014-01-06 12:55:46 -0800224int32_t String::CompareTo(String* rhs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 // Quick test for comparison of a string with itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800226 String* lhs = this;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 if (lhs == rhs) {
228 return 0;
229 }
230 // TODO: is this still true?
231 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
232 // because the interpreter converts the characters to 32-bit integers
233 // *without* sign extension before it subtracts them (which makes some
234 // sense since "char" is unsigned). So what we get is the result of
235 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
Andreas Gampe4d0589c2014-06-10 16:10:56 -0700236 int32_t lhsCount = lhs->GetLength();
237 int32_t rhsCount = rhs->GetLength();
238 int32_t countDiff = lhsCount - rhsCount;
239 int32_t minCount = (countDiff < 0) ? lhsCount : rhsCount;
Jeff Hao848f70a2014-01-15 13:49:50 -0800240 const uint16_t* lhsChars = lhs->GetValue();
241 const uint16_t* rhsChars = rhs->GetValue();
Andreas Gampe4d0589c2014-06-10 16:10:56 -0700242 int32_t otherRes = MemCmp16(lhsChars, rhsChars, minCount);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 if (otherRes != 0) {
244 return otherRes;
245 }
246 return countDiff;
247}
248
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700249void String::VisitRoots(RootVisitor* visitor) {
250 java_lang_String_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800251}
252
Jeff Hao848f70a2014-01-15 13:49:50 -0800253CharArray* String::ToCharArray(Thread* self) {
254 StackHandleScope<1> hs(self);
255 Handle<String> string(hs.NewHandle(this));
256 CharArray* result = CharArray::Alloc(self, GetLength());
257 memcpy(result->GetData(), string->GetValue(), string->GetLength() * sizeof(uint16_t));
258 return result;
259}
260
261void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) {
262 uint16_t* data = array->GetData() + index;
263 uint16_t* value = GetValue() + start;
264 memcpy(data, value, (end - start) * sizeof(uint16_t));
265}
266
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267} // namespace mirror
268} // namespace art