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