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 | |
Andreas Gampe | 4d0589c | 2014-06-10 16:10:56 -0700 | [diff] [blame] | 19 | #include "arch/memcmp16.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #include "array.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 21 | #include "class-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 22 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 23 | #include "intern_table.h" |
| 24 | #include "object-inl.h" |
| 25 | #include "runtime.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 26 | #include "handle_scope-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "thread.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 28 | #include "utf-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | namespace mirror { |
| 32 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 33 | // TODO: get global references for these |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 34 | GcRoot<Class> String::java_lang_String_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 36 | int32_t String::FastIndexOf(int32_t ch, int32_t start) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | void String::SetClass(Class* java_lang_String) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 55 | CHECK(java_lang_String_.IsNull()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | CHECK(java_lang_String != NULL); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 57 | java_lang_String_ = GcRoot<Class>(java_lang_String); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void String::ResetClass() { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 61 | CHECK(!java_lang_String_.IsNull()); |
| 62 | java_lang_String_ = GcRoot<Class>(nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | int32_t String::GetHashCode() { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 66 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)); |
| 67 | if (UNLIKELY(result == 0)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 68 | ComputeHashCode(); |
| 69 | } |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 70 | result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 71 | DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0) |
| 72 | << ToModifiedUtf8() << " " << result; |
| 73 | return result; |
| 74 | } |
| 75 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 76 | void String::ComputeHashCode() { |
| 77 | SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 80 | int32_t String::GetUtfLength() { |
| 81 | return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | String* String::AllocFromUtf16(Thread* self, |
| 85 | int32_t utf16_length, |
| 86 | const uint16_t* utf16_data_in, |
| 87 | int32_t hash_code) { |
Ian Rogers | 4069d33 | 2014-01-03 10:28:27 -0800 | [diff] [blame] | 88 | CHECK(utf16_data_in != nullptr || utf16_length == 0); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 89 | String* string = Alloc(self, utf16_length); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 90 | if (UNLIKELY(string == nullptr)) { |
| 91 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 92 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 93 | CharArray* array = const_cast<CharArray*>(string->GetCharArray()); |
Ian Rogers | 4069d33 | 2014-01-03 10:28:27 -0800 | [diff] [blame] | 94 | if (UNLIKELY(array == nullptr)) { |
| 95 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 96 | } |
Ian Rogers | 4069d33 | 2014-01-03 10:28:27 -0800 | [diff] [blame] | 97 | memcpy(array->GetData(), utf16_data_in, utf16_length * sizeof(uint16_t)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 98 | if (hash_code != 0) { |
Ian Rogers | 4069d33 | 2014-01-03 10:28:27 -0800 | [diff] [blame] | 99 | DCHECK_EQ(hash_code, ComputeUtf16Hash(utf16_data_in, utf16_length)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 100 | string->SetHashCode(hash_code); |
| 101 | } else { |
| 102 | string->ComputeHashCode(); |
| 103 | } |
| 104 | return string; |
| 105 | } |
| 106 | |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 107 | String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) { |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 108 | DCHECK(utf != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 109 | size_t char_count = CountModifiedUtf8Chars(utf); |
| 110 | return AllocFromModifiedUtf8(self, char_count, utf); |
| 111 | } |
| 112 | |
| 113 | String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, |
| 114 | const char* utf8_data_in) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 115 | String* string = Alloc(self, utf16_length); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 116 | if (UNLIKELY(string == nullptr)) { |
| 117 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 118 | } |
| 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 Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 126 | String* String::Alloc(Thread* self, int32_t utf16_length) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 127 | StackHandleScope<1> hs(self); |
| 128 | Handle<CharArray> array(hs.NewHandle(CharArray::Alloc(self, utf16_length))); |
| 129 | if (UNLIKELY(array.Get() == nullptr)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 130 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 131 | } |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 132 | return Alloc(self, array); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 135 | String* String::Alloc(Thread* self, Handle<CharArray> array) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 136 | // Hold reference in case AllocObject causes GC. |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 137 | String* string = down_cast<String*>(GetJavaLangString()->AllocObject(self)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 138 | if (LIKELY(string != nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 139 | string->SetArray(array.Get()); |
Sebastien Hertz | ee1d79a | 2014-02-21 15:46:30 +0100 | [diff] [blame] | 140 | string->SetCount(array->GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 141 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 142 | return string; |
| 143 | } |
| 144 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 145 | bool String::Equals(String* that) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 146 | 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 Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 167 | 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] | 168 | 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 Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 180 | bool String::Equals(const char* modified_utf8) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 181 | 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 Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 190 | bool String::Equals(const StringPiece& modified_utf8) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 191 | 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 Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 202 | std::string String::ToModifiedUtf8() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 203 | 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 Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 210 | int32_t String::CompareTo(String* rhs) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 211 | // Quick test for comparison of a string with itself. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 212 | String* lhs = this; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 213 | 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 Gampe | 4d0589c | 2014-06-10 16:10:56 -0700 | [diff] [blame] | 222 | 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 226 | const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset(); |
| 227 | const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset(); |
Andreas Gampe | 4d0589c | 2014-06-10 16:10:56 -0700 | [diff] [blame] | 228 | int32_t otherRes = MemCmp16(lhsChars, rhsChars, minCount); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 229 | if (otherRes != 0) { |
| 230 | return otherRes; |
| 231 | } |
| 232 | return countDiff; |
| 233 | } |
| 234 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 235 | void String::VisitRoots(RootCallback* callback, void* arg) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 236 | if (!java_lang_String_.IsNull()) { |
| 237 | java_lang_String_.VisitRoot(callback, arg, 0, kRootStickyClass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 241 | } // namespace mirror |
| 242 | } // namespace art |