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 | |
| 32 | const CharArray* String::GetCharArray() const { |
| 33 | return GetFieldObject<const CharArray*>(ValueOffset(), false); |
| 34 | } |
| 35 | |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 36 | CharArray* String::GetCharArray() { |
| 37 | return GetFieldObject<CharArray*>(ValueOffset(), false); |
| 38 | } |
| 39 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | void String::ComputeHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 41 | SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength())); |
| 42 | } |
| 43 | |
| 44 | int32_t String::GetUtfLength() const { |
| 45 | return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength()); |
| 46 | } |
| 47 | |
| 48 | int32_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 | |
| 66 | void 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 |
| 72 | Class* String::java_lang_String_ = NULL; |
| 73 | |
| 74 | void 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 | |
| 80 | void String::ResetClass() { |
| 81 | CHECK(java_lang_String_ != NULL); |
| 82 | java_lang_String_ = NULL; |
| 83 | } |
| 84 | |
| 85 | String* String::Intern() { |
| 86 | return Runtime::Current()->GetInternTable()->InternWeak(this); |
| 87 | } |
| 88 | |
| 89 | int32_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 | |
| 100 | int32_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 | |
| 106 | uint16_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 Hertz | 8a94652 | 2013-08-02 09:52:08 +0200 | [diff] [blame] | 109 | DCHECK_GE(count_, 0); // ensures the unsigned comparison is safe. |
| 110 | if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(count_))) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | Thread* self = Thread::Current(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 112 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 113 | self->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;", |
| 114 | "length=%i; index=%i", count_, index); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | return GetCharArray()->Get(index + GetOffset()); |
| 118 | } |
| 119 | |
| 120 | String* String::AllocFromUtf16(Thread* self, |
| 121 | int32_t utf16_length, |
| 122 | const uint16_t* utf16_data_in, |
| 123 | int32_t hash_code) { |
| 124 | CHECK(utf16_data_in != NULL || utf16_length == 0); |
| 125 | String* string = Alloc(self, GetJavaLangString(), utf16_length); |
| 126 | if (string == NULL) { |
| 127 | return NULL; |
| 128 | } |
| 129 | // TODO: use 16-bit wide memset variant |
| 130 | CharArray* array = const_cast<CharArray*>(string->GetCharArray()); |
| 131 | if (array == NULL) { |
| 132 | return NULL; |
| 133 | } |
| 134 | for (int i = 0; i < utf16_length; i++) { |
| 135 | array->Set(i, utf16_data_in[i]); |
| 136 | } |
| 137 | if (hash_code != 0) { |
| 138 | string->SetHashCode(hash_code); |
| 139 | } else { |
| 140 | string->ComputeHashCode(); |
| 141 | } |
| 142 | return string; |
| 143 | } |
| 144 | |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 145 | String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 146 | if (utf == NULL) { |
| 147 | return NULL; |
| 148 | } |
| 149 | size_t char_count = CountModifiedUtf8Chars(utf); |
| 150 | return AllocFromModifiedUtf8(self, char_count, utf); |
| 151 | } |
| 152 | |
| 153 | String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, |
| 154 | const char* utf8_data_in) { |
| 155 | String* string = Alloc(self, GetJavaLangString(), utf16_length); |
| 156 | if (string == NULL) { |
| 157 | return NULL; |
| 158 | } |
| 159 | uint16_t* utf16_data_out = |
| 160 | const_cast<uint16_t*>(string->GetCharArray()->GetData()); |
| 161 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); |
| 162 | string->ComputeHashCode(); |
| 163 | return string; |
| 164 | } |
| 165 | |
| 166 | String* String::Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length) { |
| 167 | SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length)); |
| 168 | if (array.get() == NULL) { |
| 169 | return NULL; |
| 170 | } |
| 171 | return Alloc(self, java_lang_String, array.get()); |
| 172 | } |
| 173 | |
| 174 | String* String::Alloc(Thread* self, Class* java_lang_String, CharArray* array) { |
| 175 | // Hold reference in case AllocObject causes GC. |
| 176 | SirtRef<CharArray> array_ref(self, array); |
| 177 | String* string = down_cast<String*>(java_lang_String->AllocObject(self)); |
| 178 | if (string == NULL) { |
| 179 | return NULL; |
| 180 | } |
| 181 | string->SetArray(array); |
| 182 | string->SetCount(array->GetLength()); |
| 183 | return string; |
| 184 | } |
| 185 | |
| 186 | bool String::Equals(const String* that) const { |
| 187 | if (this == that) { |
| 188 | // Quick reference equality test |
| 189 | return true; |
| 190 | } else if (that == NULL) { |
| 191 | // Null isn't an instanceof anything |
| 192 | return false; |
| 193 | } else if (this->GetLength() != that->GetLength()) { |
| 194 | // Quick length inequality test |
| 195 | return false; |
| 196 | } else { |
| 197 | // Note: don't short circuit on hash code as we're presumably here as the |
| 198 | // hash code was already equal |
| 199 | for (int32_t i = 0; i < that->GetLength(); ++i) { |
| 200 | if (this->CharAt(i) != that->CharAt(i)) { |
| 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const { |
| 209 | if (this->GetLength() != that_length) { |
| 210 | return false; |
| 211 | } else { |
| 212 | for (int32_t i = 0; i < that_length; ++i) { |
| 213 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
| 214 | return false; |
| 215 | } |
| 216 | } |
| 217 | return true; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | bool String::Equals(const char* modified_utf8) const { |
| 222 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 223 | uint16_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 224 | if (ch == '\0' || ch != CharAt(i)) { |
| 225 | return false; |
| 226 | } |
| 227 | } |
| 228 | return *modified_utf8 == '\0'; |
| 229 | } |
| 230 | |
| 231 | bool String::Equals(const StringPiece& modified_utf8) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 232 | const char* p = modified_utf8.data(); |
| 233 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 234 | uint16_t ch = GetUtf16FromUtf8(&p); |
| 235 | if (ch != CharAt(i)) { |
| 236 | return false; |
| 237 | } |
| 238 | } |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
| 243 | std::string String::ToModifiedUtf8() const { |
| 244 | const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); |
| 245 | size_t byte_count = GetUtfLength(); |
| 246 | std::string result(byte_count, static_cast<char>(0)); |
| 247 | ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength()); |
| 248 | return result; |
| 249 | } |
| 250 | |
| 251 | #ifdef HAVE__MEMCMP16 |
| 252 | // "count" is in 16-bit units. |
| 253 | extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count); |
| 254 | #define MemCmp16 __memcmp16 |
| 255 | #else |
| 256 | static uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) { |
| 257 | for (size_t i = 0; i < count; i++) { |
| 258 | if (s0[i] != s1[i]) { |
| 259 | return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]); |
| 260 | } |
| 261 | } |
| 262 | return 0; |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | int32_t String::CompareTo(String* rhs) const { |
| 267 | // Quick test for comparison of a string with itself. |
| 268 | const String* lhs = this; |
| 269 | if (lhs == rhs) { |
| 270 | return 0; |
| 271 | } |
| 272 | // TODO: is this still true? |
| 273 | // The annoying part here is that 0x00e9 - 0xffff != 0x00ea, |
| 274 | // because the interpreter converts the characters to 32-bit integers |
| 275 | // *without* sign extension before it subtracts them (which makes some |
| 276 | // sense since "char" is unsigned). So what we get is the result of |
| 277 | // 0x000000e9 - 0x0000ffff, which is 0xffff00ea. |
| 278 | int lhsCount = lhs->GetLength(); |
| 279 | int rhsCount = rhs->GetLength(); |
| 280 | int countDiff = lhsCount - rhsCount; |
| 281 | int minCount = (countDiff < 0) ? lhsCount : rhsCount; |
| 282 | const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset(); |
| 283 | const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset(); |
| 284 | int otherRes = MemCmp16(lhsChars, rhsChars, minCount); |
| 285 | if (otherRes != 0) { |
| 286 | return otherRes; |
| 287 | } |
| 288 | return countDiff; |
| 289 | } |
| 290 | |
| 291 | } // namespace mirror |
| 292 | } // namespace art |