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 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 44 | const uint16_t* chars = GetValue(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | const uint16_t* p = chars + start; |
| 46 | const uint16_t* end = chars + count; |
| 47 | while (p < end) { |
| 48 | if (*p++ == ch) { |
| 49 | return (p - 1) - chars; |
| 50 | } |
| 51 | } |
| 52 | return -1; |
| 53 | } |
| 54 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | void String::SetClass(Class* java_lang_String) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 56 | CHECK(java_lang_String_.IsNull()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 57 | CHECK(java_lang_String != nullptr); |
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() { |
| 67 | const int32_t hash_code = ComputeUtf16Hash(GetValue(), GetLength()); |
Mathieu Chartier | cdfd39f | 2014-08-29 18:16:58 -0700 | [diff] [blame] | 68 | SetHashCode(hash_code); |
| 69 | return hash_code; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 72 | int32_t String::GetUtfLength() { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 73 | return CountUtf8Bytes(GetValue(), GetLength()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 76 | void String::SetCharAt(int32_t index, uint16_t c) { |
| 77 | DCHECK((index >= 0) && (index < count_)); |
| 78 | GetValue()[index] = c; |
| 79 | } |
| 80 | |
| 81 | String* String::AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2) { |
| 82 | int32_t length = string->GetLength(); |
| 83 | int32_t length2 = string2->GetLength(); |
| 84 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
| 85 | SetStringCountVisitor visitor(length + length2); |
| 86 | String* new_string = Alloc<true>(self, length + length2, allocator_type, visitor); |
| 87 | if (UNLIKELY(new_string == nullptr)) { |
| 88 | return nullptr; |
| 89 | } |
| 90 | uint16_t* new_value = new_string->GetValue(); |
| 91 | memcpy(new_value, string->GetValue(), length * sizeof(uint16_t)); |
| 92 | memcpy(new_value + length, string2->GetValue(), length2 * sizeof(uint16_t)); |
| 93 | return new_string; |
| 94 | } |
| 95 | |
| 96 | 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] | 97 | CHECK(utf16_data_in != nullptr || utf16_length == 0); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 98 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
| 99 | SetStringCountVisitor visitor(utf16_length); |
| 100 | String* string = Alloc<true>(self, utf16_length, allocator_type, visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 101 | if (UNLIKELY(string == nullptr)) { |
| 102 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 103 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 104 | uint16_t* array = string->GetValue(); |
| 105 | memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | return string; |
| 107 | } |
| 108 | |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 109 | String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) { |
Mathieu Chartier | ed0fc1d | 2014-03-21 14:09:35 -0700 | [diff] [blame] | 110 | DCHECK(utf != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | size_t char_count = CountModifiedUtf8Chars(utf); |
| 112 | return AllocFromModifiedUtf8(self, char_count, utf); |
| 113 | } |
| 114 | |
| 115 | String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, |
| 116 | const char* utf8_data_in) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 117 | gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator(); |
| 118 | SetStringCountVisitor visitor(utf16_length); |
| 119 | String* string = Alloc<true>(self, utf16_length, allocator_type, visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 120 | if (UNLIKELY(string == nullptr)) { |
| 121 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 122 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 123 | uint16_t* utf16_data_out = string->GetValue(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 124 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 125 | return string; |
| 126 | } |
| 127 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 128 | bool String::Equals(String* that) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | if (this == that) { |
| 130 | // Quick reference equality test |
| 131 | return true; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 132 | } else if (that == nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 133 | // Null isn't an instanceof anything |
| 134 | return false; |
| 135 | } else if (this->GetLength() != that->GetLength()) { |
| 136 | // Quick length inequality test |
| 137 | return false; |
| 138 | } else { |
| 139 | // Note: don't short circuit on hash code as we're presumably here as the |
| 140 | // hash code was already equal |
| 141 | for (int32_t i = 0; i < that->GetLength(); ++i) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 142 | if (this->CharAt(i) != that->CharAt(i)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 150 | 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] | 151 | if (this->GetLength() != that_length) { |
| 152 | return false; |
| 153 | } else { |
| 154 | for (int32_t i = 0; i < that_length; ++i) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 155 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | } |
| 162 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 163 | bool String::Equals(const char* modified_utf8) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 164 | const int32_t length = GetLength(); |
| 165 | int32_t i = 0; |
| 166 | while (i < length) { |
| 167 | const uint32_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 168 | if (ch == '\0') { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 169 | return false; |
| 170 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 171 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 172 | if (GetLeadingUtf16Char(ch) != CharAt(i++)) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
| 176 | const uint16_t trailing = GetTrailingUtf16Char(ch); |
| 177 | if (trailing != 0) { |
| 178 | if (i == length) { |
| 179 | return false; |
| 180 | } |
| 181 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 182 | if (CharAt(i++) != trailing) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 186 | } |
| 187 | return *modified_utf8 == '\0'; |
| 188 | } |
| 189 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 190 | bool String::Equals(const StringPiece& modified_utf8) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 191 | const int32_t length = GetLength(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 192 | const char* p = modified_utf8.data(); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 193 | for (int32_t i = 0; i < length; ++i) { |
| 194 | uint32_t ch = GetUtf16FromUtf8(&p); |
| 195 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 196 | if (GetLeadingUtf16Char(ch) != CharAt(i)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 197 | return false; |
| 198 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 199 | |
| 200 | const uint16_t trailing = GetTrailingUtf16Char(ch); |
| 201 | if (trailing != 0) { |
| 202 | if (i == (length - 1)) { |
| 203 | return false; |
| 204 | } |
| 205 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 206 | if (CharAt(++i) != trailing) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | // 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] | 215 | std::string String::ToModifiedUtf8() { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 216 | const uint16_t* chars = GetValue(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 217 | size_t byte_count = GetUtfLength(); |
| 218 | std::string result(byte_count, static_cast<char>(0)); |
| 219 | ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength()); |
| 220 | return result; |
| 221 | } |
| 222 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 223 | int32_t String::CompareTo(String* rhs) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 224 | // Quick test for comparison of a string with itself. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 225 | String* lhs = this; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 226 | if (lhs == rhs) { |
| 227 | return 0; |
| 228 | } |
| 229 | // TODO: is this still true? |
| 230 | // The annoying part here is that 0x00e9 - 0xffff != 0x00ea, |
| 231 | // because the interpreter converts the characters to 32-bit integers |
| 232 | // *without* sign extension before it subtracts them (which makes some |
| 233 | // sense since "char" is unsigned). So what we get is the result of |
| 234 | // 0x000000e9 - 0x0000ffff, which is 0xffff00ea. |
Andreas Gampe | 4d0589c | 2014-06-10 16:10:56 -0700 | [diff] [blame] | 235 | int32_t lhsCount = lhs->GetLength(); |
| 236 | int32_t rhsCount = rhs->GetLength(); |
| 237 | int32_t countDiff = lhsCount - rhsCount; |
| 238 | int32_t minCount = (countDiff < 0) ? lhsCount : rhsCount; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 239 | const uint16_t* lhsChars = lhs->GetValue(); |
| 240 | const uint16_t* rhsChars = rhs->GetValue(); |
Andreas Gampe | 4d0589c | 2014-06-10 16:10:56 -0700 | [diff] [blame] | 241 | int32_t otherRes = MemCmp16(lhsChars, rhsChars, minCount); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 242 | if (otherRes != 0) { |
| 243 | return otherRes; |
| 244 | } |
| 245 | return countDiff; |
| 246 | } |
| 247 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 248 | void String::VisitRoots(RootVisitor* visitor) { |
| 249 | java_lang_String_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 252 | CharArray* String::ToCharArray(Thread* self) { |
| 253 | StackHandleScope<1> hs(self); |
| 254 | Handle<String> string(hs.NewHandle(this)); |
| 255 | CharArray* result = CharArray::Alloc(self, GetLength()); |
| 256 | memcpy(result->GetData(), string->GetValue(), string->GetLength() * sizeof(uint16_t)); |
| 257 | return result; |
| 258 | } |
| 259 | |
| 260 | void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) { |
| 261 | uint16_t* data = array->GetData() + index; |
| 262 | uint16_t* value = GetValue() + start; |
| 263 | memcpy(data, value, (end - start) * sizeof(uint16_t)); |
| 264 | } |
| 265 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 266 | } // namespace mirror |
| 267 | } // namespace art |