blob: 21a036604ded05a4c2de52531c8a3283bdee7cc4 [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>
Kenny Rootba0165b2010-11-09 14:37:23 -080020#include <utils/Unicode.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080021#include <utils/String8.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080022#include <utils/threads.h>
23
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024#include <memory.h>
25#include <stdio.h>
26#include <ctype.h>
27
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028
Kenny Root9a2d83e2009-12-04 09:38:48 -080029namespace android {
30
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080031static SharedBuffer* gEmptyStringBuf = NULL;
32static char16_t* gEmptyString = NULL;
33
34static inline char16_t* getEmptyString()
35{
36 gEmptyStringBuf->acquire();
37 return gEmptyString;
38}
39
40void initialize_string16()
41{
42 SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t));
43 char16_t* str = (char16_t*)buf->data();
44 *str = 0;
45 gEmptyStringBuf = buf;
46 gEmptyString = str;
47}
48
49void terminate_string16()
50{
51 SharedBuffer::bufferFromData(gEmptyString)->release();
52 gEmptyStringBuf = NULL;
53 gEmptyString = NULL;
54}
55
56// ---------------------------------------------------------------------------
57
Kenny Rootba0165b2010-11-09 14:37:23 -080058static char16_t* allocFromUTF8(const char* u8str, size_t u8len)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080059{
Kenny Rootba0165b2010-11-09 14:37:23 -080060 if (u8len == 0) return getEmptyString();
61
62 const uint8_t* u8cur = (const uint8_t*) u8str;
63
64 const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len);
65 if (u16len < 0) {
66 return getEmptyString();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080067 }
Kenny Rootba0165b2010-11-09 14:37:23 -080068
Kenny Rootba0165b2010-11-09 14:37:23 -080069 SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)*(u16len+1));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080070 if (buf) {
Kenny Rootba0165b2010-11-09 14:37:23 -080071 u8cur = (const uint8_t*) u8str;
72 char16_t* u16str = (char16_t*)buf->data();
73
74 utf8_to_utf16(u8cur, u8len, u16str);
Kenny Root9a2d83e2009-12-04 09:38:48 -080075
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080076 //printf("Created UTF-16 string from UTF-8 \"%s\":", in);
77 //printHexData(1, str, buf->size(), 16, 1);
78 //printf("\n");
79
Kenny Rootba0165b2010-11-09 14:37:23 -080080 return u16str;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080081 }
Kenny Rootba0165b2010-11-09 14:37:23 -080082
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080083 return getEmptyString();
84}
85
Steven Morelandd0648d82018-03-01 11:03:04 -080086static char16_t* allocFromUTF16(const char16_t* u16str, size_t u16len) {
87 if (u16len >= SIZE_MAX / sizeof(char16_t)) {
88 android_errorWriteLog(0x534e4554, "73826242");
89 abort();
90 }
91
92 SharedBuffer* buf = SharedBuffer::alloc((u16len + 1) * sizeof(char16_t));
93 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
94 if (buf) {
95 char16_t* str = (char16_t*)buf->data();
96 memcpy(str, u16str, u16len * sizeof(char16_t));
97 str[u16len] = 0;
98 return str;
99 }
100 return getEmptyString();
101}
102
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800103// ---------------------------------------------------------------------------
104
105String16::String16()
106 : mString(getEmptyString())
107{
108}
109
Mathias Agopian4485d0d2013-05-08 16:04:13 -0700110String16::String16(StaticLinkage)
111 : mString(0)
112{
113 // this constructor is used when we can't rely on the static-initializers
114 // having run. In this case we always allocate an empty string. It's less
115 // efficient than using getEmptyString(), but we assume it's uncommon.
116
117 char16_t* data = static_cast<char16_t*>(
118 SharedBuffer::alloc(sizeof(char16_t))->data());
119 data[0] = 0;
120 mString = data;
121}
122
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800123String16::String16(const String16& o)
124 : mString(o.mString)
125{
126 SharedBuffer::bufferFromData(mString)->acquire();
127}
128
129String16::String16(const String16& o, size_t len, size_t begin)
130 : mString(getEmptyString())
131{
132 setTo(o, len, begin);
133}
134
Steven Morelandd0648d82018-03-01 11:03:04 -0800135String16::String16(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o))) {}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800136
Steven Morelandd0648d82018-03-01 11:03:04 -0800137String16::String16(const char16_t* o, size_t len) : mString(allocFromUTF16(o, len)) {}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800138
139String16::String16(const String8& o)
140 : mString(allocFromUTF8(o.string(), o.size()))
141{
142}
143
144String16::String16(const char* o)
145 : mString(allocFromUTF8(o, strlen(o)))
146{
147}
148
149String16::String16(const char* o, size_t len)
150 : mString(allocFromUTF8(o, len))
151{
152}
153
154String16::~String16()
155{
156 SharedBuffer::bufferFromData(mString)->release();
157}
158
159void String16::setTo(const String16& other)
160{
161 SharedBuffer::bufferFromData(other.mString)->acquire();
162 SharedBuffer::bufferFromData(mString)->release();
163 mString = other.mString;
164}
165
166status_t String16::setTo(const String16& other, size_t len, size_t begin)
167{
168 const size_t N = other.size();
169 if (begin >= N) {
170 SharedBuffer::bufferFromData(mString)->release();
171 mString = getEmptyString();
172 return NO_ERROR;
173 }
174 if ((begin+len) > N) len = N-begin;
175 if (begin == 0 && len == N) {
176 setTo(other);
177 return NO_ERROR;
178 }
179
180 if (&other == this) {
181 LOG_ALWAYS_FATAL("Not implemented");
182 }
183
184 return setTo(other.string()+begin, len);
185}
186
187status_t String16::setTo(const char16_t* other)
188{
189 return setTo(other, strlen16(other));
190}
191
192status_t String16::setTo(const char16_t* other, size_t len)
193{
Steven Morelandd0648d82018-03-01 11:03:04 -0800194 if (len >= SIZE_MAX / sizeof(char16_t)) {
195 android_errorWriteLog(0x534e4554, "73826242");
196 abort();
197 }
198
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800199 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
200 ->editResize((len+1)*sizeof(char16_t));
201 if (buf) {
202 char16_t* str = (char16_t*)buf->data();
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800203 memmove(str, other, len*sizeof(char16_t));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800204 str[len] = 0;
205 mString = str;
206 return NO_ERROR;
207 }
208 return NO_MEMORY;
209}
210
211status_t String16::append(const String16& other)
212{
213 const size_t myLen = size();
214 const size_t otherLen = other.size();
215 if (myLen == 0) {
216 setTo(other);
217 return NO_ERROR;
218 } else if (otherLen == 0) {
219 return NO_ERROR;
220 }
Steven Morelandd0648d82018-03-01 11:03:04 -0800221
222 if (myLen >= SIZE_MAX / sizeof(char16_t) - otherLen) {
223 android_errorWriteLog(0x534e4554, "73826242");
224 abort();
225 }
226
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800227 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
228 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
229 if (buf) {
230 char16_t* str = (char16_t*)buf->data();
231 memcpy(str+myLen, other, (otherLen+1)*sizeof(char16_t));
232 mString = str;
233 return NO_ERROR;
234 }
235 return NO_MEMORY;
236}
237
238status_t String16::append(const char16_t* chrs, size_t otherLen)
239{
240 const size_t myLen = size();
241 if (myLen == 0) {
242 setTo(chrs, otherLen);
243 return NO_ERROR;
244 } else if (otherLen == 0) {
245 return NO_ERROR;
246 }
Steven Morelandd0648d82018-03-01 11:03:04 -0800247
248 if (myLen >= SIZE_MAX / sizeof(char16_t) - otherLen) {
249 android_errorWriteLog(0x534e4554, "73826242");
250 abort();
251 }
252
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800253 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
254 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
255 if (buf) {
256 char16_t* str = (char16_t*)buf->data();
257 memcpy(str+myLen, chrs, otherLen*sizeof(char16_t));
258 str[myLen+otherLen] = 0;
259 mString = str;
260 return NO_ERROR;
261 }
262 return NO_MEMORY;
263}
264
265status_t String16::insert(size_t pos, const char16_t* chrs)
266{
267 return insert(pos, chrs, strlen16(chrs));
268}
269
270status_t String16::insert(size_t pos, const char16_t* chrs, size_t len)
271{
272 const size_t myLen = size();
273 if (myLen == 0) {
274 return setTo(chrs, len);
275 return NO_ERROR;
276 } else if (len == 0) {
277 return NO_ERROR;
278 }
279
280 if (pos > myLen) pos = myLen;
281
282 #if 0
283 printf("Insert in to %s: pos=%d, len=%d, myLen=%d, chrs=%s\n",
284 String8(*this).string(), pos,
285 len, myLen, String8(chrs, len).string());
286 #endif
287
288 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
289 ->editResize((myLen+len+1)*sizeof(char16_t));
290 if (buf) {
291 char16_t* str = (char16_t*)buf->data();
292 if (pos < myLen) {
293 memmove(str+pos+len, str+pos, (myLen-pos)*sizeof(char16_t));
294 }
295 memcpy(str+pos, chrs, len*sizeof(char16_t));
296 str[myLen+len] = 0;
297 mString = str;
298 #if 0
299 printf("Result (%d chrs): %s\n", size(), String8(*this).string());
300 #endif
301 return NO_ERROR;
302 }
303 return NO_MEMORY;
304}
305
306ssize_t String16::findFirst(char16_t c) const
307{
308 const char16_t* str = string();
309 const char16_t* p = str;
310 const char16_t* e = p + size();
311 while (p < e) {
312 if (*p == c) {
313 return p-str;
314 }
315 p++;
316 }
317 return -1;
318}
319
320ssize_t String16::findLast(char16_t c) const
321{
322 const char16_t* str = string();
323 const char16_t* p = str;
324 const char16_t* e = p + size();
325 while (p < e) {
326 e--;
327 if (*e == c) {
328 return e-str;
329 }
330 }
331 return -1;
332}
333
334bool String16::startsWith(const String16& prefix) const
335{
336 const size_t ps = prefix.size();
337 if (ps > size()) return false;
338 return strzcmp16(mString, ps, prefix.string(), ps) == 0;
339}
340
341bool String16::startsWith(const char16_t* prefix) const
342{
343 const size_t ps = strlen16(prefix);
344 if (ps > size()) return false;
345 return strncmp16(mString, prefix, ps) == 0;
346}
347
348status_t String16::makeLower()
349{
350 const size_t N = size();
351 const char16_t* str = string();
352 char16_t* edit = NULL;
353 for (size_t i=0; i<N; i++) {
354 const char16_t v = str[i];
355 if (v >= 'A' && v <= 'Z') {
356 if (!edit) {
357 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
358 if (!buf) {
359 return NO_MEMORY;
360 }
361 edit = (char16_t*)buf->data();
362 mString = str = edit;
363 }
364 edit[i] = tolower((char)v);
365 }
366 }
367 return NO_ERROR;
368}
369
370status_t String16::replaceAll(char16_t replaceThis, char16_t withThis)
371{
372 const size_t N = size();
373 const char16_t* str = string();
374 char16_t* edit = NULL;
375 for (size_t i=0; i<N; i++) {
376 if (str[i] == replaceThis) {
377 if (!edit) {
378 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
379 if (!buf) {
380 return NO_MEMORY;
381 }
382 edit = (char16_t*)buf->data();
383 mString = str = edit;
384 }
385 edit[i] = withThis;
386 }
387 }
388 return NO_ERROR;
389}
390
391status_t String16::remove(size_t len, size_t begin)
392{
393 const size_t N = size();
394 if (begin >= N) {
395 SharedBuffer::bufferFromData(mString)->release();
396 mString = getEmptyString();
397 return NO_ERROR;
398 }
399 if ((begin+len) > N) len = N-begin;
400 if (begin == 0 && len == N) {
401 return NO_ERROR;
402 }
403
404 if (begin > 0) {
405 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
406 ->editResize((N+1)*sizeof(char16_t));
407 if (!buf) {
408 return NO_MEMORY;
409 }
410 char16_t* str = (char16_t*)buf->data();
411 memmove(str, str+begin, (N-begin+1)*sizeof(char16_t));
412 mString = str;
413 }
414 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
415 ->editResize((len+1)*sizeof(char16_t));
416 if (buf) {
417 char16_t* str = (char16_t*)buf->data();
418 str[len] = 0;
419 mString = str;
420 return NO_ERROR;
421 }
422 return NO_MEMORY;
423}
424
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800425}; // namespace android