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