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 17 #include "string.h" 18 19 #include "array.h" 20 #include "gc/accounting/card_table-inl.h" 21 #include "intern_table.h" 22 #include "object-inl.h" 23 #include "runtime.h" 24 #include "sirt_ref.h" 25 #include "thread.h" 26 #include "utf.h" 27 28 namespace art { 29 namespace mirror { 30 31 const CharArray* String::GetCharArray() const { 32 return GetFieldObject<const CharArray*>(ValueOffset(), false); 33 } 34 35 void String::ComputeHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 36 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength())); 37 } 38 39 int32_t String::GetUtfLength() const { 40 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength()); 41 } 42 43 int32_t String::FastIndexOf(int32_t ch, int32_t start) const { 44 int32_t count = GetLength(); 45 if (start < 0) { 46 start = 0; 47 } else if (start > count) { 48 start = count; 49 } 50 const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); 51 const uint16_t* p = chars + start; 52 const uint16_t* end = chars + count; 53 while (p < end) { 54 if (*p++ == ch) { 55 return (p - 1) - chars; 56 } 57 } 58 return -1; 59 } 60 61 void String::SetArray(CharArray* new_array) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 62 DCHECK(new_array != NULL); 63 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false); 64 } 65 66 // TODO: get global references for these 67 Class* String::java_lang_String_ = NULL; 68 69 void String::SetClass(Class* java_lang_String) { 70 CHECK(java_lang_String_ == NULL); 71 CHECK(java_lang_String != NULL); 72 java_lang_String_ = java_lang_String; 73 } 74 75 void String::ResetClass() { 76 CHECK(java_lang_String_ != NULL); 77 java_lang_String_ = NULL; 78 } 79 80 String* String::Intern() { 81 return Runtime::Current()->GetInternTable()->InternWeak(this); 82 } 83 84 int32_t String::GetHashCode() { 85 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false); 86 if (result == 0) { 87 ComputeHashCode(); 88 } 89 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false); 90 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0) 91 << ToModifiedUtf8() << " " << result; 92 return result; 93 } 94 95 int32_t String::GetLength() const { 96 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false); 97 DCHECK(result >= 0 && result <= GetCharArray()->GetLength()); 98 return result; 99 } 100 101 uint16_t String::CharAt(int32_t index) const { 102 // TODO: do we need this? Equals is the only caller, and could 103 // bounds check itself. 104 DCHECK_GE(count_, 0); // ensures the unsigned comparison is safe. 105 if (UNLIKELY(static_cast<uint32_t>(index) >= static_cast<uint32_t>(count_))) { 106 Thread* self = Thread::Current(); 107 ThrowLocation throw_location = self->GetCurrentLocationForThrow(); 108 self->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;", 109 "length=%i; index=%i", count_, index); 110 return 0; 111 } 112 return GetCharArray()->Get(index + GetOffset()); 113 } 114 115 String* String::AllocFromUtf16(Thread* self, 116 int32_t utf16_length, 117 const uint16_t* utf16_data_in, 118 int32_t hash_code) { 119 CHECK(utf16_data_in != NULL || utf16_length == 0); 120 String* string = Alloc(self, GetJavaLangString(), utf16_length); 121 if (string == NULL) { 122 return NULL; 123 } 124 // TODO: use 16-bit wide memset variant 125 CharArray* array = const_cast<CharArray*>(string->GetCharArray()); 126 if (array == NULL) { 127 return NULL; 128 } 129 for (int i = 0; i < utf16_length; i++) { 130 array->Set(i, utf16_data_in[i]); 131 } 132 if (hash_code != 0) { 133 string->SetHashCode(hash_code); 134 } else { 135 string->ComputeHashCode(); 136 } 137 return string; 138 } 139 140 String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) { 141 if (utf == NULL) { 142 return NULL; 143 } 144 size_t char_count = CountModifiedUtf8Chars(utf); 145 return AllocFromModifiedUtf8(self, char_count, utf); 146 } 147 148 String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, 149 const char* utf8_data_in) { 150 String* string = Alloc(self, GetJavaLangString(), utf16_length); 151 if (string == NULL) { 152 return NULL; 153 } 154 uint16_t* utf16_data_out = 155 const_cast<uint16_t*>(string->GetCharArray()->GetData()); 156 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); 157 string->ComputeHashCode(); 158 return string; 159 } 160 161 String* String::Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length) { 162 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length)); 163 if (array.get() == NULL) { 164 return NULL; 165 } 166 return Alloc(self, java_lang_String, array.get()); 167 } 168 169 String* String::Alloc(Thread* self, Class* java_lang_String, CharArray* array) { 170 // Hold reference in case AllocObject causes GC. 171 SirtRef<CharArray> array_ref(self, array); 172 String* string = down_cast<String*>(java_lang_String->AllocObject(self)); 173 if (string == NULL) { 174 return NULL; 175 } 176 string->SetArray(array); 177 string->SetCount(array->GetLength()); 178 return string; 179 } 180 181 bool String::Equals(const String* that) const { 182 if (this == that) { 183 // Quick reference equality test 184 return true; 185 } else if (that == NULL) { 186 // Null isn't an instanceof anything 187 return false; 188 } else if (this->GetLength() != that->GetLength()) { 189 // Quick length inequality test 190 return false; 191 } else { 192 // Note: don't short circuit on hash code as we're presumably here as the 193 // hash code was already equal 194 for (int32_t i = 0; i < that->GetLength(); ++i) { 195 if (this->CharAt(i) != that->CharAt(i)) { 196 return false; 197 } 198 } 199 return true; 200 } 201 } 202 203 bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const { 204 if (this->GetLength() != that_length) { 205 return false; 206 } else { 207 for (int32_t i = 0; i < that_length; ++i) { 208 if (this->CharAt(i) != that_chars[that_offset + i]) { 209 return false; 210 } 211 } 212 return true; 213 } 214 } 215 216 bool String::Equals(const char* modified_utf8) const { 217 for (int32_t i = 0; i < GetLength(); ++i) { 218 uint16_t ch = GetUtf16FromUtf8(&modified_utf8); 219 if (ch == '\0' || ch != CharAt(i)) { 220 return false; 221 } 222 } 223 return *modified_utf8 == '\0'; 224 } 225 226 bool String::Equals(const StringPiece& modified_utf8) const { 227 const char* p = modified_utf8.data(); 228 for (int32_t i = 0; i < GetLength(); ++i) { 229 uint16_t ch = GetUtf16FromUtf8(&p); 230 if (ch != CharAt(i)) { 231 return false; 232 } 233 } 234 return true; 235 } 236 237 // Create a modified UTF-8 encoded std::string from a java/lang/String object. 238 std::string String::ToModifiedUtf8() const { 239 const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); 240 size_t byte_count = GetUtfLength(); 241 std::string result(byte_count, static_cast<char>(0)); 242 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength()); 243 return result; 244 } 245 246 #ifdef HAVE__MEMCMP16 247 // "count" is in 16-bit units. 248 extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count); 249 #define MemCmp16 __memcmp16 250 #else 251 static uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) { 252 for (size_t i = 0; i < count; i++) { 253 if (s0[i] != s1[i]) { 254 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]); 255 } 256 } 257 return 0; 258 } 259 #endif 260 261 int32_t String::CompareTo(String* rhs) const { 262 // Quick test for comparison of a string with itself. 263 const String* lhs = this; 264 if (lhs == rhs) { 265 return 0; 266 } 267 // TODO: is this still true? 268 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea, 269 // because the interpreter converts the characters to 32-bit integers 270 // *without* sign extension before it subtracts them (which makes some 271 // sense since "char" is unsigned). So what we get is the result of 272 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea. 273 int lhsCount = lhs->GetLength(); 274 int rhsCount = rhs->GetLength(); 275 int countDiff = lhsCount - rhsCount; 276 int minCount = (countDiff < 0) ? lhsCount : rhsCount; 277 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset(); 278 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset(); 279 int otherRes = MemCmp16(lhsChars, rhsChars, minCount); 280 if (otherRes != 0) { 281 return otherRes; 282 } 283 return countDiff; 284 } 285 286 } // namespace mirror 287 } // namespace art 288 289