blob: cd63c39b1df16e7566799e8c73dfa4687431e527 [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
17#include "string.h"
18
19#include "array.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070020#include "class-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070021#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "intern_table.h"
23#include "object-inl.h"
24#include "runtime.h"
25#include "sirt_ref.h"
26#include "thread.h"
Ian Rogersa6724902013-09-23 09:23:37 -070027#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028
29namespace art {
30namespace mirror {
31
Mathieu Chartier423d2a32013-09-12 17:33:56 -070032CharArray* String::GetCharArray() {
Ian Rogersef7d42f2014-01-06 12:55:46 -080033 return GetFieldObject<CharArray>(ValueOffset(), false);
Mathieu Chartier423d2a32013-09-12 17:33:56 -070034}
35
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010036void String::ComputeHashCode() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
38}
39
Ian Rogersef7d42f2014-01-06 12:55:46 -080040int32_t String::GetUtfLength() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
42}
43
Ian Rogersef7d42f2014-01-06 12:55:46 -080044int32_t String::FastIndexOf(int32_t ch, int32_t start) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 int32_t count = GetLength();
46 if (start < 0) {
47 start = 0;
48 } else if (start > count) {
49 start = count;
50 }
51 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
52 const uint16_t* p = chars + start;
53 const uint16_t* end = chars + count;
54 while (p < end) {
55 if (*p++ == ch) {
56 return (p - 1) - chars;
57 }
58 }
59 return -1;
60}
61
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010062template<bool kTransactionActive>
63void String::SetArray(CharArray* new_array) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 DCHECK(new_array != NULL);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010065 SetFieldObject<kTransactionActive>(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066}
67
68// TODO: get global references for these
69Class* String::java_lang_String_ = NULL;
70
71void String::SetClass(Class* java_lang_String) {
72 CHECK(java_lang_String_ == NULL);
73 CHECK(java_lang_String != NULL);
74 java_lang_String_ = java_lang_String;
75}
76
77void String::ResetClass() {
78 CHECK(java_lang_String_ != NULL);
79 java_lang_String_ = NULL;
80}
81
82String* String::Intern() {
83 return Runtime::Current()->GetInternTable()->InternWeak(this);
84}
85
86int32_t String::GetHashCode() {
87 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
88 if (result == 0) {
89 ComputeHashCode();
90 }
91 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
92 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
93 << ToModifiedUtf8() << " " << result;
94 return result;
95}
96
Ian Rogersef7d42f2014-01-06 12:55:46 -080097int32_t String::GetLength() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
99 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
100 return result;
101}
102
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103uint16_t String::CharAt(int32_t index) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 // TODO: do we need this? Equals is the only caller, and could
105 // bounds check itself.
Sebastien Hertz8a946522013-08-02 09:52:08 +0200106 DCHECK_GE(count_, 0); // ensures the unsigned comparison is safe.
107 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(count_))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
110 self->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
111 "length=%i; index=%i", count_, index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 return 0;
113 }
114 return GetCharArray()->Get(index + GetOffset());
115}
116
117String* String::AllocFromUtf16(Thread* self,
118 int32_t utf16_length,
119 const uint16_t* utf16_data_in,
120 int32_t hash_code) {
Ian Rogers4069d332014-01-03 10:28:27 -0800121 CHECK(utf16_data_in != nullptr || utf16_length == 0);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800122 String* string = Alloc(self, utf16_length);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700123 if (UNLIKELY(string == nullptr)) {
124 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Ian Rogers4069d332014-01-03 10:28:27 -0800127 if (UNLIKELY(array == nullptr)) {
128 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 }
Ian Rogers4069d332014-01-03 10:28:27 -0800130 memcpy(array->GetData(), utf16_data_in, utf16_length * sizeof(uint16_t));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 if (hash_code != 0) {
Ian Rogers4069d332014-01-03 10:28:27 -0800132 DCHECK_EQ(hash_code, ComputeUtf16Hash(utf16_data_in, utf16_length));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 string->SetHashCode(hash_code);
134 } else {
135 string->ComputeHashCode();
136 }
137 return string;
138}
139
Ian Rogersa436fde2013-08-27 23:34:06 -0700140String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700141 if (UNLIKELY(utf == nullptr)) {
142 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 }
144 size_t char_count = CountModifiedUtf8Chars(utf);
145 return AllocFromModifiedUtf8(self, char_count, utf);
146}
147
148String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
149 const char* utf8_data_in) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800150 String* string = Alloc(self, utf16_length);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700151 if (UNLIKELY(string == nullptr)) {
152 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 }
154 uint16_t* utf16_data_out =
155 const_cast<uint16_t*>(string->GetCharArray()->GetData());
156 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
157 string->ComputeHashCode();
158 return string;
159}
160
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800161String* String::Alloc(Thread* self, int32_t utf16_length) {
162 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length));
163 if (UNLIKELY(array.get() == nullptr)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700164 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800166 return Alloc(self, array);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167}
168
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800169String* String::Alloc(Thread* self, const SirtRef<CharArray>& array) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 // Hold reference in case AllocObject causes GC.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800171 String* string = down_cast<String*>(GetJavaLangString()->AllocObject(self));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700172 if (LIKELY(string != nullptr)) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100173 if (Runtime::Current()->IsActiveTransaction()) {
174 string->SetArray<true>(array.get());
175 string->SetCount<true>(array->GetLength());
176 } else {
177 string->SetArray<false>(array.get());
178 string->SetCount<false>(array->GetLength());
179 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181 return string;
182}
183
Ian Rogersef7d42f2014-01-06 12:55:46 -0800184bool String::Equals(String* that) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185 if (this == that) {
186 // Quick reference equality test
187 return true;
188 } else if (that == NULL) {
189 // Null isn't an instanceof anything
190 return false;
191 } else if (this->GetLength() != that->GetLength()) {
192 // Quick length inequality test
193 return false;
194 } else {
195 // Note: don't short circuit on hash code as we're presumably here as the
196 // hash code was already equal
197 for (int32_t i = 0; i < that->GetLength(); ++i) {
198 if (this->CharAt(i) != that->CharAt(i)) {
199 return false;
200 }
201 }
202 return true;
203 }
204}
205
Ian Rogersef7d42f2014-01-06 12:55:46 -0800206bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 if (this->GetLength() != that_length) {
208 return false;
209 } else {
210 for (int32_t i = 0; i < that_length; ++i) {
211 if (this->CharAt(i) != that_chars[that_offset + i]) {
212 return false;
213 }
214 }
215 return true;
216 }
217}
218
Ian Rogersef7d42f2014-01-06 12:55:46 -0800219bool String::Equals(const char* modified_utf8) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 for (int32_t i = 0; i < GetLength(); ++i) {
221 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
222 if (ch == '\0' || ch != CharAt(i)) {
223 return false;
224 }
225 }
226 return *modified_utf8 == '\0';
227}
228
Ian Rogersef7d42f2014-01-06 12:55:46 -0800229bool String::Equals(const StringPiece& modified_utf8) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 const char* p = modified_utf8.data();
231 for (int32_t i = 0; i < GetLength(); ++i) {
232 uint16_t ch = GetUtf16FromUtf8(&p);
233 if (ch != CharAt(i)) {
234 return false;
235 }
236 }
237 return true;
238}
239
240// Create a modified UTF-8 encoded std::string from a java/lang/String object.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800241std::string String::ToModifiedUtf8() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
243 size_t byte_count = GetUtfLength();
244 std::string result(byte_count, static_cast<char>(0));
245 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
246 return result;
247}
248
249#ifdef HAVE__MEMCMP16
250// "count" is in 16-bit units.
251extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count);
252#define MemCmp16 __memcmp16
253#else
254static uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) {
255 for (size_t i = 0; i < count; i++) {
256 if (s0[i] != s1[i]) {
257 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
258 }
259 }
260 return 0;
261}
262#endif
263
Ian Rogersef7d42f2014-01-06 12:55:46 -0800264int32_t String::CompareTo(String* rhs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 // Quick test for comparison of a string with itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800266 String* lhs = this;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 if (lhs == rhs) {
268 return 0;
269 }
270 // TODO: is this still true?
271 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
272 // because the interpreter converts the characters to 32-bit integers
273 // *without* sign extension before it subtracts them (which makes some
274 // sense since "char" is unsigned). So what we get is the result of
275 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
276 int lhsCount = lhs->GetLength();
277 int rhsCount = rhs->GetLength();
278 int countDiff = lhsCount - rhsCount;
279 int minCount = (countDiff < 0) ? lhsCount : rhsCount;
280 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
281 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
282 int otherRes = MemCmp16(lhsChars, rhsChars, minCount);
283 if (otherRes != 0) {
284 return otherRes;
285 }
286 return countDiff;
287}
288
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800289void String::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800290 if (java_lang_String_ != nullptr) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800291 java_lang_String_ = down_cast<Class*>(callback(java_lang_String_, arg, 0, kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800292 }
293}
294
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295} // namespace mirror
296} // namespace art