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