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