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 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 17 | #include "string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame^] | 25 | #include "handle_scope-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 32 | // TODO: get global references for these |
| 33 | Class* String::java_lang_String_ = NULL; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 35 | int32_t String::FastIndexOf(int32_t ch, int32_t start) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | int32_t count = GetLength(); |
| 37 | if (start < 0) { |
| 38 | start = 0; |
| 39 | } else if (start > count) { |
| 40 | start = count; |
| 41 | } |
| 42 | const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); |
| 43 | const uint16_t* p = chars + start; |
| 44 | const uint16_t* end = chars + count; |
| 45 | while (p < end) { |
| 46 | if (*p++ == ch) { |
| 47 | return (p - 1) - chars; |
| 48 | } |
| 49 | } |
| 50 | return -1; |
| 51 | } |
| 52 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | void String::SetClass(Class* java_lang_String) { |
| 54 | CHECK(java_lang_String_ == NULL); |
| 55 | CHECK(java_lang_String != NULL); |
| 56 | java_lang_String_ = java_lang_String; |
| 57 | } |
| 58 | |
| 59 | void String::ResetClass() { |
| 60 | CHECK(java_lang_String_ != NULL); |
| 61 | java_lang_String_ = NULL; |
| 62 | } |
| 63 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | int32_t String::GetHashCode() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 65 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)); |
| 66 | if (UNLIKELY(result == 0)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 67 | ComputeHashCode(); |
| 68 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 69 | result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0) |
| 71 | << ToModifiedUtf8() << " " << result; |
| 72 | return result; |
| 73 | } |
| 74 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 75 | void String::ComputeHashCode() { |
| 76 | SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 79 | int32_t String::GetUtfLength() { |
| 80 | return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | String* String::AllocFromUtf16(Thread* self, |
| 84 | int32_t utf16_length, |
| 85 | const uint16_t* utf16_data_in, |
| 86 | int32_t hash_code) { |
Ian Rogers | 4069d33 | 2014-01-03 10:28:27 -0800 | [diff] [blame] | 87 | CHECK(utf16_data_in != nullptr || utf16_length == 0); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 88 | String* string = Alloc(self, utf16_length); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 89 | if (UNLIKELY(string == nullptr)) { |
| 90 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 91 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 92 | CharArray* array = const_cast<CharArray*>(string->GetCharArray()); |
Ian Rogers | 4069d33 | 2014-01-03 10:28:27 -0800 | [diff] [blame] | 93 | if (UNLIKELY(array == nullptr)) { |
| 94 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 95 | } |
Ian Rogers | 4069d33 | 2014-01-03 10:28:27 -0800 | [diff] [blame] | 96 | memcpy(array->GetData(), utf16_data_in, utf16_length * sizeof(uint16_t)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 97 | if (hash_code != 0) { |
Ian Rogers | 4069d33 | 2014-01-03 10:28:27 -0800 | [diff] [blame] | 98 | DCHECK_EQ(hash_code, ComputeUtf16Hash(utf16_data_in, utf16_length)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 99 | string->SetHashCode(hash_code); |
| 100 | } else { |
| 101 | string->ComputeHashCode(); |
| 102 | } |
| 103 | return string; |
| 104 | } |
| 105 | |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 106 | String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) { |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 107 | DCHECK(utf != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 108 | size_t char_count = CountModifiedUtf8Chars(utf); |
| 109 | return AllocFromModifiedUtf8(self, char_count, utf); |
| 110 | } |
| 111 | |
| 112 | String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, |
| 113 | const char* utf8_data_in) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 114 | String* string = Alloc(self, utf16_length); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 115 | if (UNLIKELY(string == nullptr)) { |
| 116 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 117 | } |
| 118 | uint16_t* utf16_data_out = |
| 119 | const_cast<uint16_t*>(string->GetCharArray()->GetData()); |
| 120 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); |
| 121 | string->ComputeHashCode(); |
| 122 | return string; |
| 123 | } |
| 124 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 125 | String* String::Alloc(Thread* self, int32_t utf16_length) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame^] | 126 | StackHandleScope<1> hs(self); |
| 127 | Handle<CharArray> array(hs.NewHandle(CharArray::Alloc(self, utf16_length))); |
| 128 | if (UNLIKELY(array.Get() == nullptr)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 129 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 130 | } |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 131 | return Alloc(self, array); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame^] | 134 | String* String::Alloc(Thread* self, const Handle<CharArray>& array) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 135 | // Hold reference in case AllocObject causes GC. |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 136 | String* string = down_cast<String*>(GetJavaLangString()->AllocObject(self)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 137 | if (LIKELY(string != nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame^] | 138 | string->SetArray(array.Get()); |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 139 | string->SetCount(array->GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 140 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 141 | return string; |
| 142 | } |
| 143 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 144 | bool String::Equals(String* that) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 145 | if (this == that) { |
| 146 | // Quick reference equality test |
| 147 | return true; |
| 148 | } else if (that == NULL) { |
| 149 | // Null isn't an instanceof anything |
| 150 | return false; |
| 151 | } else if (this->GetLength() != that->GetLength()) { |
| 152 | // Quick length inequality test |
| 153 | return false; |
| 154 | } else { |
| 155 | // Note: don't short circuit on hash code as we're presumably here as the |
| 156 | // hash code was already equal |
| 157 | for (int32_t i = 0; i < that->GetLength(); ++i) { |
| 158 | if (this->CharAt(i) != that->CharAt(i)) { |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | return true; |
| 163 | } |
| 164 | } |
| 165 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 166 | 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] | 167 | if (this->GetLength() != that_length) { |
| 168 | return false; |
| 169 | } else { |
| 170 | for (int32_t i = 0; i < that_length; ++i) { |
| 171 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
| 172 | return false; |
| 173 | } |
| 174 | } |
| 175 | return true; |
| 176 | } |
| 177 | } |
| 178 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 179 | bool String::Equals(const char* modified_utf8) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 180 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 181 | uint16_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 182 | if (ch == '\0' || ch != CharAt(i)) { |
| 183 | return false; |
| 184 | } |
| 185 | } |
| 186 | return *modified_utf8 == '\0'; |
| 187 | } |
| 188 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 189 | bool String::Equals(const StringPiece& modified_utf8) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 190 | const char* p = modified_utf8.data(); |
| 191 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 192 | uint16_t ch = GetUtf16FromUtf8(&p); |
| 193 | if (ch != CharAt(i)) { |
| 194 | return false; |
| 195 | } |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | // 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] | 201 | std::string String::ToModifiedUtf8() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 202 | const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); |
| 203 | size_t byte_count = GetUtfLength(); |
| 204 | std::string result(byte_count, static_cast<char>(0)); |
| 205 | ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength()); |
| 206 | return result; |
| 207 | } |
| 208 | |
| 209 | #ifdef HAVE__MEMCMP16 |
| 210 | // "count" is in 16-bit units. |
| 211 | extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count); |
| 212 | #define MemCmp16 __memcmp16 |
| 213 | #else |
| 214 | static uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) { |
| 215 | for (size_t i = 0; i < count; i++) { |
| 216 | if (s0[i] != s1[i]) { |
| 217 | return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]); |
| 218 | } |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | #endif |
| 223 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 224 | int32_t String::CompareTo(String* rhs) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 225 | // Quick test for comparison of a string with itself. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 226 | String* lhs = this; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 227 | if (lhs == rhs) { |
| 228 | return 0; |
| 229 | } |
| 230 | // TODO: is this still true? |
| 231 | // The annoying part here is that 0x00e9 - 0xffff != 0x00ea, |
| 232 | // because the interpreter converts the characters to 32-bit integers |
| 233 | // *without* sign extension before it subtracts them (which makes some |
| 234 | // sense since "char" is unsigned). So what we get is the result of |
| 235 | // 0x000000e9 - 0x0000ffff, which is 0xffff00ea. |
| 236 | int lhsCount = lhs->GetLength(); |
| 237 | int rhsCount = rhs->GetLength(); |
| 238 | int countDiff = lhsCount - rhsCount; |
| 239 | int minCount = (countDiff < 0) ? lhsCount : rhsCount; |
| 240 | const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset(); |
| 241 | const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset(); |
| 242 | int otherRes = MemCmp16(lhsChars, rhsChars, minCount); |
| 243 | if (otherRes != 0) { |
| 244 | return otherRes; |
| 245 | } |
| 246 | return countDiff; |
| 247 | } |
| 248 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 249 | void String::VisitRoots(RootCallback* callback, void* arg) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 250 | if (java_lang_String_ != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 251 | callback(reinterpret_cast<mirror::Object**>(&java_lang_String_), arg, 0, kRootStickyClass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 255 | } // namespace mirror |
| 256 | } // namespace art |