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