blob: e7c88c542552edee6b50de09b01ae905e1059ad2 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "intern_table.h"
24#include "object-inl.h"
25#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070026#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "thread.h"
Ian Rogersa6724902013-09-23 09:23:37 -070028#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029
30namespace art {
31namespace mirror {
32
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070033// TODO: get global references for these
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070034GcRoot<Class> String::java_lang_String_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035
Ian Rogersef7d42f2014-01-06 12:55:46 -080036int32_t String::FastIndexOf(int32_t ch, int32_t start) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037 int32_t count = GetLength();
38 if (start < 0) {
39 start = 0;
40 } else if (start > count) {
41 start = count;
42 }
43 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
44 const uint16_t* p = chars + start;
45 const uint16_t* end = chars + count;
46 while (p < end) {
47 if (*p++ == ch) {
48 return (p - 1) - chars;
49 }
50 }
51 return -1;
52}
53
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054void String::SetClass(Class* java_lang_String) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070055 CHECK(java_lang_String_.IsNull());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 CHECK(java_lang_String != NULL);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070057 java_lang_String_ = GcRoot<Class>(java_lang_String);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058}
59
60void String::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070061 CHECK(!java_lang_String_.IsNull());
62 java_lang_String_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063}
64
Mathieu Chartiercdfd39f2014-08-29 18:16:58 -070065int32_t String::ComputeHashCode() {
66 const int32_t hash_code = ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength());
67 SetHashCode(hash_code);
68 return hash_code;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069}
70
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070071int32_t String::GetUtfLength() {
72 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073}
74
75String* String::AllocFromUtf16(Thread* self,
76 int32_t utf16_length,
77 const uint16_t* utf16_data_in,
78 int32_t hash_code) {
Ian Rogers4069d332014-01-03 10:28:27 -080079 CHECK(utf16_data_in != nullptr || utf16_length == 0);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080080 String* string = Alloc(self, utf16_length);
Mathieu Chartier590fee92013-09-13 13:46:47 -070081 if (UNLIKELY(string == nullptr)) {
82 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Ian Rogers4069d332014-01-03 10:28:27 -080085 if (UNLIKELY(array == nullptr)) {
86 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 }
Ian Rogers4069d332014-01-03 10:28:27 -080088 memcpy(array->GetData(), utf16_data_in, utf16_length * sizeof(uint16_t));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 if (hash_code != 0) {
Ian Rogers4069d332014-01-03 10:28:27 -080090 DCHECK_EQ(hash_code, ComputeUtf16Hash(utf16_data_in, utf16_length));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 string->SetHashCode(hash_code);
92 } else {
93 string->ComputeHashCode();
94 }
95 return string;
96}
97
Ian Rogersa436fde2013-08-27 23:34:06 -070098String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -070099 DCHECK(utf != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 size_t char_count = CountModifiedUtf8Chars(utf);
101 return AllocFromModifiedUtf8(self, char_count, utf);
102}
103
104String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
105 const char* utf8_data_in) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800106 String* string = Alloc(self, utf16_length);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700107 if (UNLIKELY(string == nullptr)) {
108 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 }
110 uint16_t* utf16_data_out =
111 const_cast<uint16_t*>(string->GetCharArray()->GetData());
112 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
113 string->ComputeHashCode();
114 return string;
115}
116
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800117String* String::Alloc(Thread* self, int32_t utf16_length) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700118 StackHandleScope<1> hs(self);
119 Handle<CharArray> array(hs.NewHandle(CharArray::Alloc(self, utf16_length)));
120 if (UNLIKELY(array.Get() == nullptr)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700121 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800123 return Alloc(self, array);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124}
125
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700126String* String::Alloc(Thread* self, Handle<CharArray> array) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 // Hold reference in case AllocObject causes GC.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800128 String* string = down_cast<String*>(GetJavaLangString()->AllocObject(self));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700129 if (LIKELY(string != nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700130 string->SetArray(array.Get());
Sebastien Hertzee1d79a2014-02-21 15:46:30 +0100131 string->SetCount(array->GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 return string;
134}
135
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136bool String::Equals(String* that) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 if (this == that) {
138 // Quick reference equality test
139 return true;
140 } else if (that == NULL) {
141 // Null isn't an instanceof anything
142 return false;
143 } else if (this->GetLength() != that->GetLength()) {
144 // Quick length inequality test
145 return false;
146 } else {
147 // Note: don't short circuit on hash code as we're presumably here as the
148 // hash code was already equal
149 for (int32_t i = 0; i < that->GetLength(); ++i) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000150 if (this->UncheckedCharAt(i) != that->UncheckedCharAt(i)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 return false;
152 }
153 }
154 return true;
155 }
156}
157
Ian Rogersef7d42f2014-01-06 12:55:46 -0800158bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 if (this->GetLength() != that_length) {
160 return false;
161 } else {
162 for (int32_t i = 0; i < that_length; ++i) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000163 if (this->UncheckedCharAt(i) != that_chars[that_offset + i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 return false;
165 }
166 }
167 return true;
168 }
169}
170
Ian Rogersef7d42f2014-01-06 12:55:46 -0800171bool String::Equals(const char* modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000172 const int32_t length = GetLength();
173 int32_t i = 0;
174 while (i < length) {
175 const uint32_t ch = GetUtf16FromUtf8(&modified_utf8);
176 if (ch == '\0') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177 return false;
178 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000179
180 if (GetLeadingUtf16Char(ch) != UncheckedCharAt(i++)) {
181 return false;
182 }
183
184 const uint16_t trailing = GetTrailingUtf16Char(ch);
185 if (trailing != 0) {
186 if (i == length) {
187 return false;
188 }
189
190 if (UncheckedCharAt(i++) != trailing) {
191 return false;
192 }
193 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 }
195 return *modified_utf8 == '\0';
196}
197
Ian Rogersef7d42f2014-01-06 12:55:46 -0800198bool String::Equals(const StringPiece& modified_utf8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000199 const int32_t length = GetLength();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 const char* p = modified_utf8.data();
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000201 for (int32_t i = 0; i < length; ++i) {
202 uint32_t ch = GetUtf16FromUtf8(&p);
203
204 if (GetLeadingUtf16Char(ch) != UncheckedCharAt(i)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 return false;
206 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000207
208 const uint16_t trailing = GetTrailingUtf16Char(ch);
209 if (trailing != 0) {
210 if (i == (length - 1)) {
211 return false;
212 }
213
214 if (UncheckedCharAt(++i) != trailing) {
215 return false;
216 }
217 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 }
219 return true;
220}
221
222// Create a modified UTF-8 encoded std::string from a java/lang/String object.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800223std::string String::ToModifiedUtf8() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
225 size_t byte_count = GetUtfLength();
226 std::string result(byte_count, static_cast<char>(0));
227 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
228 return result;
229}
230
Ian Rogersef7d42f2014-01-06 12:55:46 -0800231int32_t String::CompareTo(String* rhs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 // Quick test for comparison of a string with itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800233 String* lhs = this;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234 if (lhs == rhs) {
235 return 0;
236 }
237 // TODO: is this still true?
238 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
239 // because the interpreter converts the characters to 32-bit integers
240 // *without* sign extension before it subtracts them (which makes some
241 // sense since "char" is unsigned). So what we get is the result of
242 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
Andreas Gampe4d0589c2014-06-10 16:10:56 -0700243 int32_t lhsCount = lhs->GetLength();
244 int32_t rhsCount = rhs->GetLength();
245 int32_t countDiff = lhsCount - rhsCount;
246 int32_t minCount = (countDiff < 0) ? lhsCount : rhsCount;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
248 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
Andreas Gampe4d0589c2014-06-10 16:10:56 -0700249 int32_t otherRes = MemCmp16(lhsChars, rhsChars, minCount);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250 if (otherRes != 0) {
251 return otherRes;
252 }
253 return countDiff;
254}
255
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800256void String::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800257 java_lang_String_.VisitRootIfNonNull(callback, arg, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800258}
259
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260} // namespace mirror
261} // namespace art