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