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