blob: 3bdc349abfc7f6a25ad88238ceea6d717f1d6ca3 [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
69 const uint8_t* const u8end = u8cur + u8len;
70
71 SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)*(u16len+1));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080072 if (buf) {
Kenny Rootba0165b2010-11-09 14:37:23 -080073 u8cur = (const uint8_t*) u8str;
74 char16_t* u16str = (char16_t*)buf->data();
75
76 utf8_to_utf16(u8cur, u8len, u16str);
Kenny Root9a2d83e2009-12-04 09:38:48 -080077
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080078 //printf("Created UTF-16 string from UTF-8 \"%s\":", in);
79 //printHexData(1, str, buf->size(), 16, 1);
80 //printf("\n");
81
Kenny Rootba0165b2010-11-09 14:37:23 -080082 return u16str;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080083 }
Kenny Rootba0165b2010-11-09 14:37:23 -080084
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080085 return getEmptyString();
86}
87
88// ---------------------------------------------------------------------------
89
90String16::String16()
91 : mString(getEmptyString())
92{
93}
94
Mathias Agopian4485d0d2013-05-08 16:04:13 -070095String16::String16(StaticLinkage)
96 : mString(0)
97{
98 // this constructor is used when we can't rely on the static-initializers
99 // having run. In this case we always allocate an empty string. It's less
100 // efficient than using getEmptyString(), but we assume it's uncommon.
101
102 char16_t* data = static_cast<char16_t*>(
103 SharedBuffer::alloc(sizeof(char16_t))->data());
104 data[0] = 0;
105 mString = data;
106}
107
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800108String16::String16(const String16& o)
109 : mString(o.mString)
110{
111 SharedBuffer::bufferFromData(mString)->acquire();
112}
113
114String16::String16(const String16& o, size_t len, size_t begin)
115 : mString(getEmptyString())
116{
117 setTo(o, len, begin);
118}
119
120String16::String16(const char16_t* o)
121{
122 size_t len = strlen16(o);
123 SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t));
Steve Blockae074452012-01-09 18:35:44 +0000124 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800125 if (buf) {
126 char16_t* str = (char16_t*)buf->data();
127 strcpy16(str, o);
128 mString = str;
129 return;
130 }
131
132 mString = getEmptyString();
133}
134
135String16::String16(const char16_t* o, size_t len)
136{
137 SharedBuffer* buf = SharedBuffer::alloc((len+1)*sizeof(char16_t));
Steve Blockae074452012-01-09 18:35:44 +0000138 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800139 if (buf) {
140 char16_t* str = (char16_t*)buf->data();
141 memcpy(str, o, len*sizeof(char16_t));
142 str[len] = 0;
143 mString = str;
144 return;
145 }
146
147 mString = getEmptyString();
148}
149
150String16::String16(const String8& o)
151 : mString(allocFromUTF8(o.string(), o.size()))
152{
153}
154
155String16::String16(const char* o)
156 : mString(allocFromUTF8(o, strlen(o)))
157{
158}
159
160String16::String16(const char* o, size_t len)
161 : mString(allocFromUTF8(o, len))
162{
163}
164
165String16::~String16()
166{
167 SharedBuffer::bufferFromData(mString)->release();
168}
169
170void String16::setTo(const String16& other)
171{
172 SharedBuffer::bufferFromData(other.mString)->acquire();
173 SharedBuffer::bufferFromData(mString)->release();
174 mString = other.mString;
175}
176
177status_t String16::setTo(const String16& other, size_t len, size_t begin)
178{
179 const size_t N = other.size();
180 if (begin >= N) {
181 SharedBuffer::bufferFromData(mString)->release();
182 mString = getEmptyString();
183 return NO_ERROR;
184 }
185 if ((begin+len) > N) len = N-begin;
186 if (begin == 0 && len == N) {
187 setTo(other);
188 return NO_ERROR;
189 }
190
191 if (&other == this) {
192 LOG_ALWAYS_FATAL("Not implemented");
193 }
194
195 return setTo(other.string()+begin, len);
196}
197
198status_t String16::setTo(const char16_t* other)
199{
200 return setTo(other, strlen16(other));
201}
202
203status_t String16::setTo(const char16_t* other, size_t len)
204{
205 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
206 ->editResize((len+1)*sizeof(char16_t));
207 if (buf) {
208 char16_t* str = (char16_t*)buf->data();
The Android Open Source Project7a4c8392009-03-05 14:34:35 -0800209 memmove(str, other, len*sizeof(char16_t));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800210 str[len] = 0;
211 mString = str;
212 return NO_ERROR;
213 }
214 return NO_MEMORY;
215}
216
217status_t String16::append(const String16& other)
218{
219 const size_t myLen = size();
220 const size_t otherLen = other.size();
221 if (myLen == 0) {
222 setTo(other);
223 return NO_ERROR;
224 } else if (otherLen == 0) {
225 return NO_ERROR;
226 }
227
228 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
229 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
230 if (buf) {
231 char16_t* str = (char16_t*)buf->data();
232 memcpy(str+myLen, other, (otherLen+1)*sizeof(char16_t));
233 mString = str;
234 return NO_ERROR;
235 }
236 return NO_MEMORY;
237}
238
239status_t String16::append(const char16_t* chrs, size_t otherLen)
240{
241 const size_t myLen = size();
242 if (myLen == 0) {
243 setTo(chrs, otherLen);
244 return NO_ERROR;
245 } else if (otherLen == 0) {
246 return NO_ERROR;
247 }
248
249 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
250 ->editResize((myLen+otherLen+1)*sizeof(char16_t));
251 if (buf) {
252 char16_t* str = (char16_t*)buf->data();
253 memcpy(str+myLen, chrs, otherLen*sizeof(char16_t));
254 str[myLen+otherLen] = 0;
255 mString = str;
256 return NO_ERROR;
257 }
258 return NO_MEMORY;
259}
260
261status_t String16::insert(size_t pos, const char16_t* chrs)
262{
263 return insert(pos, chrs, strlen16(chrs));
264}
265
266status_t String16::insert(size_t pos, const char16_t* chrs, size_t len)
267{
268 const size_t myLen = size();
269 if (myLen == 0) {
270 return setTo(chrs, len);
271 return NO_ERROR;
272 } else if (len == 0) {
273 return NO_ERROR;
274 }
275
276 if (pos > myLen) pos = myLen;
277
278 #if 0
279 printf("Insert in to %s: pos=%d, len=%d, myLen=%d, chrs=%s\n",
280 String8(*this).string(), pos,
281 len, myLen, String8(chrs, len).string());
282 #endif
283
284 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
285 ->editResize((myLen+len+1)*sizeof(char16_t));
286 if (buf) {
287 char16_t* str = (char16_t*)buf->data();
288 if (pos < myLen) {
289 memmove(str+pos+len, str+pos, (myLen-pos)*sizeof(char16_t));
290 }
291 memcpy(str+pos, chrs, len*sizeof(char16_t));
292 str[myLen+len] = 0;
293 mString = str;
294 #if 0
295 printf("Result (%d chrs): %s\n", size(), String8(*this).string());
296 #endif
297 return NO_ERROR;
298 }
299 return NO_MEMORY;
300}
301
302ssize_t String16::findFirst(char16_t c) const
303{
304 const char16_t* str = string();
305 const char16_t* p = str;
306 const char16_t* e = p + size();
307 while (p < e) {
308 if (*p == c) {
309 return p-str;
310 }
311 p++;
312 }
313 return -1;
314}
315
316ssize_t String16::findLast(char16_t c) const
317{
318 const char16_t* str = string();
319 const char16_t* p = str;
320 const char16_t* e = p + size();
321 while (p < e) {
322 e--;
323 if (*e == c) {
324 return e-str;
325 }
326 }
327 return -1;
328}
329
330bool String16::startsWith(const String16& prefix) const
331{
332 const size_t ps = prefix.size();
333 if (ps > size()) return false;
334 return strzcmp16(mString, ps, prefix.string(), ps) == 0;
335}
336
337bool String16::startsWith(const char16_t* prefix) const
338{
339 const size_t ps = strlen16(prefix);
340 if (ps > size()) return false;
341 return strncmp16(mString, prefix, ps) == 0;
342}
343
344status_t String16::makeLower()
345{
346 const size_t N = size();
347 const char16_t* str = string();
348 char16_t* edit = NULL;
349 for (size_t i=0; i<N; i++) {
350 const char16_t v = str[i];
351 if (v >= 'A' && v <= 'Z') {
352 if (!edit) {
353 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
354 if (!buf) {
355 return NO_MEMORY;
356 }
357 edit = (char16_t*)buf->data();
358 mString = str = edit;
359 }
360 edit[i] = tolower((char)v);
361 }
362 }
363 return NO_ERROR;
364}
365
366status_t String16::replaceAll(char16_t replaceThis, char16_t withThis)
367{
368 const size_t N = size();
369 const char16_t* str = string();
370 char16_t* edit = NULL;
371 for (size_t i=0; i<N; i++) {
372 if (str[i] == replaceThis) {
373 if (!edit) {
374 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
375 if (!buf) {
376 return NO_MEMORY;
377 }
378 edit = (char16_t*)buf->data();
379 mString = str = edit;
380 }
381 edit[i] = withThis;
382 }
383 }
384 return NO_ERROR;
385}
386
387status_t String16::remove(size_t len, size_t begin)
388{
389 const size_t N = size();
390 if (begin >= N) {
391 SharedBuffer::bufferFromData(mString)->release();
392 mString = getEmptyString();
393 return NO_ERROR;
394 }
395 if ((begin+len) > N) len = N-begin;
396 if (begin == 0 && len == N) {
397 return NO_ERROR;
398 }
399
400 if (begin > 0) {
401 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
402 ->editResize((N+1)*sizeof(char16_t));
403 if (!buf) {
404 return NO_MEMORY;
405 }
406 char16_t* str = (char16_t*)buf->data();
407 memmove(str, str+begin, (N-begin+1)*sizeof(char16_t));
408 mString = str;
409 }
410 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
411 ->editResize((len+1)*sizeof(char16_t));
412 if (buf) {
413 char16_t* str = (char16_t*)buf->data();
414 str[len] = 0;
415 mString = str;
416 return NO_ERROR;
417 }
418 return NO_MEMORY;
419}
420
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800421}; // namespace android