blob: 6a5273ff22ceb1860e006eb3576f7966c45eda3c [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
Sergio Girod2529f22015-09-23 16:22:59 +010028#include "SharedBuffer.h"
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080029
Kenny Root9a2d83e2009-12-04 09:38:48 -080030namespace android {
31
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080032static SharedBuffer* gEmptyStringBuf = NULL;
33static char16_t* gEmptyString = NULL;
34
35static inline char16_t* getEmptyString()
36{
37 gEmptyStringBuf->acquire();
38 return gEmptyString;
39}
40
41void initialize_string16()
42{
43 SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t));
44 char16_t* str = (char16_t*)buf->data();
45 *str = 0;
46 gEmptyStringBuf = buf;
47 gEmptyString = str;
48}
49
50void terminate_string16()
51{
52 SharedBuffer::bufferFromData(gEmptyString)->release();
53 gEmptyStringBuf = NULL;
54 gEmptyString = NULL;
55}
56
57// ---------------------------------------------------------------------------
58
Kenny Rootba0165b2010-11-09 14:37:23 -080059static char16_t* allocFromUTF8(const char* u8str, size_t u8len)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080060{
Kenny Rootba0165b2010-11-09 14:37:23 -080061 if (u8len == 0) return getEmptyString();
62
63 const uint8_t* u8cur = (const uint8_t*) u8str;
64
65 const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len);
66 if (u16len < 0) {
67 return getEmptyString();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080068 }
Kenny Rootba0165b2010-11-09 14:37:23 -080069
Kenny Rootba0165b2010-11-09 14:37:23 -080070 SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)*(u16len+1));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080071 if (buf) {
Kenny Rootba0165b2010-11-09 14:37:23 -080072 u8cur = (const uint8_t*) u8str;
73 char16_t* u16str = (char16_t*)buf->data();
74
75 utf8_to_utf16(u8cur, u8len, u16str);
Kenny Root9a2d83e2009-12-04 09:38:48 -080076
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080077 //printf("Created UTF-16 string from UTF-8 \"%s\":", in);
78 //printHexData(1, str, buf->size(), 16, 1);
79 //printf("\n");
80
Kenny Rootba0165b2010-11-09 14:37:23 -080081 return u16str;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080082 }
Kenny Rootba0165b2010-11-09 14:37:23 -080083
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080084 return getEmptyString();
85}
86
87// ---------------------------------------------------------------------------
88
89String16::String16()
90 : mString(getEmptyString())
91{
92}
93
Mathias Agopian4485d0d2013-05-08 16:04:13 -070094String16::String16(StaticLinkage)
95 : mString(0)
96{
97 // this constructor is used when we can't rely on the static-initializers
98 // having run. In this case we always allocate an empty string. It's less
99 // efficient than using getEmptyString(), but we assume it's uncommon.
100
101 char16_t* data = static_cast<char16_t*>(
102 SharedBuffer::alloc(sizeof(char16_t))->data());
103 data[0] = 0;
104 mString = data;
105}
106
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800107String16::String16(const String16& o)
108 : mString(o.mString)
109{
110 SharedBuffer::bufferFromData(mString)->acquire();
111}
112
113String16::String16(const String16& o, size_t len, size_t begin)
114 : mString(getEmptyString())
115{
116 setTo(o, len, begin);
117}
118
119String16::String16(const char16_t* o)
120{
121 size_t len = strlen16(o);
122 SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t));
Steve Blockae074452012-01-09 18:35:44 +0000123 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800124 if (buf) {
125 char16_t* str = (char16_t*)buf->data();
126 strcpy16(str, o);
127 mString = str;
128 return;
129 }
130
131 mString = getEmptyString();
132}
133
134String16::String16(const char16_t* o, size_t len)
135{
136 SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t));
Steve Blockae074452012-01-09 18:35:44 +0000137 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800138 if (buf) {
139 char16_t* str = (char16_t*)buf->data();
140 memcpy(str, o, len*sizeof(char16_t));
141 str[len] = 0;
142 mString = str;
143 return;
144 }
145
146 mString = getEmptyString();
147}
148
149String16::String16(const String8& o)
150 : mString(allocFromUTF8(o.string(), o.size()))
151{
152}
153
154String16::String16(const char* o)
155 : mString(allocFromUTF8(o, strlen(o)))
156{
157}
158
159String16::String16(const char* o, size_t len)
160 : mString(allocFromUTF8(o, len))
161{
162}
163
164String16::~String16()
165{
166 SharedBuffer::bufferFromData(mString)->release();
167}
168
Sergio Girod2529f22015-09-23 16:22:59 +0100169size_t String16::size() const
170{
171 return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1;
172}
173
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800174void String16::setTo(const String16& other)
175{
176 SharedBuffer::bufferFromData(other.mString)->acquire();
177 SharedBuffer::bufferFromData(mString)->release();
178 mString = other.mString;
179}
180
181status_t String16::setTo(const String16& other, size_t len, size_t begin)
182{
183 const size_t N = other.size();
184 if (begin >= N) {
185 SharedBuffer::bufferFromData(mString)->release();
186 mString = getEmptyString();
187 return NO_ERROR;
188 }
189 if ((begin+len) > N) len = N-begin;
190 if (begin == 0 && len == N) {
191 setTo(other);
192 return NO_ERROR;
193 }
194
195 if (&other == this) {
196 LOG_ALWAYS_FATAL("Not implemented");
197 }
198
199 return setTo(other.string()+begin, len);
200}
201
202status_t String16::setTo(const char16_t* other)
203{
204 return setTo(other, strlen16(other));
205}
206
207status_t String16::setTo(const char16_t* other, size_t len)
208{
209 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
210 ->editResize((len+1)*sizeof(char16_t));
211 if (buf) {
212 char16_t* str = (char16_t*)buf->data();
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800213 memmove(str, other, len*sizeof(char16_t));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800214 str[len] = 0;
215 mString = str;
216 return NO_ERROR;
217 }
218 return NO_MEMORY;
219}
220
221status_t String16::append(const String16& other)
222{
223 const size_t myLen = size();
224 const size_t otherLen = other.size();
225 if (myLen == 0) {
226 setTo(other);
227 return NO_ERROR;
228 } else if (otherLen == 0) {
229 return NO_ERROR;
230 }
231
232 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
233 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
234 if (buf) {
235 char16_t* str = (char16_t*)buf->data();
236 memcpy(str+myLen, other, (otherLen+1)*sizeof(char16_t));
237 mString = str;
238 return NO_ERROR;
239 }
240 return NO_MEMORY;
241}
242
243status_t String16::append(const char16_t* chrs, size_t otherLen)
244{
245 const size_t myLen = size();
246 if (myLen == 0) {
247 setTo(chrs, otherLen);
248 return NO_ERROR;
249 } else if (otherLen == 0) {
250 return NO_ERROR;
251 }
252
253 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