1 /*
2 * Copyright (C) 2005 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 <utils/String16.h>
18
19 #include <utils/Log.h>
20
21 #include <ctype.h>
22
23 #include "SharedBuffer.h"
24
25 namespace android {
26
27 static const StaticString16 emptyString(u"");
getEmptyString()28 static inline char16_t* getEmptyString() {
29 return const_cast<char16_t*>(emptyString.string());
30 }
31
32 // ---------------------------------------------------------------------------
33
alloc(size_t size)34 void* String16::alloc(size_t size)
35 {
36 SharedBuffer* buf = SharedBuffer::alloc(size);
37 buf->mClientMetadata = kIsSharedBufferAllocated;
38 return buf;
39 }
40
allocFromUTF8(const char * u8str,size_t u8len)41 char16_t* String16::allocFromUTF8(const char* u8str, size_t u8len)
42 {
43 if (u8len == 0) return getEmptyString();
44
45 const uint8_t* u8cur = (const uint8_t*) u8str;
46
47 const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len);
48 if (u16len < 0) {
49 return getEmptyString();
50 }
51
52 SharedBuffer* buf = static_cast<SharedBuffer*>(alloc(sizeof(char16_t) * (u16len + 1)));
53 if (buf) {
54 u8cur = (const uint8_t*) u8str;
55 char16_t* u16str = (char16_t*)buf->data();
56
57 utf8_to_utf16(u8cur, u8len, u16str, ((size_t) u16len) + 1);
58
59 //printf("Created UTF-16 string from UTF-8 \"%s\":", in);
60 //printHexData(1, str, buf->size(), 16, 1);
61 //printf("\n");
62
63 return u16str;
64 }
65
66 return getEmptyString();
67 }
68
allocFromUTF16(const char16_t * u16str,size_t u16len)69 char16_t* String16::allocFromUTF16(const char16_t* u16str, size_t u16len) {
70 if (u16len >= SIZE_MAX / sizeof(char16_t)) {
71 android_errorWriteLog(0x534e4554, "73826242");
72 abort();
73 }
74
75 SharedBuffer* buf = static_cast<SharedBuffer*>(alloc((u16len + 1) * sizeof(char16_t)));
76 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
77 if (buf) {
78 char16_t* str = (char16_t*)buf->data();
79 memcpy(str, u16str, u16len * sizeof(char16_t));
80 str[u16len] = 0;
81 return str;
82 }
83 return getEmptyString();
84 }
85
86 // ---------------------------------------------------------------------------
87
String16()88 String16::String16()
89 : mString(getEmptyString())
90 {
91 }
92
String16(const String16 & o)93 String16::String16(const String16& o)
94 : mString(o.mString)
95 {
96 acquire();
97 }
98
String16(const String16 & o,size_t len,size_t begin)99 String16::String16(const String16& o, size_t len, size_t begin)
100 : mString(getEmptyString())
101 {
102 setTo(o, len, begin);
103 }
104
String16(const char16_t * o)105 String16::String16(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o))) {}
106
String16(const char16_t * o,size_t len)107 String16::String16(const char16_t* o, size_t len) : mString(allocFromUTF16(o, len)) {}
108
String16(const String8 & o)109 String16::String16(const String8& o)
110 : mString(allocFromUTF8(o.string(), o.size()))
111 {
112 }
113
String16(const char * o)114 String16::String16(const char* o)
115 : mString(allocFromUTF8(o, strlen(o)))
116 {
117 }
118
String16(const char * o,size_t len)119 String16::String16(const char* o, size_t len)
120 : mString(allocFromUTF8(o, len))
121 {
122 }
123
~String16()124 String16::~String16()
125 {
126 release();
127 }
128
size() const129 size_t String16::size() const
130 {
131 if (isStaticString()) {
132 return staticStringSize();
133 } else {
134 return SharedBuffer::sizeFromData(mString) / sizeof(char16_t) - 1;
135 }
136 }
137
setTo(const String16 & other)138 void String16::setTo(const String16& other)
139 {
140 release();
141 mString = other.mString;
142 acquire();
143 }
144
setTo(const String16 & other,size_t len,size_t begin)145 status_t String16::setTo(const String16& other, size_t len, size_t begin)
146 {
147 const size_t N = other.size();
148 if (begin >= N) {
149 release();
150 mString = getEmptyString();
151 return OK;
152 }
153 if ((begin+len) > N) len = N-begin;
154 if (begin == 0 && len == N) {
155 setTo(other);
156 return OK;
157 }
158
159 if (&other == this) {
160 LOG_ALWAYS_FATAL("Not implemented");
161 }
162
163 return setTo(other.string()+begin, len);
164 }
165
setTo(const char16_t * other)166 status_t String16::setTo(const char16_t* other)
167 {
168 return setTo(other, strlen16(other));
169 }
170
setTo(const char16_t * other,size_t len)171 status_t String16::setTo(const char16_t* other, size_t len)
172 {
173 if (len >= SIZE_MAX / sizeof(char16_t)) {
174 android_errorWriteLog(0x534e4554, "73826242");
175 abort();
176 }
177
178 SharedBuffer* buf = static_cast<SharedBuffer*>(editResize((len + 1) * sizeof(char16_t)));
179 if (buf) {
180 char16_t* str = (char16_t*)buf->data();
181 memmove(str, other, len*sizeof(char16_t));
182 str[len] = 0;
183 mString = str;
184 return OK;
185 }
186 return NO_MEMORY;
187 }
188
append(const String16 & other)189 status_t String16::append(const String16& other) {
190 return append(other.string(), other.size());
191 }
192
append(const char16_t * chrs,size_t otherLen)193 status_t String16::append(const char16_t* chrs, size_t otherLen) {
194 const size_t myLen = size();
195
196 if (myLen == 0) return setTo(chrs, otherLen);
197
198 if (otherLen == 0) return OK;
199
200 size_t size = myLen;
201 if (__builtin_add_overflow(size, otherLen, &size) ||
202 __builtin_add_overflow(size, 1, &size) ||
203 __builtin_mul_overflow(size, sizeof(char16_t), &size)) return NO_MEMORY;
204
205 SharedBuffer* buf = static_cast<SharedBuffer*>(editResize(size));
206 if (!buf) return NO_MEMORY;
207
208 char16_t* str = static_cast<char16_t*>(buf->data());
209 memcpy(str + myLen, chrs, otherLen * sizeof(char16_t));
210 str[myLen + otherLen] = 0;
211 mString = str;
212 return OK;
213 }
214
insert(size_t pos,const char16_t * chrs)215 status_t String16::insert(size_t pos, const char16_t* chrs) {
216 return insert(pos, chrs, strlen16(chrs));
217 }
218
insert(size_t pos,const char16_t * chrs,size_t otherLen)219 status_t String16::insert(size_t pos, const char16_t* chrs, size_t otherLen) {
220 const size_t myLen = size();
221
222 if (myLen == 0) return setTo(chrs, otherLen);
223
224 if (otherLen == 0) return OK;
225
226 if (pos > myLen) pos = myLen;
227
228 size_t size = myLen;
229 if (__builtin_add_overflow(size, otherLen, &size) ||
230 __builtin_add_overflow(size, 1, &size) ||
231 __builtin_mul_overflow(size, sizeof(char16_t), &size)) return NO_MEMORY;
232
233 SharedBuffer* buf = static_cast<SharedBuffer*>(editResize(size));
234 if (!buf) return NO_MEMORY;
235
236 char16_t* str = static_cast<char16_t*>(buf->data());
237 if (pos < myLen) memmove(str + pos + otherLen, str + pos, (myLen - pos) * sizeof(char16_t));
238 memcpy(str + pos, chrs, otherLen * sizeof(char16_t));
239 str[myLen + otherLen] = 0;
240 mString = str;
241 return OK;
242 }
243
findFirst(char16_t c) const244 ssize_t String16::findFirst(char16_t c) const
245 {
246 const char16_t* str = string();
247 const char16_t* p = str;
248 const char16_t* e = p + size();
249 while (p < e) {
250 if (*p == c) {
251 return p-str;
252 }
253 p++;
254 }
255 return -1;
256 }
257
findLast(char16_t c) const258 ssize_t String16::findLast(char16_t c) const
259 {
260 const char16_t* str = string();
261 const char16_t* p = str;
262 const char16_t* e = p + size();
263 while (p < e) {
264 e--;
265 if (*e == c) {
266 return e-str;
267 }
268 }
269 return -1;
270 }
271
startsWith(const String16 & prefix) const272 bool String16::startsWith(const String16& prefix) const
273 {
274 const size_t ps = prefix.size();
275 if (ps > size()) return false;
276 return strzcmp16(mString, ps, prefix.string(), ps) == 0;
277 }
278
startsWith(const char16_t * prefix) const279 bool String16::startsWith(const char16_t* prefix) const
280 {
281 const size_t ps = strlen16(prefix);
282 if (ps > size()) return false;
283 return strncmp16(mString, prefix, ps) == 0;
284 }
285
contains(const char16_t * chrs) const286 bool String16::contains(const char16_t* chrs) const
287 {
288 return strstr16(mString, chrs) != nullptr;
289 }
290
edit()291 void* String16::edit() {
292 SharedBuffer* buf;
293 if (isStaticString()) {
294 buf = static_cast<SharedBuffer*>(alloc((size() + 1) * sizeof(char16_t)));
295 if (buf) {
296 memcpy(buf->data(), mString, (size() + 1) * sizeof(char16_t));
297 }
298 } else {
299 buf = SharedBuffer::bufferFromData(mString)->edit();
300 buf->mClientMetadata = kIsSharedBufferAllocated;
301 }
302 return buf;
303 }
304
editResize(size_t newSize)305 void* String16::editResize(size_t newSize) {
306 SharedBuffer* buf;
307 if (isStaticString()) {
308 size_t copySize = (size() + 1) * sizeof(char16_t);
309 if (newSize < copySize) {
310 copySize = newSize;
311 }
312 buf = static_cast<SharedBuffer*>(alloc(newSize));
313 if (buf) {
314 memcpy(buf->data(), mString, copySize);
315 }
316 } else {
317 buf = SharedBuffer::bufferFromData(mString)->editResize(newSize);
318 buf->mClientMetadata = kIsSharedBufferAllocated;
319 }
320 return buf;
321 }
322
acquire()323 void String16::acquire()
324 {
325 if (!isStaticString()) {
326 SharedBuffer::bufferFromData(mString)->acquire();
327 }
328 }
329
release()330 void String16::release()
331 {
332 if (!isStaticString()) {
333 SharedBuffer::bufferFromData(mString)->release();
334 }
335 }
336
isStaticString() const337 bool String16::isStaticString() const {
338 // See String16.h for notes on the memory layout of String16::StaticData and
339 // SharedBuffer.
340 static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4);
341 const uint32_t* p = reinterpret_cast<const uint32_t*>(mString);
342 return (*(p - 1) & kIsSharedBufferAllocated) == 0;
343 }
344
staticStringSize() const345 size_t String16::staticStringSize() const {
346 // See String16.h for notes on the memory layout of String16::StaticData and
347 // SharedBuffer.
348 static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4);
349 const uint32_t* p = reinterpret_cast<const uint32_t*>(mString);
350 return static_cast<size_t>(*(p - 1));
351 }
352
replaceAll(char16_t replaceThis,char16_t withThis)353 status_t String16::replaceAll(char16_t replaceThis, char16_t withThis)
354 {
355 const size_t N = size();
356 const char16_t* str = string();
357 char16_t* edited = nullptr;
358 for (size_t i=0; i<N; i++) {
359 if (str[i] == replaceThis) {
360 if (!edited) {
361 SharedBuffer* buf = static_cast<SharedBuffer*>(edit());
362 if (!buf) {
363 return NO_MEMORY;
364 }
365 edited = (char16_t*)buf->data();
366 mString = str = edited;
367 }
368 edited[i] = withThis;
369 }
370 }
371 return OK;
372 }
373
374 }; // namespace android
375