blob: ac12d8aa72c2c6bdda0d8fc546c80a9d7b34451f [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/threads.h>
22
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023#include <memory.h>
24#include <stdio.h>
25#include <ctype.h>
26
Sergio Girod2529f22015-09-23 16:22:59 +010027#include "SharedBuffer.h"
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");
Samuel Tanf9d16ef2016-02-16 15:17:10 -080079
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
86// ---------------------------------------------------------------------------
87
88String16::String16()
89 : mString(getEmptyString())
90{
91}
92
Mathias Agopian4485d0d2013-05-08 16:04:13 -070093String16::String16(StaticLinkage)
94 : mString(0)
95{
96 // this constructor is used when we can't rely on the static-initializers
97 // having run. In this case we always allocate an empty string. It's less
98 // efficient than using getEmptyString(), but we assume it's uncommon.
99
100 char16_t* data = static_cast<char16_t*>(
101 SharedBuffer::alloc(sizeof(char16_t))->data());
102 data[0] = 0;
103 mString = data;
104}
105
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800106String16::String16(const String16& o)
107 : mString(o.mString)
108{
109 SharedBuffer::bufferFromData(mString)->acquire();
110}
111
112String16::String16(const String16& o, size_t len, size_t begin)
113 : mString(getEmptyString())
114{
115 setTo(o, len, begin);
116}
117
118String16::String16(const char16_t* o)
119{
120 size_t len = strlen16(o);
121 SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t));
Steve Blockae074452012-01-09 18:35:44 +0000122 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800123 if (buf) {
124 char16_t* str = (char16_t*)buf->data();
125 strcpy16(str, o);
126 mString = str;
127 return;
128 }
Samuel Tanf9d16ef2016-02-16 15:17:10 -0800129
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800130 mString = getEmptyString();
131}
132
133String16::String16(const char16_t* o, size_t len)
134{
135 SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t));
Steve Blockae074452012-01-09 18:35:44 +0000136 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800137 if (buf) {
138 char16_t* str = (char16_t*)buf->data();
139 memcpy(str, o, len*sizeof(char16_t));
140 str[len] = 0;
141 mString = str;
142 return;
143 }
Samuel Tanf9d16ef2016-02-16 15:17:10 -0800144
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800145 mString = getEmptyString();
146}
147
148String16::String16(const String8& o)
149 : mString(allocFromUTF8(o.string(), o.size()))
150{
151}
152
153String16::String16(const char* o)
154 : mString(allocFromUTF8(o, strlen(o)))
155{
156}
157
158String16::String16(const char* o, size_t len)
159 : mString(allocFromUTF8(o, len))
160{
161}
162
163String16::~String16()
164{
165 SharedBuffer::bufferFromData(mString)->release();
166}
167
Sergio Girod2529f22015-09-23 16:22:59 +0100168size_t String16::size() const
169{
170 return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1;
171}
172
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800173void String16::setTo(const String16& other)
174{
175 SharedBuffer::bufferFromData(other.mString)->acquire();
176 SharedBuffer::bufferFromData(mString)->release();
177 mString = other.mString;
178}
179
180status_t String16::setTo(const String16& other, size_t len, size_t begin)
181{
182 const size_t N = other.size();
183 if (begin >= N) {
184 SharedBuffer::bufferFromData(mString)->release();
185 mString = getEmptyString();
186 return NO_ERROR;
187 }
188 if ((begin+len) > N) len = N-begin;
189 if (begin == 0 && len == N) {
190 setTo(other);
191 return NO_ERROR;
192 }
193
194 if (&other == this) {
195 LOG_ALWAYS_FATAL("Not implemented");
196 }
197
198 return setTo(other.string()+begin, len);
199}
200
201status_t String16::setTo(const char16_t* other)
202{
203 return setTo(other, strlen16(other));
204}
205
206status_t String16::setTo(const char16_t* other, size_t len)
207{
208 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
209 ->editResize((len+1)*sizeof(char16_t));
210 if (buf) {
211 char16_t* str = (char16_t*)buf->data();
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800212 memmove(str, other, len*sizeof(char16_t));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800213 str[len] = 0;
214 mString = str;
215 return NO_ERROR;
216 }
217 return NO_MEMORY;
218}
219
220status_t String16::append(const String16& other)
221{
222 const size_t myLen = size();
223 const size_t otherLen = other.size();
224 if (myLen == 0) {
225 setTo(other);
226 return NO_ERROR;
227 } else if (otherLen == 0) {
228 return NO_ERROR;
229 }
Samuel Tanf9d16ef2016-02-16 15:17:10 -0800230
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800231 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
232 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
233 if (buf) {
234 char16_t* str = (char16_t*)buf->data();
235 memcpy(str+myLen, other, (otherLen+1)*sizeof(char16_t));
236 mString = str;
237 return NO_ERROR;
238 }
239 return NO_MEMORY;
240}
241
242status_t String16::append(const char16_t* chrs, size_t otherLen)
243{
244 const size_t myLen = size();
245 if (myLen == 0) {
246 setTo(chrs, otherLen);
247 return NO_ERROR;
248 } else if (otherLen == 0) {
249 return NO_ERROR;
250 }
Samuel Tanf9d16ef2016-02-16 15:17:10 -0800251
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800252 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
253 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
254 if (buf) {
255 char16_t* str = (char16_t*)buf->data();
256 memcpy(str+myLen, chrs, otherLen*sizeof(char16_t));
257 str[myLen+otherLen] = 0;
258 mString = str;
259 return NO_ERROR;
260 }
261 return NO_MEMORY;
262}
263
264status_t String16::insert(size_t pos, const char16_t* chrs)
265{
266 return insert(pos, chrs, strlen16(chrs));
267}
268
269status_t String16::insert(size_t pos, const char16_t* chrs, size_t len)
270{
271 const size_t myLen = size();
272 if (myLen == 0) {
273 return setTo(chrs, len);
274 return NO_ERROR;
275 } else if (len == 0) {
276 return NO_ERROR;
277 }
278
279 if (pos > myLen) pos = myLen;
280
281 #if 0
282 printf("Insert in to %s: pos=%d, len=%d, myLen=%d, chrs=%s\n",
283 String8(*this).string(), pos,
284 len, myLen, String8(chrs, len).string());
285 #endif
286
287 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
288 ->editResize((myLen+len+1)*sizeof(char16_t));
289 if (buf) {
290 char16_t* str = (char16_t*)buf->data();
291 if (pos < myLen) {
292 memmove(str+pos+len, str+pos, (myLen-pos)*sizeof(char16_t));
293 }
294 memcpy(str+pos, chrs, len*sizeof(char16_t));
295 str[myLen+len] = 0;
296 mString = str;
297 #if 0
298 printf("Result (%d chrs): %s\n", size(), String8(*this).string());
299 #endif
300 return NO_ERROR;
301 }
302 return NO_MEMORY;
303}
304
305ssize_t String16::findFirst(char16_t c) const
306{
307 const char16_t* str = string();
308 const char16_t* p = str;
309 const char16_t* e = p + size();
310 while (p < e) {
311 if (*p == c) {
312 return p-str;
313 }
314 p++;
315 }
316 return -1;
317}
318
319ssize_t String16::findLast(char16_t c) const
320{
321 const char16_t* str = string();
322 const char16_t* p = str;
323 const char16_t* e = p + size();
324 while (p < e) {
325 e--;
326 if (*e == c) {
327 return e-str;
328 }
329 }
330 return -1;
331}
332
333bool String16::startsWith(const String16& prefix) const
334{
335 const size_t ps = prefix.size();
336 if (ps > size()) return false;
337 return strzcmp16(mString, ps, prefix.string(), ps) == 0;
338}
339
340bool String16::startsWith(const char16_t* prefix) const
341{
342 const size_t ps = strlen16(prefix);
343 if (ps > size()) return false;
344 return strncmp16(mString, prefix, ps) == 0;
345}
346
Michael Wright5bacef32016-05-09 14:43:31 +0100347bool String16::contains(const char16_t* chrs) const
348{
349 return strstr16(mString, chrs) != nullptr;
350}
351
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800352status_t String16::makeLower()
353{
354 const size_t N = size();
355 const char16_t* str = string();
356 char16_t* edit = NULL;
357 for (size_t i=0; i<N; i++) {
358 const char16_t v = str[i];
359 if (v >= 'A' && v <= 'Z') {
360 if (!edit) {
361 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
362 if (!buf) {
363 return NO_MEMORY;
364 }
365 edit = (char16_t*)buf->data();
366 mString = str = edit;
367 }
368 edit[i] = tolower((char)v);
369 }
370 }
371 return NO_ERROR;
372}
373
374status_t String16::replaceAll(char16_t replaceThis, char16_t withThis)
375{
376 const size_t N = size();
377 const char16_t* str = string();
378 char16_t* edit = NULL;
379 for (size_t i=0; i<N; i++) {
380 if (str[i] == replaceThis) {
381 if (!edit) {
382 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
383 if (!buf) {
384 return NO_MEMORY;
385 }
386 edit = (char16_t*)buf->data();
387 mString = str = edit;
388 }
389 edit[i] = withThis;
390 }
391 }
392 return NO_ERROR;
393}
394
395status_t String16::remove(size_t len, size_t begin)
396{
397 const size_t N = size();
398 if (begin >= N) {
399 SharedBuffer::bufferFromData(mString)->release();
400 mString = getEmptyString();
401 return NO_ERROR;
402 }
403 if ((begin+len) > N) len = N-begin;
404 if (begin == 0 && len == N) {
405 return NO_ERROR;
406 }
407
408 if (begin > 0) {
409 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
410 ->editResize((N+1)*sizeof(char16_t));
411 if (!buf) {
412 return NO_MEMORY;
413 }
414 char16_t* str = (char16_t*)buf->data();
415 memmove(str, str+begin, (N-begin+1)*sizeof(char16_t));
416 mString = str;
417 }
418 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
419 ->editResize((len+1)*sizeof(char16_t));
420 if (buf) {
421 char16_t* str = (char16_t*)buf->data();
422 str[len] = 0;
423 mString = str;
424 return NO_ERROR;
425 }
426 return NO_MEMORY;
427}
428
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800429}; // namespace android