blob: 5c57dcef459f48c8472db4e05204fb3c79ad99cb [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
34Class* String::java_lang_String_ = NULL;
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) {
55 CHECK(java_lang_String_ == NULL);
56 CHECK(java_lang_String != NULL);
57 java_lang_String_ = java_lang_String;
58}
59
60void String::ResetClass() {
61 CHECK(java_lang_String_ != NULL);
62 java_lang_String_ = NULL;
63}
64
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065int32_t String::GetHashCode() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070066 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_));
67 if (UNLIKELY(result == 0)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 ComputeHashCode();
69 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070070 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
72 << ToModifiedUtf8() << " " << result;
73 return result;
74}
75
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070076void String::ComputeHashCode() {
77 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078}
79
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070080int32_t String::GetUtfLength() {
81 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082}
83
84String* String::AllocFromUtf16(Thread* self,
85 int32_t utf16_length,
86 const uint16_t* utf16_data_in,
87 int32_t hash_code) {
Ian Rogers4069d332014-01-03 10:28:27 -080088 CHECK(utf16_data_in != nullptr || utf16_length == 0);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080089 String* string = Alloc(self, utf16_length);
Mathieu Chartier590fee92013-09-13 13:46:47 -070090 if (UNLIKELY(string == nullptr)) {
91 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Ian Rogers4069d332014-01-03 10:28:27 -080094 if (UNLIKELY(array == nullptr)) {
95 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 }
Ian Rogers4069d332014-01-03 10:28:27 -080097 memcpy(array->GetData(), utf16_data_in, utf16_length * sizeof(uint16_t));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 if (hash_code != 0) {
Ian Rogers4069d332014-01-03 10:28:27 -080099 DCHECK_EQ(hash_code, ComputeUtf16Hash(utf16_data_in, utf16_length));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 string->SetHashCode(hash_code);
101 } else {
102 string->ComputeHashCode();
103 }
104 return string;
105}
106
Ian Rogersa436fde2013-08-27 23:34:06 -0700107String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Mathieu Chartiered0fc1d2014-03-21 14:09:35 -0700108 DCHECK(utf != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 size_t char_count = CountModifiedUtf8Chars(utf);
110 return AllocFromModifiedUtf8(self, char_count, utf);
111}
112
113String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
114 const char* utf8_data_in) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800115 String* string = Alloc(self, utf16_length);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700116 if (UNLIKELY(string == nullptr)) {
117 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118 }
119 uint16_t* utf16_data_out =
120 const_cast<uint16_t*>(string->GetCharArray()->GetData());
121 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
122 string->ComputeHashCode();
123 return string;
124}
125
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800126String* String::Alloc(Thread* self, int32_t utf16_length) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700127 StackHandleScope<1> hs(self);
128 Handle<CharArray> array(hs.NewHandle(CharArray::Alloc(self, utf16_length)));
129 if (UNLIKELY(array.Get() == nullptr)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700130 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800132 return Alloc(self, array);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133}
134
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700135String* String::Alloc(Thread* self, Handle<CharArray> array) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 // Hold reference in case AllocObject causes GC.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800137 String* string = down_cast<String*>(GetJavaLangString()->AllocObject(self));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700138 if (LIKELY(string != nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700139 string->SetArray(array.Get());
Sebastien Hertzee1d79a2014-02-21 15:46:30 +0100140 string->SetCount(array->GetLength());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 return string;
143}
144
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145bool String::Equals(String* that) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 if (this == that) {
147 // Quick reference equality test
148 return true;
149 } else if (that == NULL) {
150 // Null isn't an instanceof anything
151 return false;
152 } else if (this->GetLength() != that->GetLength()) {
153 // Quick length inequality test
154 return false;
155 } else {
156 // Note: don't short circuit on hash code as we're presumably here as the
157 // hash code was already equal
158 for (int32_t i = 0; i < that->GetLength(); ++i) {
159 if (this->CharAt(i) != that->CharAt(i)) {
160 return false;
161 }
162 }
163 return true;
164 }
165}
166
Ian Rogersef7d42f2014-01-06 12:55:46 -0800167bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 if (this->GetLength() != that_length) {
169 return false;
170 } else {
171 for (int32_t i = 0; i < that_length; ++i) {
172 if (this->CharAt(i) != that_chars[that_offset + i]) {
173 return false;
174 }
175 }
176 return true;
177 }
178}
179
Ian Rogersef7d42f2014-01-06 12:55:46 -0800180bool String::Equals(const char* modified_utf8) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181 for (int32_t i = 0; i < GetLength(); ++i) {
182 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
183 if (ch == '\0' || ch != CharAt(i)) {
184 return false;
185 }
186 }
187 return *modified_utf8 == '\0';
188}
189
Ian Rogersef7d42f2014-01-06 12:55:46 -0800190bool String::Equals(const StringPiece& modified_utf8) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 const char* p = modified_utf8.data();
192 for (int32_t i = 0; i < GetLength(); ++i) {
193 uint16_t ch = GetUtf16FromUtf8(&p);
194 if (ch != CharAt(i)) {
195 return false;
196 }
197 }
198 return true;
199}
200
201// Create a modified UTF-8 encoded std::string from a java/lang/String object.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800202std::string String::ToModifiedUtf8() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
204 size_t byte_count = GetUtfLength();
205 std::string result(byte_count, static_cast<char>(0));
206 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
207 return result;
208}
209
Ian Rogersef7d42f2014-01-06 12:55:46 -0800210int32_t String::CompareTo(String* rhs) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 // Quick test for comparison of a string with itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800212 String* lhs = this;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 if (lhs == rhs) {
214 return 0;
215 }
216 // TODO: is this still true?
217 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
218 // because the interpreter converts the characters to 32-bit integers
219 // *without* sign extension before it subtracts them (which makes some
220 // sense since "char" is unsigned). So what we get is the result of
221 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
Andreas Gampe4d0589c2014-06-10 16:10:56 -0700222 int32_t lhsCount = lhs->GetLength();
223 int32_t rhsCount = rhs->GetLength();
224 int32_t countDiff = lhsCount - rhsCount;
225 int32_t minCount = (countDiff < 0) ? lhsCount : rhsCount;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
227 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
Andreas Gampe4d0589c2014-06-10 16:10:56 -0700228 int32_t otherRes = MemCmp16(lhsChars, rhsChars, minCount);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 if (otherRes != 0) {
230 return otherRes;
231 }
232 return countDiff;
233}
234
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800235void String::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800236 if (java_lang_String_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -0800237 callback(reinterpret_cast<mirror::Object**>(&java_lang_String_), arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800238 }
239}
240
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800241} // namespace mirror
242} // namespace art