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" |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 21 | #include "base/array_ref.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 23 | #include "class-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 24 | #include "dex/descriptors_names.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 25 | #include "dex/utf-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 26 | #include "gc/accounting/card_table-inl.h" |
Andreas Gampe | 508fdf3 | 2017-06-05 16:42:13 -0700 | [diff] [blame] | 27 | #include "gc_root-inl.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 28 | #include "handle_scope-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "intern_table.h" |
| 30 | #include "object-inl.h" |
| 31 | #include "runtime.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 32 | #include "string-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "thread.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | namespace mirror { |
| 37 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 38 | // TODO: get global references for these |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 39 | GcRoot<Class> String::java_lang_String_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 41 | int32_t String::FastIndexOf(int32_t ch, int32_t start) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 42 | int32_t count = GetLength(); |
| 43 | if (start < 0) { |
| 44 | start = 0; |
| 45 | } else if (start > count) { |
| 46 | start = count; |
| 47 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 48 | if (IsCompressed()) { |
| 49 | return FastIndexOf<uint8_t>(GetValueCompressed(), ch, start); |
| 50 | } else { |
| 51 | return FastIndexOf<uint16_t>(GetValue(), ch, start); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 55 | void String::SetClass(ObjPtr<Class> java_lang_String) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 56 | CHECK(java_lang_String_.IsNull()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 57 | CHECK(java_lang_String != nullptr); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 58 | CHECK(java_lang_String->IsStringClass()); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 59 | java_lang_String_ = GcRoot<Class>(java_lang_String); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void String::ResetClass() { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 63 | CHECK(!java_lang_String_.IsNull()); |
| 64 | java_lang_String_ = GcRoot<Class>(nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 67 | int String::ComputeHashCode() { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 68 | int32_t hash_code = 0; |
| 69 | if (IsCompressed()) { |
| 70 | hash_code = ComputeUtf16Hash(GetValueCompressed(), GetLength()); |
| 71 | } else { |
| 72 | hash_code = ComputeUtf16Hash(GetValue(), GetLength()); |
| 73 | } |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 74 | SetHashCode(hash_code); |
| 75 | return hash_code; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 78 | int32_t String::GetUtfLength() { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 79 | if (IsCompressed()) { |
| 80 | return GetLength(); |
| 81 | } else { |
| 82 | return CountUtf8Bytes(GetValue(), GetLength()); |
| 83 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 86 | inline bool String::AllASCIIExcept(const uint16_t* chars, int32_t length, uint16_t non_ascii) { |
| 87 | DCHECK(!IsASCII(non_ascii)); |
| 88 | for (int32_t i = 0; i < length; ++i) { |
| 89 | if (!IsASCII(chars[i]) && chars[i] != non_ascii) { |
| 90 | return false; |
| 91 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 92 | } |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Vladimir Marko | 9e57aba | 2017-03-16 10:45:40 +0000 | [diff] [blame] | 96 | ObjPtr<String> String::DoReplace(Thread* self, Handle<String> src, uint16_t old_c, uint16_t new_c) { |
| 97 | int32_t length = src->GetLength(); |
| 98 | DCHECK(src->IsCompressed() |
| 99 | ? ContainsElement(ArrayRef<uint8_t>(src->value_compressed_, length), old_c) |
| 100 | : ContainsElement(ArrayRef<uint16_t>(src->value_, length), old_c)); |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 101 | bool compressible = |
| 102 | kUseStringCompression && |
| 103 | IsASCII(new_c) && |
Vladimir Marko | 9e57aba | 2017-03-16 10:45:40 +0000 | [diff] [blame] | 104 | (src->IsCompressed() || (!IsASCII(old_c) && AllASCIIExcept(src->value_, length, old_c))); |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 105 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
Vladimir Marko | 9e57aba | 2017-03-16 10:45:40 +0000 | [diff] [blame] | 106 | const int32_t length_with_flag = String::GetFlaggedCount(length, compressible); |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 107 | SetStringCountVisitor visitor(length_with_flag); |
| 108 | ObjPtr<String> string = Alloc<true>(self, length_with_flag, allocator_type, visitor); |
| 109 | if (UNLIKELY(string == nullptr)) { |
| 110 | return nullptr; |
| 111 | } |
| 112 | if (compressible) { |
| 113 | auto replace = [old_c, new_c](uint16_t c) { |
| 114 | return dchecked_integral_cast<uint8_t>((old_c != c) ? c : new_c); |
| 115 | }; |
| 116 | uint8_t* out = string->value_compressed_; |
Vladimir Marko | 9e57aba | 2017-03-16 10:45:40 +0000 | [diff] [blame] | 117 | if (LIKELY(src->IsCompressed())) { // LIKELY(compressible == src->IsCompressed()) |
| 118 | std::transform(src->value_compressed_, src->value_compressed_ + length, out, replace); |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 119 | } else { |
Vladimir Marko | 9e57aba | 2017-03-16 10:45:40 +0000 | [diff] [blame] | 120 | std::transform(src->value_, src->value_ + length, out, replace); |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 121 | } |
| 122 | DCHECK(kUseStringCompression && AllASCII(out, length)); |
| 123 | } else { |
| 124 | auto replace = [old_c, new_c](uint16_t c) { |
| 125 | return (old_c != c) ? c : new_c; |
| 126 | }; |
| 127 | uint16_t* out = string->value_; |
Vladimir Marko | 9e57aba | 2017-03-16 10:45:40 +0000 | [diff] [blame] | 128 | if (UNLIKELY(src->IsCompressed())) { // LIKELY(compressible == src->IsCompressed()) |
| 129 | std::transform(src->value_compressed_, src->value_compressed_ + length, out, replace); |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 130 | } else { |
Vladimir Marko | 9e57aba | 2017-03-16 10:45:40 +0000 | [diff] [blame] | 131 | std::transform(src->value_, src->value_ + length, out, replace); |
Vladimir Marko | 92907f3 | 2017-02-20 14:08:30 +0000 | [diff] [blame] | 132 | } |
| 133 | DCHECK(!kUseStringCompression || !AllASCII(out, length)); |
| 134 | } |
| 135 | return string; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | String* String::AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2) { |
| 139 | int32_t length = string->GetLength(); |
| 140 | int32_t length2 = string2->GetLength(); |
| 141 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 142 | const bool compressible = kUseStringCompression && |
| 143 | (string->IsCompressed() && string2->IsCompressed()); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 144 | const int32_t length_with_flag = String::GetFlaggedCount(length + length2, compressible); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 145 | |
| 146 | SetStringCountVisitor visitor(length_with_flag); |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 147 | ObjPtr<String> new_string = Alloc<true>(self, length_with_flag, allocator_type, visitor); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 148 | if (UNLIKELY(new_string == nullptr)) { |
| 149 | return nullptr; |
| 150 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 151 | if (compressible) { |
| 152 | uint8_t* new_value = new_string->GetValueCompressed(); |
| 153 | memcpy(new_value, string->GetValueCompressed(), length * sizeof(uint8_t)); |
| 154 | memcpy(new_value + length, string2->GetValueCompressed(), length2 * sizeof(uint8_t)); |
| 155 | } else { |
| 156 | uint16_t* new_value = new_string->GetValue(); |
| 157 | if (string->IsCompressed()) { |
| 158 | for (int i = 0; i < length; ++i) { |
| 159 | new_value[i] = string->CharAt(i); |
| 160 | } |
| 161 | } else { |
| 162 | memcpy(new_value, string->GetValue(), length * sizeof(uint16_t)); |
| 163 | } |
| 164 | if (string2->IsCompressed()) { |
| 165 | for (int i = 0; i < length2; ++i) { |
| 166 | new_value[i+length] = string2->CharAt(i); |
| 167 | } |
| 168 | } else { |
| 169 | memcpy(new_value + length, string2->GetValue(), length2 * sizeof(uint16_t)); |
| 170 | } |
| 171 | } |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 172 | return new_string.Ptr(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | String* String::AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in) { |
Ian Rogers | 4069d33 | 2014-01-03 10:28:27 -0800 | [diff] [blame] | 176 | CHECK(utf16_data_in != nullptr || utf16_length == 0); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 177 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 178 | const bool compressible = kUseStringCompression && |
| 179 | String::AllASCII<uint16_t>(utf16_data_in, utf16_length); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 180 | int32_t length_with_flag = String::GetFlaggedCount(utf16_length, compressible); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 181 | SetStringCountVisitor visitor(length_with_flag); |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 182 | ObjPtr<String> string = Alloc<true>(self, length_with_flag, allocator_type, visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 183 | if (UNLIKELY(string == nullptr)) { |
| 184 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 185 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 186 | if (compressible) { |
| 187 | for (int i = 0; i < utf16_length; ++i) { |
| 188 | string->GetValueCompressed()[i] = static_cast<uint8_t>(utf16_data_in[i]); |
| 189 | } |
| 190 | } else { |
| 191 | uint16_t* array = string->GetValue(); |
| 192 | memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t)); |
| 193 | } |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 194 | return string.Ptr(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 197 | String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) { |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 198 | DCHECK(utf != nullptr); |
Bruce Hoult | 1646d7a | 2015-10-28 15:06:12 +0300 | [diff] [blame] | 199 | size_t byte_count = strlen(utf); |
| 200 | size_t char_count = CountModifiedUtf8Chars(utf, byte_count); |
| 201 | return AllocFromModifiedUtf8(self, char_count, utf, byte_count); |
| 202 | } |
| 203 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 204 | String* String::AllocFromModifiedUtf8(Thread* self, |
| 205 | int32_t utf16_length, |
| 206 | const char* utf8_data_in) { |
Bruce Hoult | 1646d7a | 2015-10-28 15:06:12 +0300 | [diff] [blame] | 207 | return AllocFromModifiedUtf8(self, utf16_length, utf8_data_in, strlen(utf8_data_in)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 210 | String* String::AllocFromModifiedUtf8(Thread* self, |
| 211 | int32_t utf16_length, |
| 212 | const char* utf8_data_in, |
| 213 | int32_t utf8_length) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 214 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 215 | const bool compressible = kUseStringCompression && (utf16_length == utf8_length); |
Vladimir Marko | fdaf0f4 | 2016-10-13 19:29:53 +0100 | [diff] [blame] | 216 | const int32_t utf16_length_with_flag = String::GetFlaggedCount(utf16_length, compressible); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 217 | SetStringCountVisitor visitor(utf16_length_with_flag); |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 218 | ObjPtr<String> string = Alloc<true>(self, utf16_length_with_flag, allocator_type, visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 219 | if (UNLIKELY(string == nullptr)) { |
| 220 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 221 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 222 | if (compressible) { |
| 223 | memcpy(string->GetValueCompressed(), utf8_data_in, utf16_length * sizeof(uint8_t)); |
| 224 | } else { |
| 225 | uint16_t* utf16_data_out = string->GetValue(); |
| 226 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length); |
| 227 | } |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 228 | return string.Ptr(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 231 | bool String::Equals(ObjPtr<String> that) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 232 | if (this == that) { |
| 233 | // Quick reference equality test |
| 234 | return true; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 235 | } else if (that == nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 236 | // Null isn't an instanceof anything |
| 237 | return false; |
| 238 | } else if (this->GetLength() != that->GetLength()) { |
| 239 | // Quick length inequality test |
| 240 | return false; |
| 241 | } else { |
| 242 | // Note: don't short circuit on hash code as we're presumably here as the |
| 243 | // hash code was already equal |
| 244 | for (int32_t i = 0; i < that->GetLength(); ++i) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 245 | if (this->CharAt(i) != that->CharAt(i)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | return true; |
| 250 | } |
| 251 | } |
| 252 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 253 | 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] | 254 | if (this->GetLength() != that_length) { |
| 255 | return false; |
| 256 | } else { |
| 257 | for (int32_t i = 0; i < that_length; ++i) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 258 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | } |
| 262 | return true; |
| 263 | } |
| 264 | } |
| 265 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 266 | bool String::Equals(const char* modified_utf8) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 267 | const int32_t length = GetLength(); |
| 268 | int32_t i = 0; |
| 269 | while (i < length) { |
| 270 | const uint32_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 271 | if (ch == '\0') { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 272 | return false; |
| 273 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 274 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 275 | if (GetLeadingUtf16Char(ch) != CharAt(i++)) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | |
| 279 | const uint16_t trailing = GetTrailingUtf16Char(ch); |
| 280 | if (trailing != 0) { |
| 281 | if (i == length) { |
| 282 | return false; |
| 283 | } |
| 284 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 285 | if (CharAt(i++) != trailing) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 289 | } |
| 290 | return *modified_utf8 == '\0'; |
| 291 | } |
| 292 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 293 | bool String::Equals(const StringPiece& modified_utf8) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 294 | const int32_t length = GetLength(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 295 | const char* p = modified_utf8.data(); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 296 | for (int32_t i = 0; i < length; ++i) { |
| 297 | uint32_t ch = GetUtf16FromUtf8(&p); |
| 298 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 299 | if (GetLeadingUtf16Char(ch) != CharAt(i)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 300 | return false; |
| 301 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 302 | |
| 303 | const uint16_t trailing = GetTrailingUtf16Char(ch); |
| 304 | if (trailing != 0) { |
| 305 | if (i == (length - 1)) { |
| 306 | return false; |
| 307 | } |
| 308 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 309 | if (CharAt(++i) != trailing) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 310 | return false; |
| 311 | } |
| 312 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 313 | } |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | // 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] | 318 | std::string String::ToModifiedUtf8() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 319 | size_t byte_count = GetUtfLength(); |
| 320 | std::string result(byte_count, static_cast<char>(0)); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 321 | if (IsCompressed()) { |
| 322 | for (size_t i = 0; i < byte_count; ++i) { |
| 323 | result[i] = static_cast<char>(CharAt(i)); |
| 324 | } |
| 325 | } else { |
| 326 | const uint16_t* chars = GetValue(); |
| 327 | ConvertUtf16ToModifiedUtf8(&result[0], byte_count, chars, GetLength()); |
| 328 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 329 | return result; |
| 330 | } |
| 331 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 332 | int32_t String::CompareTo(ObjPtr<String> rhs) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 333 | // Quick test for comparison of a string with itself. |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 334 | ObjPtr<String> lhs = this; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 335 | if (lhs == rhs) { |
| 336 | return 0; |
| 337 | } |
Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 338 | int32_t lhs_count = lhs->GetLength(); |
| 339 | int32_t rhs_count = rhs->GetLength(); |
| 340 | int32_t count_diff = lhs_count - rhs_count; |
| 341 | int32_t min_count = (count_diff < 0) ? lhs_count : rhs_count; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 342 | if (lhs->IsCompressed() && rhs->IsCompressed()) { |
Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 343 | const uint8_t* lhs_chars = lhs->GetValueCompressed(); |
| 344 | const uint8_t* rhs_chars = rhs->GetValueCompressed(); |
| 345 | for (int32_t i = 0; i < min_count; ++i) { |
| 346 | int32_t char_diff = static_cast<int32_t>(lhs_chars[i]) - static_cast<int32_t>(rhs_chars[i]); |
| 347 | if (char_diff != 0) { |
| 348 | return char_diff; |
| 349 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 350 | } |
| 351 | } else if (lhs->IsCompressed() || rhs->IsCompressed()) { |
Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 352 | const uint8_t* compressed_chars = |
| 353 | lhs->IsCompressed() ? lhs->GetValueCompressed() : rhs->GetValueCompressed(); |
| 354 | const uint16_t* uncompressed_chars = lhs->IsCompressed() ? rhs->GetValue() : lhs->GetValue(); |
| 355 | for (int32_t i = 0; i < min_count; ++i) { |
| 356 | int32_t char_diff = |
| 357 | static_cast<int32_t>(compressed_chars[i]) - static_cast<int32_t>(uncompressed_chars[i]); |
| 358 | if (char_diff != 0) { |
| 359 | return lhs->IsCompressed() ? char_diff : -char_diff; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } else { |
Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 363 | const uint16_t* lhs_chars = lhs->GetValue(); |
| 364 | const uint16_t* rhs_chars = rhs->GetValue(); |
| 365 | // FIXME: The MemCmp16() name is misleading. It returns the char difference on mismatch |
| 366 | // where memcmp() only guarantees that the returned value has the same sign. |
| 367 | int32_t char_diff = MemCmp16(lhs_chars, rhs_chars, min_count); |
| 368 | if (char_diff != 0) { |
| 369 | return char_diff; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 370 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 371 | } |
Vladimir Marko | 9c9883b | 2016-10-17 14:45:29 +0100 | [diff] [blame] | 372 | return count_diff; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 375 | void String::VisitRoots(RootVisitor* visitor) { |
| 376 | java_lang_String_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 379 | CharArray* String::ToCharArray(Thread* self) { |
| 380 | StackHandleScope<1> hs(self); |
| 381 | Handle<String> string(hs.NewHandle(this)); |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 382 | ObjPtr<CharArray> result = CharArray::Alloc(self, GetLength()); |
Mathieu Chartier | 04e983a | 2015-11-13 08:36:59 -0800 | [diff] [blame] | 383 | if (result != nullptr) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 384 | if (string->IsCompressed()) { |
| 385 | int32_t length = string->GetLength(); |
| 386 | for (int i = 0; i < length; ++i) { |
| 387 | result->GetData()[i] = string->CharAt(i); |
| 388 | } |
| 389 | } else { |
| 390 | memcpy(result->GetData(), string->GetValue(), string->GetLength() * sizeof(uint16_t)); |
| 391 | } |
Mathieu Chartier | 04e983a | 2015-11-13 08:36:59 -0800 | [diff] [blame] | 392 | } else { |
| 393 | self->AssertPendingOOMException(); |
| 394 | } |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 395 | return result.Ptr(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) { |
| 399 | uint16_t* data = array->GetData() + index; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 400 | if (IsCompressed()) { |
| 401 | for (int i = start; i < end; ++i) { |
| 402 | data[i-start] = CharAt(i); |
| 403 | } |
| 404 | } else { |
| 405 | uint16_t* value = GetValue() + start; |
| 406 | memcpy(data, value, (end - start) * sizeof(uint16_t)); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | bool String::IsValueNull() { |
| 411 | return (IsCompressed()) ? (GetValueCompressed() == nullptr) : (GetValue() == nullptr); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 412 | } |
| 413 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 414 | std::string String::PrettyStringDescriptor(ObjPtr<mirror::String> java_descriptor) { |
| 415 | if (java_descriptor == nullptr) { |
| 416 | return "null"; |
| 417 | } |
| 418 | return java_descriptor->PrettyStringDescriptor(); |
| 419 | } |
| 420 | |
| 421 | std::string String::PrettyStringDescriptor() { |
| 422 | return PrettyDescriptor(ToModifiedUtf8().c_str()); |
| 423 | } |
| 424 | |
Andreas Gampe | b2d18fa | 2017-06-06 20:46:10 -0700 | [diff] [blame] | 425 | ObjPtr<String> String::Intern() { |
| 426 | return Runtime::Current()->GetInternTable()->InternWeak(this); |
| 427 | } |
| 428 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 429 | } // namespace mirror |
| 430 | } // namespace art |