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 "arch/memcmp16.h"
20 #include "array.h"
21 #include "class-inl.h"
22 #include "gc/accounting/card_table-inl.h"
23 #include "handle_scope-inl.h"
24 #include "intern_table.h"
25 #include "object-inl.h"
26 #include "runtime.h"
27 #include "string-inl.h"
28 #include "thread.h"
29 #include "utf-inl.h"
30 
31 namespace art {
32 namespace mirror {
33 
34 // TODO: get global references for these
35 GcRoot<Class> String::java_lang_String_;
36 
FastIndexOf(int32_t ch,int32_t start)37 int32_t String::FastIndexOf(int32_t ch, int32_t start) {
38   int32_t count = GetLength();
39   if (start < 0) {
40     start = 0;
41   } else if (start > count) {
42     start = count;
43   }
44   const uint16_t* chars = GetValue();
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 
SetClass(Class * java_lang_String)55 void String::SetClass(Class* java_lang_String) {
56   CHECK(java_lang_String_.IsNull());
57   CHECK(java_lang_String != nullptr);
58   CHECK(java_lang_String->IsStringClass());
59   java_lang_String_ = GcRoot<Class>(java_lang_String);
60 }
61 
ResetClass()62 void String::ResetClass() {
63   CHECK(!java_lang_String_.IsNull());
64   java_lang_String_ = GcRoot<Class>(nullptr);
65 }
66 
ComputeHashCode()67 int String::ComputeHashCode() {
68   const int32_t hash_code = ComputeUtf16Hash(GetValue(), GetLength());
69   SetHashCode(hash_code);
70   return hash_code;
71 }
72 
GetUtfLength()73 int32_t String::GetUtfLength() {
74   return CountUtf8Bytes(GetValue(), GetLength());
75 }
76 
SetCharAt(int32_t index,uint16_t c)77 void String::SetCharAt(int32_t index, uint16_t c) {
78   DCHECK((index >= 0) && (index < count_));
79   GetValue()[index] = c;
80 }
81 
AllocFromStrings(Thread * self,Handle<String> string,Handle<String> string2)82 String* String::AllocFromStrings(Thread* self, Handle<String> string, Handle<String> string2) {
83   int32_t length = string->GetLength();
84   int32_t length2 = string2->GetLength();
85   gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
86   SetStringCountVisitor visitor(length + length2);
87   String* new_string = Alloc<true>(self, length + length2, allocator_type, visitor);
88   if (UNLIKELY(new_string == nullptr)) {
89     return nullptr;
90   }
91   uint16_t* new_value = new_string->GetValue();
92   memcpy(new_value, string->GetValue(), length * sizeof(uint16_t));
93   memcpy(new_value + length, string2->GetValue(), length2 * sizeof(uint16_t));
94   return new_string;
95 }
96 
AllocFromUtf16(Thread * self,int32_t utf16_length,const uint16_t * utf16_data_in)97 String* String::AllocFromUtf16(Thread* self, int32_t utf16_length, const uint16_t* utf16_data_in) {
98   CHECK(utf16_data_in != nullptr || utf16_length == 0);
99   gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
100   SetStringCountVisitor visitor(utf16_length);
101   String* string = Alloc<true>(self, utf16_length, allocator_type, visitor);
102   if (UNLIKELY(string == nullptr)) {
103     return nullptr;
104   }
105   uint16_t* array = string->GetValue();
106   memcpy(array, utf16_data_in, utf16_length * sizeof(uint16_t));
107   return string;
108 }
109 
AllocFromModifiedUtf8(Thread * self,const char * utf)110 String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
111   DCHECK(utf != nullptr);
112   size_t byte_count = strlen(utf);
113   size_t char_count = CountModifiedUtf8Chars(utf, byte_count);
114   return AllocFromModifiedUtf8(self, char_count, utf, byte_count);
115 }
116 
AllocFromModifiedUtf8(Thread * self,int32_t utf16_length,const char * utf8_data_in)117 String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length, const char* utf8_data_in) {
118   return AllocFromModifiedUtf8(self, utf16_length, utf8_data_in, strlen(utf8_data_in));
119 }
120 
AllocFromModifiedUtf8(Thread * self,int32_t utf16_length,const char * utf8_data_in,int32_t utf8_length)121 String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
122                                       const char* utf8_data_in, int32_t utf8_length) {
123   gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
124   SetStringCountVisitor visitor(utf16_length);
125   String* string = Alloc<true>(self, utf16_length, allocator_type, visitor);
126   if (UNLIKELY(string == nullptr)) {
127     return nullptr;
128   }
129   uint16_t* utf16_data_out = string->GetValue();
130   ConvertModifiedUtf8ToUtf16(utf16_data_out, utf16_length, utf8_data_in, utf8_length);
131   return string;
132 }
133 
Equals(String * that)134 bool String::Equals(String* that) {
135   if (this == that) {
136     // Quick reference equality test
137     return true;
138   } else if (that == nullptr) {
139     // Null isn't an instanceof anything
140     return false;
141   } else if (this->GetLength() != that->GetLength()) {
142     // Quick length inequality test
143     return false;
144   } else {
145     // Note: don't short circuit on hash code as we're presumably here as the
146     // hash code was already equal
147     for (int32_t i = 0; i < that->GetLength(); ++i) {
148       if (this->CharAt(i) != that->CharAt(i)) {
149         return false;
150       }
151     }
152     return true;
153   }
154 }
155 
Equals(const uint16_t * that_chars,int32_t that_offset,int32_t that_length)156 bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) {
157   if (this->GetLength() != that_length) {
158     return false;
159   } else {
160     for (int32_t i = 0; i < that_length; ++i) {
161       if (this->CharAt(i) != that_chars[that_offset + i]) {
162         return false;
163       }
164     }
165     return true;
166   }
167 }
168 
Equals(const char * modified_utf8)169 bool String::Equals(const char* modified_utf8) {
170   const int32_t length = GetLength();
171   int32_t i = 0;
172   while (i < length) {
173     const uint32_t ch = GetUtf16FromUtf8(&modified_utf8);
174     if (ch == '\0') {
175       return false;
176     }
177 
178     if (GetLeadingUtf16Char(ch) != CharAt(i++)) {
179       return false;
180     }
181 
182     const uint16_t trailing = GetTrailingUtf16Char(ch);
183     if (trailing != 0) {
184       if (i == length) {
185         return false;
186       }
187 
188       if (CharAt(i++) != trailing) {
189         return false;
190       }
191     }
192   }
193   return *modified_utf8 == '\0';
194 }
195 
Equals(const StringPiece & modified_utf8)196 bool String::Equals(const StringPiece& modified_utf8) {
197   const int32_t length = GetLength();
198   const char* p = modified_utf8.data();
199   for (int32_t i = 0; i < length; ++i) {
200     uint32_t ch = GetUtf16FromUtf8(&p);
201 
202     if (GetLeadingUtf16Char(ch) != CharAt(i)) {
203       return false;
204     }
205 
206     const uint16_t trailing = GetTrailingUtf16Char(ch);
207     if (trailing != 0) {
208       if (i == (length - 1)) {
209         return false;
210       }
211 
212       if (CharAt(++i) != trailing) {
213         return false;
214       }
215     }
216   }
217   return true;
218 }
219 
220 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
ToModifiedUtf8()221 std::string String::ToModifiedUtf8() {
222   const uint16_t* chars = GetValue();
223   size_t byte_count = GetUtfLength();
224   std::string result(byte_count, static_cast<char>(0));
225   ConvertUtf16ToModifiedUtf8(&result[0], byte_count, chars, GetLength());
226   return result;
227 }
228 
CompareTo(String * rhs)229 int32_t String::CompareTo(String* rhs) {
230   // Quick test for comparison of a string with itself.
231   String* lhs = this;
232   if (lhs == rhs) {
233     return 0;
234   }
235   // TODO: is this still true?
236   // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
237   // because the interpreter converts the characters to 32-bit integers
238   // *without* sign extension before it subtracts them (which makes some
239   // sense since "char" is unsigned).  So what we get is the result of
240   // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
241   int32_t lhsCount = lhs->GetLength();
242   int32_t rhsCount = rhs->GetLength();
243   int32_t countDiff = lhsCount - rhsCount;
244   int32_t minCount = (countDiff < 0) ? lhsCount : rhsCount;
245   const uint16_t* lhsChars = lhs->GetValue();
246   const uint16_t* rhsChars = rhs->GetValue();
247   int32_t otherRes = MemCmp16(lhsChars, rhsChars, minCount);
248   if (otherRes != 0) {
249     return otherRes;
250   }
251   return countDiff;
252 }
253 
VisitRoots(RootVisitor * visitor)254 void String::VisitRoots(RootVisitor* visitor) {
255   java_lang_String_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
256 }
257 
ToCharArray(Thread * self)258 CharArray* String::ToCharArray(Thread* self) {
259   StackHandleScope<1> hs(self);
260   Handle<String> string(hs.NewHandle(this));
261   CharArray* result = CharArray::Alloc(self, GetLength());
262   if (result != nullptr) {
263     memcpy(result->GetData(), string->GetValue(), string->GetLength() * sizeof(uint16_t));
264   } else {
265     self->AssertPendingOOMException();
266   }
267   return result;
268 }
269 
GetChars(int32_t start,int32_t end,Handle<CharArray> array,int32_t index)270 void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) {
271   uint16_t* data = array->GetData() + index;
272   uint16_t* value = GetValue() + start;
273   memcpy(data, value, (end - start) * sizeof(uint16_t));
274 }
275 
276 }  // namespace mirror
277 }  // namespace art
278