blob: 68642d84f7ea1ffb20ce10f1123ac632c684c031 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080019#include <utils/Log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080020
Steven Morelandd21cfab2017-03-10 08:58:36 -080021#include <ctype.h>
22
Sergio Girod2529f22015-09-23 16:22:59 +010023#include "SharedBuffer.h"
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024
Kenny Root9a2d83e2009-12-04 09:38:48 -080025namespace android {
26
Vic Yang9fb93ed2019-09-05 13:18:27 -070027static const StaticString16 emptyString(u"");
Steven Moreland241b93c2018-03-06 09:11:29 -080028static inline char16_t* getEmptyString() {
Vic Yang9fb93ed2019-09-05 13:18:27 -070029 return const_cast<char16_t*>(emptyString.string());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080030}
31
32// ---------------------------------------------------------------------------
33
Vic Yang9fb93ed2019-09-05 13:18:27 -070034void* String16::alloc(size_t size)
35{
36 SharedBuffer* buf = SharedBuffer::alloc(size);
37 buf->mClientMetadata = kIsSharedBufferAllocated;
38 return buf;
39}
40
41char16_t* String16::allocFromUTF8(const char* u8str, size_t u8len)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080042{
Kenny Rootba0165b2010-11-09 14:37:23 -080043 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();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050 }
Kenny Rootba0165b2010-11-09 14:37:23 -080051
Vic Yang9fb93ed2019-09-05 13:18:27 -070052 SharedBuffer* buf = static_cast<SharedBuffer*>(alloc(sizeof(char16_t) * (u16len + 1)));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080053 if (buf) {
Kenny Rootba0165b2010-11-09 14:37:23 -080054 u8cur = (const uint8_t*) u8str;
55 char16_t* u16str = (char16_t*)buf->data();
56
Sergio Giro1dcc0c82016-07-20 20:01:33 +010057 utf8_to_utf16(u8cur, u8len, u16str, ((size_t) u16len) + 1);
Kenny Root9a2d83e2009-12-04 09:38:48 -080058
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080059 //printf("Created UTF-16 string from UTF-8 \"%s\":", in);
60 //printHexData(1, str, buf->size(), 16, 1);
61 //printf("\n");
Samuel Tanf9d16ef2016-02-16 15:17:10 -080062
Kenny Rootba0165b2010-11-09 14:37:23 -080063 return u16str;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080064 }
Kenny Rootba0165b2010-11-09 14:37:23 -080065
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080066 return getEmptyString();
67}
68
Vic Yang9fb93ed2019-09-05 13:18:27 -070069char16_t* String16::allocFromUTF16(const char16_t* u16str, size_t u16len) {
Steven Moreland977f72f2018-03-01 11:03:04 -080070 if (u16len >= SIZE_MAX / sizeof(char16_t)) {
71 android_errorWriteLog(0x534e4554, "73826242");
72 abort();
73 }
74
Vic Yang9fb93ed2019-09-05 13:18:27 -070075 SharedBuffer* buf = static_cast<SharedBuffer*>(alloc((u16len + 1) * sizeof(char16_t)));
Steven Moreland977f72f2018-03-01 11:03:04 -080076 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
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080086// ---------------------------------------------------------------------------
87
88String16::String16()
89 : mString(getEmptyString())
90{
91}
92
93String16::String16(const String16& o)
94 : mString(o.mString)
95{
Vic Yang9fb93ed2019-09-05 13:18:27 -070096 acquire();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080097}
98
Jooyung Han98b396e2021-06-27 03:30:42 +090099String16::String16(String16&& o) noexcept
100 : mString(o.mString)
101{
102 o.mString = getEmptyString();
103}
104
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800105String16::String16(const String16& o, size_t len, size_t begin)
106 : mString(getEmptyString())
107{
108 setTo(o, len, begin);
109}
110
Steven Moreland977f72f2018-03-01 11:03:04 -0800111String16::String16(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o))) {}
Samuel Tanf9d16ef2016-02-16 15:17:10 -0800112
Steven Moreland977f72f2018-03-01 11:03:04 -0800113String16::String16(const char16_t* o, size_t len) : mString(allocFromUTF16(o, len)) {}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800114
115String16::String16(const String8& o)
116 : mString(allocFromUTF8(o.string(), o.size()))
117{
118}
119
120String16::String16(const char* o)
121 : mString(allocFromUTF8(o, strlen(o)))
122{
123}
124
125String16::String16(const char* o, size_t len)
126 : mString(allocFromUTF8(o, len))
127{
128}
129
130String16::~String16()
131{
Vic Yang9fb93ed2019-09-05 13:18:27 -0700132 release();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800133}
134
Jooyung Han98b396e2021-06-27 03:30:42 +0900135String16& String16::operator=(String16&& other) noexcept {
136 release();
137 mString = other.mString;
138 other.mString = getEmptyString();
139 return *this;
140}
141
Sergio Girod2529f22015-09-23 16:22:59 +0100142size_t String16::size() const
143{
Vic Yang9fb93ed2019-09-05 13:18:27 -0700144 if (isStaticString()) {
145 return staticStringSize();
146 } else {
147 return SharedBuffer::sizeFromData(mString) / sizeof(char16_t) - 1;
148 }
Sergio Girod2529f22015-09-23 16:22:59 +0100149}
150
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800151void String16::setTo(const String16& other)
152{
Vic Yang9fb93ed2019-09-05 13:18:27 -0700153 release();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800154 mString = other.mString;
Vic Yang9fb93ed2019-09-05 13:18:27 -0700155 acquire();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800156}
157
158status_t String16::setTo(const String16& other, size_t len, size_t begin)
159{
160 const size_t N = other.size();
161 if (begin >= N) {
Vic Yang9fb93ed2019-09-05 13:18:27 -0700162 release();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800163 mString = getEmptyString();
Elliott Hughes643268f2018-10-08 11:10:11 -0700164 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800165 }
166 if ((begin+len) > N) len = N-begin;
167 if (begin == 0 && len == N) {
168 setTo(other);
Elliott Hughes643268f2018-10-08 11:10:11 -0700169 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800170 }
171
172 if (&other == this) {
173 LOG_ALWAYS_FATAL("Not implemented");
174 }
175
176 return setTo(other.string()+begin, len);
177}
178
179status_t String16::setTo(const char16_t* other)
180{
181 return setTo(other, strlen16(other));
182}
183
184status_t String16::setTo(const char16_t* other, size_t len)
185{
Steven Moreland977f72f2018-03-01 11:03:04 -0800186 if (len >= SIZE_MAX / sizeof(char16_t)) {
187 android_errorWriteLog(0x534e4554, "73826242");
188 abort();
189 }
190
Vic Yang9fb93ed2019-09-05 13:18:27 -0700191 SharedBuffer* buf = static_cast<SharedBuffer*>(editResize((len + 1) * sizeof(char16_t)));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800192 if (buf) {
193 char16_t* str = (char16_t*)buf->data();
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800194 memmove(str, other, len*sizeof(char16_t));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800195 str[len] = 0;
196 mString = str;
Elliott Hughes643268f2018-10-08 11:10:11 -0700197 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800198 }
199 return NO_MEMORY;
200}
201
Elliott Hughesa6be6f02021-06-10 17:06:26 -0700202status_t String16::append(const String16& other) {
203 return append(other.string(), other.size());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800204}
205
Elliott Hughesa6be6f02021-06-10 17:06:26 -0700206status_t String16::append(const char16_t* chrs, size_t otherLen) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800207 const size_t myLen = size();
Samuel Tanf9d16ef2016-02-16 15:17:10 -0800208
Elliott Hughesa6be6f02021-06-10 17:06:26 -0700209 if (myLen == 0) return setTo(chrs, otherLen);
Steven Moreland977f72f2018-03-01 11:03:04 -0800210
Elliott Hughesa6be6f02021-06-10 17:06:26 -0700211 if (otherLen == 0) return OK;
212
213 size_t size = myLen;
214 if (__builtin_add_overflow(size, otherLen, &size) ||
215 __builtin_add_overflow(size, 1, &size) ||
216 __builtin_mul_overflow(size, sizeof(char16_t), &size)) return NO_MEMORY;
217
218 SharedBuffer* buf = static_cast<SharedBuffer*>(editResize(size));
219 if (!buf) return NO_MEMORY;
220
221 char16_t* str = static_cast<char16_t*>(buf->data());
222 memcpy(str + myLen, chrs, otherLen * sizeof(char16_t));
223 str[myLen + otherLen] = 0;
224 mString = str;
225 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800226}
227
Elliott Hughesa6be6f02021-06-10 17:06:26 -0700228status_t String16::insert(size_t pos, const char16_t* chrs) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800229 return insert(pos, chrs, strlen16(chrs));
230}
231
Elliott Hughesa6be6f02021-06-10 17:06:26 -0700232status_t String16::insert(size_t pos, const char16_t* chrs, size_t otherLen) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800233 const size_t myLen = size();
Elliott Hughesa6be6f02021-06-10 17:06:26 -0700234
235 if (myLen == 0) return setTo(chrs, otherLen);
236
237 if (otherLen == 0) return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800238
239 if (pos > myLen) pos = myLen;
240
Elliott Hughesa6be6f02021-06-10 17:06:26 -0700241 size_t size = myLen;
242 if (__builtin_add_overflow(size, otherLen, &size) ||
243 __builtin_add_overflow(size, 1, &size) ||
244 __builtin_mul_overflow(size, sizeof(char16_t), &size)) return NO_MEMORY;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800245
Elliott Hughesa6be6f02021-06-10 17:06:26 -0700246 SharedBuffer* buf = static_cast<SharedBuffer*>(editResize(size));
247 if (!buf) return NO_MEMORY;
248
249 char16_t* str = static_cast<char16_t*>(buf->data());
250 if (pos < myLen) memmove(str + pos + otherLen, str + pos, (myLen - pos) * sizeof(char16_t));
251 memcpy(str + pos, chrs, otherLen * sizeof(char16_t));
252 str[myLen + otherLen] = 0;
253 mString = str;
254 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800255}
256
257ssize_t String16::findFirst(char16_t c) const
258{
259 const char16_t* str = string();
260 const char16_t* p = str;
261 const char16_t* e = p + size();
262 while (p < e) {
263 if (*p == c) {
264 return p-str;
265 }
266 p++;
267 }
268 return -1;
269}
270
271ssize_t String16::findLast(char16_t c) const
272{
273 const char16_t* str = string();
274 const char16_t* p = str;
275 const char16_t* e = p + size();
276 while (p < e) {
277 e--;
278 if (*e == c) {
279 return e-str;
280 }
281 }
282 return -1;
283}
284
285bool String16::startsWith(const String16& prefix) const
286{
287 const size_t ps = prefix.size();
288 if (ps > size()) return false;
289 return strzcmp16(mString, ps, prefix.string(), ps) == 0;
290}
291
292bool String16::startsWith(const char16_t* prefix) const
293{
294 const size_t ps = strlen16(prefix);
295 if (ps > size()) return false;
296 return strncmp16(mString, prefix, ps) == 0;
297}
298
Michael Wright5bacef32016-05-09 14:43:31 +0100299bool String16::contains(const char16_t* chrs) const
300{
301 return strstr16(mString, chrs) != nullptr;
302}
303
Vic Yang9fb93ed2019-09-05 13:18:27 -0700304void* String16::edit() {
305 SharedBuffer* buf;
306 if (isStaticString()) {
307 buf = static_cast<SharedBuffer*>(alloc((size() + 1) * sizeof(char16_t)));
308 if (buf) {
Vic Yang9fb93ed2019-09-05 13:18:27 -0700309 memcpy(buf->data(), mString, (size() + 1) * sizeof(char16_t));
310 }
311 } else {
312 buf = SharedBuffer::bufferFromData(mString)->edit();
313 buf->mClientMetadata = kIsSharedBufferAllocated;
314 }
315 return buf;
316}
317
318void* String16::editResize(size_t newSize) {
319 SharedBuffer* buf;
320 if (isStaticString()) {
321 size_t copySize = (size() + 1) * sizeof(char16_t);
322 if (newSize < copySize) {
323 copySize = newSize;
324 }
325 buf = static_cast<SharedBuffer*>(alloc(newSize));
326 if (buf) {
Vic Yang9fb93ed2019-09-05 13:18:27 -0700327 memcpy(buf->data(), mString, copySize);
328 }
329 } else {
330 buf = SharedBuffer::bufferFromData(mString)->editResize(newSize);
331 buf->mClientMetadata = kIsSharedBufferAllocated;
332 }
333 return buf;
334}
335
336void String16::acquire()
337{
338 if (!isStaticString()) {
339 SharedBuffer::bufferFromData(mString)->acquire();
340 }
341}
342
343void String16::release()
344{
345 if (!isStaticString()) {
346 SharedBuffer::bufferFromData(mString)->release();
347 }
348}
349
350bool String16::isStaticString() const {
351 // See String16.h for notes on the memory layout of String16::StaticData and
352 // SharedBuffer.
353 static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4);
354 const uint32_t* p = reinterpret_cast<const uint32_t*>(mString);
355 return (*(p - 1) & kIsSharedBufferAllocated) == 0;
356}
357
358size_t String16::staticStringSize() const {
359 // See String16.h for notes on the memory layout of String16::StaticData and
360 // SharedBuffer.
361 static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4);
362 const uint32_t* p = reinterpret_cast<const uint32_t*>(mString);
363 return static_cast<size_t>(*(p - 1));
364}
365
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800366status_t String16::replaceAll(char16_t replaceThis, char16_t withThis)
367{
368 const size_t N = size();
369 const char16_t* str = string();
Vic Yang9fb93ed2019-09-05 13:18:27 -0700370 char16_t* edited = nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800371 for (size_t i=0; i<N; i++) {
372 if (str[i] == replaceThis) {
Vic Yang9fb93ed2019-09-05 13:18:27 -0700373 if (!edited) {
374 SharedBuffer* buf = static_cast<SharedBuffer*>(edit());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800375 if (!buf) {
376 return NO_MEMORY;
377 }
Vic Yang9fb93ed2019-09-05 13:18:27 -0700378 edited = (char16_t*)buf->data();
379 mString = str = edited;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800380 }
Vic Yang9fb93ed2019-09-05 13:18:27 -0700381 edited[i] = withThis;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800382 }
383 }
Elliott Hughes643268f2018-10-08 11:10:11 -0700384 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800385}
386
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800387}; // namespace android