blob: 1f756a1e53c65f6b040ed0fb787c220bbc2df2c4 [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
32const CharArray* String::GetCharArray() const {
33 return GetFieldObject<const CharArray*>(ValueOffset(), false);
34}
35
Mathieu Chartier423d2a32013-09-12 17:33:56 -070036CharArray* String::GetCharArray() {
37 return GetFieldObject<CharArray*>(ValueOffset(), false);
38}
39
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040void String::ComputeHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
41 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
42}
43
44int32_t String::GetUtfLength() const {
45 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
46}
47
48int32_t String::FastIndexOf(int32_t ch, int32_t start) const {
49 int32_t count = GetLength();
50 if (start < 0) {
51 start = 0;
52 } else if (start > count) {
53 start = count;
54 }
55 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
56 const uint16_t* p = chars + start;
57 const uint16_t* end = chars + count;
58 while (p < end) {
59 if (*p++ == ch) {
60 return (p - 1) - chars;
61 }
62 }
63 return -1;
64}
65
66void String::SetArray(CharArray* new_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
67 DCHECK(new_array != NULL);
68 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
69}
70
71// TODO: get global references for these
72Class* String::java_lang_String_ = NULL;
73
74void String::SetClass(Class* java_lang_String) {
75 CHECK(java_lang_String_ == NULL);
76 CHECK(java_lang_String != NULL);
77 java_lang_String_ = java_lang_String;
78}
79
80void String::ResetClass() {
81 CHECK(java_lang_String_ != NULL);
82 java_lang_String_ = NULL;
83}
84
85String* String::Intern() {
86 return Runtime::Current()->GetInternTable()->InternWeak(this);
87}
88
89int32_t String::GetHashCode() {
90 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
91 if (result == 0) {
92 ComputeHashCode();
93 }
94 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
95 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
96 << ToModifiedUtf8() << " " << result;
97 return result;
98}
99
100int32_t String::GetLength() const {
101 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
102 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
103 return result;
104}
105
106uint16_t String::CharAt(int32_t index) const {
107 // TODO: do we need this? Equals is the only caller, and could
108 // bounds check itself.
Sebastien Hertz8a946522013-08-02 09:52:08 +0200109 DCHECK_GE(count_, 0); // ensures the unsigned comparison is safe.
110 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(count_))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800112 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
113 self->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
114 "length=%i; index=%i", count_, index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 return 0;
116 }
117 return GetCharArray()->Get(index + GetOffset());
118}
119
120String* String::AllocFromUtf16(Thread* self,
121 int32_t utf16_length,
122 const uint16_t* utf16_data_in,
123 int32_t hash_code) {
Ian Rogers4069d332014-01-03 10:28:27 -0800124 CHECK(utf16_data_in != nullptr || utf16_length == 0);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800125 String* string = Alloc(self, utf16_length);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700126 if (UNLIKELY(string == nullptr)) {
127 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Ian Rogers4069d332014-01-03 10:28:27 -0800130 if (UNLIKELY(array == nullptr)) {
131 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132 }
Ian Rogers4069d332014-01-03 10:28:27 -0800133 memcpy(array->GetData(), utf16_data_in, utf16_length * sizeof(uint16_t));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 if (hash_code != 0) {
Ian Rogers4069d332014-01-03 10:28:27 -0800135 DCHECK_EQ(hash_code, ComputeUtf16Hash(utf16_data_in, utf16_length));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 string->SetHashCode(hash_code);
137 } else {
138 string->ComputeHashCode();
139 }
140 return string;
141}
142
Ian Rogersa436fde2013-08-27 23:34:06 -0700143String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700144 if (UNLIKELY(utf == nullptr)) {
145 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 }
147 size_t char_count = CountModifiedUtf8Chars(utf);
148 return AllocFromModifiedUtf8(self, char_count, utf);
149}
150
151String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
152 const char* utf8_data_in) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800153 String* string = Alloc(self, utf16_length);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700154 if (UNLIKELY(string == nullptr)) {
155 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 }
157 uint16_t* utf16_data_out =
158 const_cast<uint16_t*>(string->GetCharArray()->GetData());
159 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
160 string->ComputeHashCode();
161 return string;
162}
163
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800164String* String::Alloc(Thread* self, int32_t utf16_length) {
165 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length));
166 if (UNLIKELY(array.get() == nullptr)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700167 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800169 return Alloc(self, array);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170}
171
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800172String* String::Alloc(Thread* self, const SirtRef<CharArray>& array) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 // Hold reference in case AllocObject causes GC.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800174 String* string = down_cast<String*>(GetJavaLangString()->AllocObject(self));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700175 if (LIKELY(string != nullptr)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800176 string->SetArray(array.get());
177 string->SetCount(array->GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 return string;
180}
181
182bool String::Equals(const String* that) const {
183 if (this == that) {
184 // Quick reference equality test
185 return true;
186 } else if (that == NULL) {
187 // Null isn't an instanceof anything
188 return false;
189 } else if (this->GetLength() != that->GetLength()) {
190 // Quick length inequality test
191 return false;
192 } else {
193 // Note: don't short circuit on hash code as we're presumably here as the
194 // hash code was already equal
195 for (int32_t i = 0; i < that->GetLength(); ++i) {
196 if (this->CharAt(i) != that->CharAt(i)) {
197 return false;
198 }
199 }
200 return true;
201 }
202}
203
204bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
205 if (this->GetLength() != that_length) {
206 return false;
207 } else {
208 for (int32_t i = 0; i < that_length; ++i) {
209 if (this->CharAt(i) != that_chars[that_offset + i]) {
210 return false;
211 }
212 }
213 return true;
214 }
215}
216
217bool String::Equals(const char* modified_utf8) const {
218 for (int32_t i = 0; i < GetLength(); ++i) {
219 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
220 if (ch == '\0' || ch != CharAt(i)) {
221 return false;
222 }
223 }
224 return *modified_utf8 == '\0';
225}
226
227bool String::Equals(const StringPiece& modified_utf8) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 const char* p = modified_utf8.data();
229 for (int32_t i = 0; i < GetLength(); ++i) {
230 uint16_t ch = GetUtf16FromUtf8(&p);
231 if (ch != CharAt(i)) {
232 return false;
233 }
234 }
235 return true;
236}
237
238// Create a modified UTF-8 encoded std::string from a java/lang/String object.
239std::string String::ToModifiedUtf8() const {
240 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
241 size_t byte_count = GetUtfLength();
242 std::string result(byte_count, static_cast<char>(0));
243 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
244 return result;
245}
246
247#ifdef HAVE__MEMCMP16
248// "count" is in 16-bit units.
249extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count);
250#define MemCmp16 __memcmp16
251#else
252static uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) {
253 for (size_t i = 0; i < count; i++) {
254 if (s0[i] != s1[i]) {
255 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
256 }
257 }
258 return 0;
259}
260#endif
261
262int32_t String::CompareTo(String* rhs) const {
263 // Quick test for comparison of a string with itself.
264 const String* lhs = this;
265 if (lhs == rhs) {
266 return 0;
267 }
268 // TODO: is this still true?
269 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
270 // because the interpreter converts the characters to 32-bit integers
271 // *without* sign extension before it subtracts them (which makes some
272 // sense since "char" is unsigned). So what we get is the result of
273 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
274 int lhsCount = lhs->GetLength();
275 int rhsCount = rhs->GetLength();
276 int countDiff = lhsCount - rhsCount;
277 int minCount = (countDiff < 0) ? lhsCount : rhsCount;
278 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
279 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
280 int otherRes = MemCmp16(lhsChars, rhsChars, minCount);
281 if (otherRes != 0) {
282 return otherRes;
283 }
284 return countDiff;
285}
286
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800287void String::VisitRoots(RootVisitor* visitor, void* arg) {
288 if (java_lang_String_ != nullptr) {
289 java_lang_String_ = down_cast<Class*>(visitor(java_lang_String_, arg));
290 }
291}
292
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293} // namespace mirror
294} // namespace art