blob: 97126cba4c8aa9185c7ddd26b7d0019264ebd9ca [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"
Ian Rogers1d54e732013-05-02 21:10:01 -070020#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "intern_table.h"
22#include "object-inl.h"
23#include "runtime.h"
24#include "sirt_ref.h"
25#include "thread.h"
26#include "utf.h"
27
28namespace art {
29namespace mirror {
30
31const CharArray* String::GetCharArray() const {
32 return GetFieldObject<const CharArray*>(ValueOffset(), false);
33}
34
35void String::ComputeHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
36 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
37}
38
39int32_t String::GetUtfLength() const {
40 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
41}
42
43int32_t String::FastIndexOf(int32_t ch, int32_t start) const {
44 int32_t count = GetLength();
45 if (start < 0) {
46 start = 0;
47 } else if (start > count) {
48 start = count;
49 }
50 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
51 const uint16_t* p = chars + start;
52 const uint16_t* end = chars + count;
53 while (p < end) {
54 if (*p++ == ch) {
55 return (p - 1) - chars;
56 }
57 }
58 return -1;
59}
60
61void String::SetArray(CharArray* new_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
62 DCHECK(new_array != NULL);
63 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
64}
65
66// TODO: get global references for these
67Class* String::java_lang_String_ = NULL;
68
69void String::SetClass(Class* java_lang_String) {
70 CHECK(java_lang_String_ == NULL);
71 CHECK(java_lang_String != NULL);
72 java_lang_String_ = java_lang_String;
73}
74
75void String::ResetClass() {
76 CHECK(java_lang_String_ != NULL);
77 java_lang_String_ = NULL;
78}
79
80String* String::Intern() {
81 return Runtime::Current()->GetInternTable()->InternWeak(this);
82}
83
84int32_t String::GetHashCode() {
85 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
86 if (result == 0) {
87 ComputeHashCode();
88 }
89 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
90 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
91 << ToModifiedUtf8() << " " << result;
92 return result;
93}
94
95int32_t String::GetLength() const {
96 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
97 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
98 return result;
99}
100
101uint16_t String::CharAt(int32_t index) const {
102 // TODO: do we need this? Equals is the only caller, and could
103 // bounds check itself.
104 if (index < 0 || index >= count_) {
105 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -0800106 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
107 self->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
108 "length=%i; index=%i", count_, index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 return 0;
110 }
111 return GetCharArray()->Get(index + GetOffset());
112}
113
114String* String::AllocFromUtf16(Thread* self,
115 int32_t utf16_length,
116 const uint16_t* utf16_data_in,
117 int32_t hash_code) {
118 CHECK(utf16_data_in != NULL || utf16_length == 0);
119 String* string = Alloc(self, GetJavaLangString(), utf16_length);
120 if (string == NULL) {
121 return NULL;
122 }
123 // TODO: use 16-bit wide memset variant
124 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
125 if (array == NULL) {
126 return NULL;
127 }
128 for (int i = 0; i < utf16_length; i++) {
129 array->Set(i, utf16_data_in[i]);
130 }
131 if (hash_code != 0) {
132 string->SetHashCode(hash_code);
133 } else {
134 string->ComputeHashCode();
135 }
136 return string;
137}
138
139 String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
140 if (utf == NULL) {
141 return NULL;
142 }
143 size_t char_count = CountModifiedUtf8Chars(utf);
144 return AllocFromModifiedUtf8(self, char_count, utf);
145}
146
147String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
148 const char* utf8_data_in) {
149 String* string = Alloc(self, GetJavaLangString(), utf16_length);
150 if (string == NULL) {
151 return NULL;
152 }
153 uint16_t* utf16_data_out =
154 const_cast<uint16_t*>(string->GetCharArray()->GetData());
155 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
156 string->ComputeHashCode();
157 return string;
158}
159
160String* String::Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length) {
161 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length));
162 if (array.get() == NULL) {
163 return NULL;
164 }
165 return Alloc(self, java_lang_String, array.get());
166}
167
168String* String::Alloc(Thread* self, Class* java_lang_String, CharArray* array) {
169 // Hold reference in case AllocObject causes GC.
170 SirtRef<CharArray> array_ref(self, array);
171 String* string = down_cast<String*>(java_lang_String->AllocObject(self));
172 if (string == NULL) {
173 return NULL;
174 }
175 string->SetArray(array);
176 string->SetCount(array->GetLength());
177 return string;
178}
179
180bool String::Equals(const String* that) const {
181 if (this == that) {
182 // Quick reference equality test
183 return true;
184 } else if (that == NULL) {
185 // Null isn't an instanceof anything
186 return false;
187 } else if (this->GetLength() != that->GetLength()) {
188 // Quick length inequality test
189 return false;
190 } else {
191 // Note: don't short circuit on hash code as we're presumably here as the
192 // hash code was already equal
193 for (int32_t i = 0; i < that->GetLength(); ++i) {
194 if (this->CharAt(i) != that->CharAt(i)) {
195 return false;
196 }
197 }
198 return true;
199 }
200}
201
202bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
203 if (this->GetLength() != that_length) {
204 return false;
205 } else {
206 for (int32_t i = 0; i < that_length; ++i) {
207 if (this->CharAt(i) != that_chars[that_offset + i]) {
208 return false;
209 }
210 }
211 return true;
212 }
213}
214
215bool String::Equals(const char* modified_utf8) const {
216 for (int32_t i = 0; i < GetLength(); ++i) {
217 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
218 if (ch == '\0' || ch != CharAt(i)) {
219 return false;
220 }
221 }
222 return *modified_utf8 == '\0';
223}
224
225bool String::Equals(const StringPiece& modified_utf8) const {
226 if (modified_utf8.size() != GetLength()) {
227 return false;
228 }
229 const char* p = modified_utf8.data();
230 for (int32_t i = 0; i < GetLength(); ++i) {
231 uint16_t ch = GetUtf16FromUtf8(&p);
232 if (ch != CharAt(i)) {
233 return false;
234 }
235 }
236 return true;
237}
238
239// Create a modified UTF-8 encoded std::string from a java/lang/String object.
240std::string String::ToModifiedUtf8() const {
241 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
242 size_t byte_count = GetUtfLength();
243 std::string result(byte_count, static_cast<char>(0));
244 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
245 return result;
246}
247
248#ifdef HAVE__MEMCMP16
249// "count" is in 16-bit units.
250extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count);
251#define MemCmp16 __memcmp16
252#else
253static uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) {
254 for (size_t i = 0; i < count; i++) {
255 if (s0[i] != s1[i]) {
256 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
257 }
258 }
259 return 0;
260}
261#endif
262
263int32_t String::CompareTo(String* rhs) const {
264 // Quick test for comparison of a string with itself.
265 const String* lhs = this;
266 if (lhs == rhs) {
267 return 0;
268 }
269 // TODO: is this still true?
270 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
271 // because the interpreter converts the characters to 32-bit integers
272 // *without* sign extension before it subtracts them (which makes some
273 // sense since "char" is unsigned). So what we get is the result of
274 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
275 int lhsCount = lhs->GetLength();
276 int rhsCount = rhs->GetLength();
277 int countDiff = lhsCount - rhsCount;
278 int minCount = (countDiff < 0) ? lhsCount : rhsCount;
279 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
280 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
281 int otherRes = MemCmp16(lhsChars, rhsChars, minCount);
282 if (otherRes != 0) {
283 return otherRes;
284 }
285 return countDiff;
286}
287
288} // namespace mirror
289} // namespace art
290