blob: 67be9d8fafb754bc6285ceae4d9f409fa5f34566 [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
174const SharedBuffer* String16::sharedBuffer() const
175{
176 return SharedBuffer::bufferFromData(mString);
177}
178
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800179void String16::setTo(const String16& other)
180{
181 SharedBuffer::bufferFromData(other.mString)->acquire();
182 SharedBuffer::bufferFromData(mString)->release();
183 mString = other.mString;
184}
185
186status_t String16::setTo(const String16& other, size_t len, size_t begin)
187{
188 const size_t N = other.size();
189 if (begin >= N) {
190 SharedBuffer::bufferFromData(mString)->release();
191 mString = getEmptyString();
192 return NO_ERROR;
193 }
194 if ((begin+len) > N) len = N-begin;
195 if (begin == 0 && len == N) {
196 setTo(other);
197 return NO_ERROR;
198 }
199
200 if (&other == this) {
201 LOG_ALWAYS_FATAL("Not implemented");
202 }
203
204 return setTo(other.string()+begin, len);
205}
206
207status_t String16::setTo(const char16_t* other)
208{
209 return setTo(other, strlen16(other));
210}
211
212status_t String16::setTo(const char16_t* other, size_t len)
213{
214 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
215 ->editResize((len+1)*sizeof(char16_t));
216 if (buf) {
217 char16_t* str = (char16_t*)buf->data();
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800218 memmove(str, other, len*sizeof(char16_t));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800219 str[len] = 0;
220 mString = str;
221 return NO_ERROR;
222 }
223 return NO_MEMORY;
224}
225
226status_t String16::append(const String16& other)
227{
228 const size_t myLen = size();
229 const size_t otherLen = other.size();
230 if (myLen == 0) {
231 setTo(other);
232 return NO_ERROR;
233 } else if (otherLen == 0) {
234 return NO_ERROR;
235 }
236
237 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
238 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
239 if (buf) {
240 char16_t* str = (char16_t*)buf->data();
241 memcpy(str+myLen, other, (otherLen+1)*sizeof(char16_t));
242 mString = str;
243 return NO_ERROR;
244 }
245 return NO_MEMORY;
246}
247
248status_t String16::append(const char16_t* chrs, size_t otherLen)
249{
250 const size_t myLen = size();
251 if (myLen == 0) {
252 setTo(chrs, otherLen);
253 return NO_ERROR;
254 } else if (otherLen == 0) {
255 return NO_ERROR;
256 }
257
258 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
259 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
260 if (buf) {
261 char16_t* str = (char16_t*)buf->data();
262 memcpy(str+myLen, chrs, otherLen*sizeof(char16_t));
263 str[myLen+otherLen] = 0;
264 mString = str;
265 return NO_ERROR;
266 }
267 return NO_MEMORY;
268}
269
270status_t String16::insert(size_t pos, const char16_t* chrs)
271{
272 return insert(pos, chrs, strlen16(chrs));
273}
274
275status_t String16::insert(size_t pos, const char16_t* chrs, size_t len)
276{
277 const size_t myLen = size();
278 if (myLen == 0) {
279 return setTo(chrs, len);
280 return NO_ERROR;
281 } else if (len == 0) {
282 return NO_ERROR;
283 }
284
285 if (pos > myLen) pos = myLen;
286
287 #if 0
288 printf("Insert in to %s: pos=%d, len=%d, myLen=%d, chrs=%s\n",
289 String8(*this).string(), pos,
290 len, myLen, String8(chrs, len).string());
291 #endif
292
293 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
294 ->editResize((myLen+len+1)*sizeof(char16_t));
295 if (buf) {
296 char16_t* str = (char16_t*)buf->data();
297 if (pos < myLen) {
298 memmove(str+pos+len, str+pos, (myLen-pos)*sizeof(char16_t));
299 }
300 memcpy(str+pos, chrs, len*sizeof(char16_t));
301 str[myLen+len] = 0;
302 mString = str;
303 #if 0
304 printf("Result (%d chrs): %s\n", size(), String8(*this).string());
305 #endif
306 return NO_ERROR;
307 }
308 return NO_MEMORY;
309}
310
311ssize_t String16::findFirst(char16_t c) const
312{
313 const char16_t* str = string();
314 const char16_t* p = str;
315 const char16_t* e = p + size();
316 while (p < e) {
317 if (*p == c) {
318 return p-str;
319 }
320 p++;
321 }
322 return -1;
323}
324
325ssize_t String16::findLast(char16_t c) const
326{
327 const char16_t* str = string();
328 const char16_t* p = str;
329 const char16_t* e = p + size();
330 while (p < e) {
331 e--;
332 if (*e == c) {
333 return e-str;
334 }
335 }
336 return -1;
337}
338
339bool String16::startsWith(const String16& prefix) const
340{
341 const size_t ps = prefix.size();
342 if (ps > size()) return false;
343 return strzcmp16(mString, ps, prefix.string(), ps) == 0;
344}
345
346bool String16::startsWith(const char16_t* prefix) const
347{
348 const size_t ps = strlen16(prefix);
349 if (ps > size()) return false;
350 return strncmp16(mString, prefix, ps) == 0;
351}
352
353status_t String16::makeLower()
354{
355 const size_t N = size();
356 const char16_t* str = string();
357 char16_t* edit = NULL;
358 for (size_t i=0; i<N; i++) {
359 const char16_t v = str[i];
360 if (v >= 'A' && v <= 'Z') {
361 if (!edit) {
362 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
363 if (!buf) {
364 return NO_MEMORY;
365 }
366 edit = (char16_t*)buf->data();
367 mString = str = edit;
368 }
369 edit[i] = tolower((char)v);
370 }
371 }
372 return NO_ERROR;
373}
374
375status_t String16::replaceAll(char16_t replaceThis, char16_t withThis)
376{
377 const size_t N = size();
378 const char16_t* str = string();
379 char16_t* edit = NULL;
380 for (size_t i=0; i<N; i++) {
381 if (str[i] == replaceThis) {
382 if (!edit) {
383 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
384 if (!buf) {
385 return NO_MEMORY;
386 }
387 edit = (char16_t*)buf->data();
388 mString = str = edit;
389 }
390 edit[i] = withThis;
391 }
392 }
393 return NO_ERROR;
394}
395
396status_t String16::remove(size_t len, size_t begin)
397{
398 const size_t N = size();
399 if (begin >= N) {
400 SharedBuffer::bufferFromData(mString)->release();
401 mString = getEmptyString();
402 return NO_ERROR;
403 }
404 if ((begin+len) > N) len = N-begin;
405 if (begin == 0 && len == N) {
406 return NO_ERROR;
407 }
408
409 if (begin > 0) {
410 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
411 ->editResize((N+1)*sizeof(char16_t));
412 if (!buf) {
413 return NO_MEMORY;
414 }
415 char16_t* str = (char16_t*)buf->data();
416 memmove(str, str+begin, (N-begin+1)*sizeof(char16_t));
417 mString = str;
418 }
419 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
420 ->editResize((len+1)*sizeof(char16_t));
421 if (buf) {
422 char16_t* str = (char16_t*)buf->data();
423 str[len] = 0;
424 mString = str;
425 return NO_ERROR;
426 }
427 return NO_MEMORY;
428}
429
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800430}; // namespace android