string.cc revision b0fa5dc7769c1e054032f39de0a3f6d6dd06f8cf
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-inl.h" 18 19#include "array.h" 20#include "class-inl.h" 21#include "gc/accounting/card_table-inl.h" 22#include "intern_table.h" 23#include "object-inl.h" 24#include "runtime.h" 25#include "sirt_ref.h" 26#include "thread.h" 27#include "utf-inl.h" 28 29namespace art { 30namespace mirror { 31 32// TODO: get global references for these 33Class* String::java_lang_String_ = NULL; 34 35int32_t String::FastIndexOf(int32_t ch, int32_t start) { 36 int32_t count = GetLength(); 37 if (start < 0) { 38 start = 0; 39 } else if (start > count) { 40 start = count; 41 } 42 const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); 43 const uint16_t* p = chars + start; 44 const uint16_t* end = chars + count; 45 while (p < end) { 46 if (*p++ == ch) { 47 return (p - 1) - chars; 48 } 49 } 50 return -1; 51} 52 53void String::SetClass(Class* java_lang_String) { 54 CHECK(java_lang_String_ == NULL); 55 CHECK(java_lang_String != NULL); 56 java_lang_String_ = java_lang_String; 57} 58 59void String::ResetClass() { 60 CHECK(java_lang_String_ != NULL); 61 java_lang_String_ = NULL; 62} 63 64int32_t String::GetHashCode() { 65 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)); 66 if (UNLIKELY(result == 0)) { 67 ComputeHashCode(); 68 } 69 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_)); 70 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0) 71 << ToModifiedUtf8() << " " << result; 72 return result; 73} 74 75void String::ComputeHashCode() { 76 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength())); 77} 78 79int32_t String::GetUtfLength() { 80 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength()); 81} 82 83String* String::AllocFromUtf16(Thread* self, 84 int32_t utf16_length, 85 const uint16_t* utf16_data_in, 86 int32_t hash_code) { 87 CHECK(utf16_data_in != nullptr || utf16_length == 0); 88 String* string = Alloc(self, utf16_length); 89 if (UNLIKELY(string == nullptr)) { 90 return nullptr; 91 } 92 CharArray* array = const_cast<CharArray*>(string->GetCharArray()); 93 if (UNLIKELY(array == nullptr)) { 94 return nullptr; 95 } 96 memcpy(array->GetData(), utf16_data_in, utf16_length * sizeof(uint16_t)); 97 if (hash_code != 0) { 98 DCHECK_EQ(hash_code, ComputeUtf16Hash(utf16_data_in, utf16_length)); 99 string->SetHashCode(hash_code); 100 } else { 101 string->ComputeHashCode(); 102 } 103 return string; 104} 105 106String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) { 107 DCHECK(utf != nullptr); 108 size_t char_count = CountModifiedUtf8Chars(utf); 109 return AllocFromModifiedUtf8(self, char_count, utf); 110} 111 112String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, 113 const char* utf8_data_in) { 114 String* string = Alloc(self, utf16_length); 115 if (UNLIKELY(string == nullptr)) { 116 return nullptr; 117 } 118 uint16_t* utf16_data_out = 119 const_cast<uint16_t*>(string->GetCharArray()->GetData()); 120 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); 121 string->ComputeHashCode(); 122 return string; 123} 124 125String* String::Alloc(Thread* self, int32_t utf16_length) { 126 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length)); 127 if (UNLIKELY(array.get() == nullptr)) { 128 return nullptr; 129 } 130 return Alloc(self, array); 131} 132 133String* String::Alloc(Thread* self, const SirtRef<CharArray>& array) { 134 // Hold reference in case AllocObject causes GC. 135 String* string = down_cast<String*>(GetJavaLangString()->AllocObject(self)); 136 if (LIKELY(string != nullptr)) { 137 string->SetArray(array.get()); 138 string->SetCount(array->GetLength()); 139 } 140 return string; 141} 142 143bool String::Equals(String* that) { 144 if (this == that) { 145 // Quick reference equality test 146 return true; 147 } else if (that == NULL) { 148 // Null isn't an instanceof anything 149 return false; 150 } else if (this->GetLength() != that->GetLength()) { 151 // Quick length inequality test 152 return false; 153 } else { 154 // Note: don't short circuit on hash code as we're presumably here as the 155 // hash code was already equal 156 for (int32_t i = 0; i < that->GetLength(); ++i) { 157 if (this->CharAt(i) != that->CharAt(i)) { 158 return false; 159 } 160 } 161 return true; 162 } 163} 164 165bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) { 166 if (this->GetLength() != that_length) { 167 return false; 168 } else { 169 for (int32_t i = 0; i < that_length; ++i) { 170 if (this->CharAt(i) != that_chars[that_offset + i]) { 171 return false; 172 } 173 } 174 return true; 175 } 176} 177 178bool String::Equals(const char* modified_utf8) { 179 for (int32_t i = 0; i < GetLength(); ++i) { 180 uint16_t ch = GetUtf16FromUtf8(&modified_utf8); 181 if (ch == '\0' || ch != CharAt(i)) { 182 return false; 183 } 184 } 185 return *modified_utf8 == '\0'; 186} 187 188bool String::Equals(const StringPiece& modified_utf8) { 189 const char* p = modified_utf8.data(); 190 for (int32_t i = 0; i < GetLength(); ++i) { 191 uint16_t ch = GetUtf16FromUtf8(&p); 192 if (ch != CharAt(i)) { 193 return false; 194 } 195 } 196 return true; 197} 198 199// Create a modified UTF-8 encoded std::string from a java/lang/String object. 200std::string String::ToModifiedUtf8() { 201 const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); 202 size_t byte_count = GetUtfLength(); 203 std::string result(byte_count, static_cast<char>(0)); 204 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength()); 205 return result; 206} 207 208#ifdef HAVE__MEMCMP16 209// "count" is in 16-bit units. 210extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count); 211#define MemCmp16 __memcmp16 212#else 213static uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) { 214 for (size_t i = 0; i < count; i++) { 215 if (s0[i] != s1[i]) { 216 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]); 217 } 218 } 219 return 0; 220} 221#endif 222 223int32_t String::CompareTo(String* rhs) { 224 // Quick test for comparison of a string with itself. 225 String* lhs = this; 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. 235 int lhsCount = lhs->GetLength(); 236 int rhsCount = rhs->GetLength(); 237 int countDiff = lhsCount - rhsCount; 238 int minCount = (countDiff < 0) ? lhsCount : rhsCount; 239 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset(); 240 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset(); 241 int otherRes = MemCmp16(lhsChars, rhsChars, minCount); 242 if (otherRes != 0) { 243 return otherRes; 244 } 245 return countDiff; 246} 247 248void String::VisitRoots(RootCallback* callback, void* arg) { 249 if (java_lang_String_ != nullptr) { 250 callback(reinterpret_cast<mirror::Object**>(&java_lang_String_), arg, 0, kRootStickyClass); 251 } 252} 253 254} // namespace mirror 255} // namespace art 256