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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | void String::SetClass(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(); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 96 | const bool compressible = kUseStringCompression && (string->IsCompressed() && string2->IsCompressed()); |
| 97 | const int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(length + length2) |
| 98 | : (length + length2); |
| 99 | |
| 100 | SetStringCountVisitor visitor(length_with_flag); |
| 101 | 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 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 126 | return new_string; |
| 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); |
| 134 | int32_t length_with_flag = (compressible) ? String::GetFlaggedCount(utf16_length) |
| 135 | : utf16_length; |
| 136 | SetStringCountVisitor visitor(length_with_flag); |
| 137 | String* string = Alloc<true>(self, length_with_flag, allocator_type, visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 138 | if (UNLIKELY(string == nullptr)) { |
| 139 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 140 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 141 | if (compressible) { |
| 142 | for (int i = 0; i < utf16_length; ++i) { |
| 143 | string->GetValueCompressed()[i] = static_cast<uint8_t>(utf16_data_in[i]); |
| 144 | } |
| 145 | } else { |
| 146 | uint16_t* array = string->GetValue(); |
| 147 | memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t)); |
| 148 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 149 | return string; |
| 150 | } |
| 151 | |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 152 | String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) { |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 153 | DCHECK(utf != nullptr); |
Bruce Hoult | 1646d7a | 2015-10-28 15:06:12 +0300 | [diff] [blame] | 154 | size_t byte_count = strlen(utf); |
| 155 | size_t char_count = CountModifiedUtf8Chars(utf, byte_count); |
| 156 | return AllocFromModifiedUtf8(self, char_count, utf, byte_count); |
| 157 | } |
| 158 | |
| 159 | String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, const char* utf8_data_in) { |
| 160 | return AllocFromModifiedUtf8(self, utf16_length, utf8_data_in, strlen(utf8_data_in)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, |
Bruce Hoult | 1646d7a | 2015-10-28 15:06:12 +0300 | [diff] [blame] | 164 | const char* utf8_data_in, int32_t utf8_length) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 165 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 166 | const bool compressible = kUseStringCompression && (utf16_length == utf8_length); |
| 167 | const int32_t utf16_length_with_flag = (compressible) ? String::GetFlaggedCount(utf16_length) |
| 168 | : utf16_length; |
| 169 | SetStringCountVisitor visitor(utf16_length_with_flag); |
| 170 | String* string = Alloc<true>(self, utf16_length_with_flag, allocator_type, visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 171 | if (UNLIKELY(string == nullptr)) { |
| 172 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 173 | } |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 174 | if (compressible) { |
| 175 | memcpy(string->GetValueCompressed(), utf8_data_in, utf16_length * sizeof(uint8_t)); |
| 176 | } else { |
| 177 | uint16_t* utf16_data_out = string->GetValue(); |
| 178 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length); |
| 179 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 180 | return string; |
| 181 | } |
| 182 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 183 | bool String::Equals(String* that) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 184 | if (this == that) { |
| 185 | // Quick reference equality test |
| 186 | return true; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 187 | } else if (that == nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 188 | // Null isn't an instanceof anything |
| 189 | return false; |
| 190 | } else if (this->GetLength() != that->GetLength()) { |
| 191 | // Quick length inequality test |
| 192 | return false; |
| 193 | } else { |
| 194 | // Note: don't short circuit on hash code as we're presumably here as the |
| 195 | // hash code was already equal |
| 196 | for (int32_t i = 0; i < that->GetLength(); ++i) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 197 | if (this->CharAt(i) != that->CharAt(i)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | } |
| 204 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 205 | 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] | 206 | if (this->GetLength() != that_length) { |
| 207 | return false; |
| 208 | } else { |
| 209 | for (int32_t i = 0; i < that_length; ++i) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 210 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | } |
| 214 | return true; |
| 215 | } |
| 216 | } |
| 217 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 218 | bool String::Equals(const char* modified_utf8) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 219 | const int32_t length = GetLength(); |
| 220 | int32_t i = 0; |
| 221 | while (i < length) { |
| 222 | const uint32_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 223 | if (ch == '\0') { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 224 | return false; |
| 225 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 226 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 227 | if (GetLeadingUtf16Char(ch) != CharAt(i++)) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
| 231 | const uint16_t trailing = GetTrailingUtf16Char(ch); |
| 232 | if (trailing != 0) { |
| 233 | if (i == length) { |
| 234 | return false; |
| 235 | } |
| 236 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 237 | if (CharAt(i++) != trailing) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 241 | } |
| 242 | return *modified_utf8 == '\0'; |
| 243 | } |
| 244 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 245 | bool String::Equals(const StringPiece& modified_utf8) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 246 | const int32_t length = GetLength(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 247 | const char* p = modified_utf8.data(); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 248 | for (int32_t i = 0; i < length; ++i) { |
| 249 | uint32_t ch = GetUtf16FromUtf8(&p); |
| 250 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 251 | if (GetLeadingUtf16Char(ch) != CharAt(i)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 252 | return false; |
| 253 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 254 | |
| 255 | const uint16_t trailing = GetTrailingUtf16Char(ch); |
| 256 | if (trailing != 0) { |
| 257 | if (i == (length - 1)) { |
| 258 | return false; |
| 259 | } |
| 260 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 261 | if (CharAt(++i) != trailing) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 265 | } |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | // 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] | 270 | std::string String::ToModifiedUtf8() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 271 | size_t byte_count = GetUtfLength(); |
| 272 | std::string result(byte_count, static_cast<char>(0)); |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 273 | if (IsCompressed()) { |
| 274 | for (size_t i = 0; i < byte_count; ++i) { |
| 275 | result[i] = static_cast<char>(CharAt(i)); |
| 276 | } |
| 277 | } else { |
| 278 | const uint16_t* chars = GetValue(); |
| 279 | ConvertUtf16ToModifiedUtf8(&result[0], byte_count, chars, GetLength()); |
| 280 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 281 | return result; |
| 282 | } |
| 283 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 284 | int32_t String::CompareTo(String* rhs) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 285 | // Quick test for comparison of a string with itself. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 286 | String* lhs = this; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 287 | if (lhs == rhs) { |
| 288 | return 0; |
| 289 | } |
| 290 | // TODO: is this still true? |
| 291 | // The annoying part here is that 0x00e9 - 0xffff != 0x00ea, |
| 292 | // because the interpreter converts the characters to 32-bit integers |
| 293 | // *without* sign extension before it subtracts them (which makes some |
| 294 | // sense since "char" is unsigned). So what we get is the result of |
| 295 | // 0x000000e9 - 0x0000ffff, which is 0xffff00ea. |
Andreas Gampe | 4d0589c | 2014-06-10 16:10:56 -0700 | [diff] [blame] | 296 | int32_t lhsCount = lhs->GetLength(); |
| 297 | int32_t rhsCount = rhs->GetLength(); |
| 298 | int32_t countDiff = lhsCount - rhsCount; |
| 299 | int32_t minCount = (countDiff < 0) ? lhsCount : rhsCount; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 300 | if (lhs->IsCompressed() && rhs->IsCompressed()) { |
| 301 | int32_t comparison = memcmp(lhs->GetValueCompressed(), rhs->GetValueCompressed(), minCount * sizeof(uint8_t)); |
| 302 | if (comparison != 0) { |
| 303 | return comparison; |
| 304 | } |
| 305 | } else if (lhs->IsCompressed() || rhs->IsCompressed()) { |
| 306 | for (int32_t i = 0; i < minCount; ++i) { |
| 307 | if (lhs->CharAt(i) != rhs->CharAt(i)) { |
| 308 | return static_cast<int32_t>(lhs->CharAt(i)) - static_cast<int32_t>(rhs->CharAt(i)); |
| 309 | } |
| 310 | } |
| 311 | } else { |
| 312 | const uint16_t* lhsChars = lhs->GetValue(); |
| 313 | const uint16_t* rhsChars = rhs->GetValue(); |
| 314 | int32_t otherRes = MemCmp16(lhsChars, rhsChars, minCount); |
| 315 | if (otherRes != 0) { |
| 316 | return otherRes; |
| 317 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 318 | } |
| 319 | return countDiff; |
| 320 | } |
| 321 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 322 | void String::VisitRoots(RootVisitor* visitor) { |
| 323 | java_lang_String_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 326 | CharArray* String::ToCharArray(Thread* self) { |
| 327 | StackHandleScope<1> hs(self); |
| 328 | Handle<String> string(hs.NewHandle(this)); |
| 329 | CharArray* result = CharArray::Alloc(self, GetLength()); |
Mathieu Chartier | 04e983a | 2015-11-13 08:36:59 -0800 | [diff] [blame] | 330 | if (result != nullptr) { |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 331 | if (string->IsCompressed()) { |
| 332 | int32_t length = string->GetLength(); |
| 333 | for (int i = 0; i < length; ++i) { |
| 334 | result->GetData()[i] = string->CharAt(i); |
| 335 | } |
| 336 | } else { |
| 337 | memcpy(result->GetData(), string->GetValue(), string->GetLength() * sizeof(uint16_t)); |
| 338 | } |
Mathieu Chartier | 04e983a | 2015-11-13 08:36:59 -0800 | [diff] [blame] | 339 | } else { |
| 340 | self->AssertPendingOOMException(); |
| 341 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 342 | return result; |
| 343 | } |
| 344 | |
| 345 | void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) { |
| 346 | uint16_t* data = array->GetData() + index; |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 347 | if (IsCompressed()) { |
| 348 | for (int i = start; i < end; ++i) { |
| 349 | data[i-start] = CharAt(i); |
| 350 | } |
| 351 | } else { |
| 352 | uint16_t* value = GetValue() + start; |
| 353 | memcpy(data, value, (end - start) * sizeof(uint16_t)); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | bool String::IsValueNull() { |
| 358 | return (IsCompressed()) ? (GetValueCompressed() == nullptr) : (GetValue() == nullptr); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 361 | } // namespace mirror |
| 362 | } // namespace art |