blob: e5a48e55a6890136a1e274ad3d544b01c886a1ca [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/String8.h>
18
19#include <utils/Log.h>
Kenny Rootba0165b2010-11-09 14:37:23 -080020#include <utils/Unicode.h>
21#include <utils/SharedBuffer.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080022#include <utils/String16.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023#include <utils/threads.h>
24
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025#include <ctype.h>
26
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090027/*
28 * Functions outside android is below the namespace android, since they use
29 * functions and constants in android namespace.
30 */
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080031
32// ---------------------------------------------------------------------------
33
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090034namespace android {
35
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080036// Separator used by resource paths. This is not platform dependent contrary
37// to OS_PATH_SEPARATOR.
38#define RES_PATH_SEPARATOR '/'
39
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080040static SharedBuffer* gEmptyStringBuf = NULL;
41static char* gEmptyString = NULL;
42
43extern int gDarwinCantLoadAllObjects;
44int gDarwinIsReallyAnnoying;
45
Mathias Agopian9eb2a3b2013-05-06 20:20:50 -070046void initialize_string8();
47
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080048static inline char* getEmptyString()
49{
50 gEmptyStringBuf->acquire();
51 return gEmptyString;
52}
53
54void initialize_string8()
55{
Dan Egnor88753ae2010-05-06 00:55:09 -070056 // HACK: This dummy dependency forces linking libutils Static.cpp,
57 // which is needed to initialize String8/String16 classes.
58 // These variables are named for Darwin, but are needed elsewhere too,
59 // including static linking on any platform.
60 gDarwinIsReallyAnnoying = gDarwinCantLoadAllObjects;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090061
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080062 SharedBuffer* buf = SharedBuffer::alloc(1);
63 char* str = (char*)buf->data();
64 *str = 0;
65 gEmptyStringBuf = buf;
66 gEmptyString = str;
67}
68
69void terminate_string8()
70{
71 SharedBuffer::bufferFromData(gEmptyString)->release();
72 gEmptyStringBuf = NULL;
73 gEmptyString = NULL;
74}
75
76// ---------------------------------------------------------------------------
77
78static char* allocFromUTF8(const char* in, size_t len)
79{
80 if (len > 0) {
Sergio Giro5b85b1d2015-08-18 14:44:54 +010081 if (len == SIZE_MAX) {
82 return NULL;
83 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080084 SharedBuffer* buf = SharedBuffer::alloc(len+1);
Steve Blockae074452012-01-09 18:35:44 +000085 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080086 if (buf) {
87 char* str = (char*)buf->data();
88 memcpy(str, in, len);
89 str[len] = 0;
90 return str;
91 }
92 return NULL;
93 }
94
95 return getEmptyString();
96}
97
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090098static char* allocFromUTF16(const char16_t* in, size_t len)
99{
Kenny Root9a2d83e2009-12-04 09:38:48 -0800100 if (len == 0) return getEmptyString();
101
Kenny Rootba0165b2010-11-09 14:37:23 -0800102 const ssize_t bytes = utf16_to_utf8_length(in, len);
103 if (bytes < 0) {
104 return getEmptyString();
105 }
Kenny Root9a2d83e2009-12-04 09:38:48 -0800106
107 SharedBuffer* buf = SharedBuffer::alloc(bytes+1);
Steve Blockae074452012-01-09 18:35:44 +0000108 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
Kenny Rootba0165b2010-11-09 14:37:23 -0800109 if (!buf) {
110 return getEmptyString();
Kenny Root9a2d83e2009-12-04 09:38:48 -0800111 }
112
Kenny Rootba0165b2010-11-09 14:37:23 -0800113 char* str = (char*)buf->data();
114 utf16_to_utf8(in, len, str);
115 return str;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900116}
117
118static char* allocFromUTF32(const char32_t* in, size_t len)
119{
Kenny Rootba0165b2010-11-09 14:37:23 -0800120 if (len == 0) {
121 return getEmptyString();
122 }
123
124 const ssize_t bytes = utf32_to_utf8_length(in, len);
125 if (bytes < 0) {
126 return getEmptyString();
127 }
128
129 SharedBuffer* buf = SharedBuffer::alloc(bytes+1);
Steve Blockae074452012-01-09 18:35:44 +0000130 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
Kenny Rootba0165b2010-11-09 14:37:23 -0800131 if (!buf) {
132 return getEmptyString();
133 }
134
135 char* str = (char*) buf->data();
136 utf32_to_utf8(in, len, str);
137
138 return str;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900139}
140
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800141// ---------------------------------------------------------------------------
142
143String8::String8()
144 : mString(getEmptyString())
145{
146}
147
Mathias Agopian4485d0d2013-05-08 16:04:13 -0700148String8::String8(StaticLinkage)
149 : mString(0)
150{
151 // this constructor is used when we can't rely on the static-initializers
152 // having run. In this case we always allocate an empty string. It's less
153 // efficient than using getEmptyString(), but we assume it's uncommon.
154
155 char* data = static_cast<char*>(
156 SharedBuffer::alloc(sizeof(char))->data());
157 data[0] = 0;
158 mString = data;
159}
160
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800161String8::String8(const String8& o)
162 : mString(o.mString)
163{
164 SharedBuffer::bufferFromData(mString)->acquire();
165}
166
167String8::String8(const char* o)
168 : mString(allocFromUTF8(o, strlen(o)))
169{
170 if (mString == NULL) {
171 mString = getEmptyString();
172 }
173}
174
175String8::String8(const char* o, size_t len)
176 : mString(allocFromUTF8(o, len))
177{
178 if (mString == NULL) {
179 mString = getEmptyString();
180 }
181}
182
183String8::String8(const String16& o)
184 : mString(allocFromUTF16(o.string(), o.size()))
185{
186}
187
188String8::String8(const char16_t* o)
189 : mString(allocFromUTF16(o, strlen16(o)))
190{
191}
192
193String8::String8(const char16_t* o, size_t len)
194 : mString(allocFromUTF16(o, len))
195{
196}
197
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900198String8::String8(const char32_t* o)
199 : mString(allocFromUTF32(o, strlen32(o)))
200{
201}
202
203String8::String8(const char32_t* o, size_t len)
204 : mString(allocFromUTF32(o, len))
205{
206}
207
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800208String8::~String8()
209{
210 SharedBuffer::bufferFromData(mString)->release();
211}
212
Jeff Brown1d618d62010-12-02 13:50:46 -0800213String8 String8::format(const char* fmt, ...)
214{
215 va_list args;
216 va_start(args, fmt);
217
218 String8 result(formatV(fmt, args));
219
220 va_end(args);
221 return result;
222}
223
224String8 String8::formatV(const char* fmt, va_list args)
225{
226 String8 result;
227 result.appendFormatV(fmt, args);
228 return result;
229}
230
Jeff Brown48da31b2010-09-12 17:55:08 -0700231void String8::clear() {
232 SharedBuffer::bufferFromData(mString)->release();
233 mString = getEmptyString();
234}
235
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800236void String8::setTo(const String8& other)
237{
238 SharedBuffer::bufferFromData(other.mString)->acquire();
239 SharedBuffer::bufferFromData(mString)->release();
240 mString = other.mString;
241}
242
243status_t String8::setTo(const char* other)
244{
Andreas Huber10e5da52010-06-10 11:14:26 -0700245 const char *newString = allocFromUTF8(other, strlen(other));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800246 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700247 mString = newString;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800248 if (mString) return NO_ERROR;
249
250 mString = getEmptyString();
251 return NO_MEMORY;
252}
253
254status_t String8::setTo(const char* other, size_t len)
255{
Andreas Huber10e5da52010-06-10 11:14:26 -0700256 const char *newString = allocFromUTF8(other, len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800257 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700258 mString = newString;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800259 if (mString) return NO_ERROR;
260
261 mString = getEmptyString();
262 return NO_MEMORY;
263}
264
265status_t String8::setTo(const char16_t* other, size_t len)
266{
Andreas Huber10e5da52010-06-10 11:14:26 -0700267 const char *newString = allocFromUTF16(other, len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800268 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700269 mString = newString;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800270 if (mString) return NO_ERROR;
271
272 mString = getEmptyString();
273 return NO_MEMORY;
274}
275
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900276status_t String8::setTo(const char32_t* other, size_t len)
277{
Andreas Huber10e5da52010-06-10 11:14:26 -0700278 const char *newString = allocFromUTF32(other, len);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900279 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700280 mString = newString;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900281 if (mString) return NO_ERROR;
282
283 mString = getEmptyString();
284 return NO_MEMORY;
285}
286
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800287status_t String8::append(const String8& other)
288{
289 const size_t otherLen = other.bytes();
290 if (bytes() == 0) {
291 setTo(other);
292 return NO_ERROR;
293 } else if (otherLen == 0) {
294 return NO_ERROR;
295 }
296
297 return real_append(other.string(), otherLen);
298}
299
300status_t String8::append(const char* other)
301{
302 return append(other, strlen(other));
303}
304
305status_t String8::append(const char* other, size_t otherLen)
306{
307 if (bytes() == 0) {
308 return setTo(other, otherLen);
309 } else if (otherLen == 0) {
310 return NO_ERROR;
311 }
312
313 return real_append(other, otherLen);
314}
315
Jeff Brown35a154e2010-07-15 23:54:05 -0700316status_t String8::appendFormat(const char* fmt, ...)
317{
Jeff Brown647925d2010-11-10 16:03:06 -0800318 va_list args;
319 va_start(args, fmt);
Jeff Brown35a154e2010-07-15 23:54:05 -0700320
Jeff Brown647925d2010-11-10 16:03:06 -0800321 status_t result = appendFormatV(fmt, args);
322
323 va_end(args);
324 return result;
325}
326
327status_t String8::appendFormatV(const char* fmt, va_list args)
328{
Jeff Brown35a154e2010-07-15 23:54:05 -0700329 int result = NO_ERROR;
Jeff Brown647925d2010-11-10 16:03:06 -0800330 int n = vsnprintf(NULL, 0, fmt, args);
Jeff Brown35a154e2010-07-15 23:54:05 -0700331 if (n != 0) {
332 size_t oldLength = length();
333 char* buf = lockBuffer(oldLength + n);
334 if (buf) {
Jeff Brown647925d2010-11-10 16:03:06 -0800335 vsnprintf(buf + oldLength, n + 1, fmt, args);
Jeff Brown35a154e2010-07-15 23:54:05 -0700336 } else {
337 result = NO_MEMORY;
338 }
339 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700340 return result;
341}
342
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800343status_t String8::real_append(const char* other, size_t otherLen)
344{
345 const size_t myLen = bytes();
346
347 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
348 ->editResize(myLen+otherLen+1);
349 if (buf) {
350 char* str = (char*)buf->data();
351 mString = str;
352 str += myLen;
353 memcpy(str, other, otherLen);
354 str[otherLen] = '\0';
355 return NO_ERROR;
356 }
357 return NO_MEMORY;
358}
359
360char* String8::lockBuffer(size_t size)
361{
362 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
363 ->editResize(size+1);
364 if (buf) {
365 char* str = (char*)buf->data();
366 mString = str;
367 return str;
368 }
369 return NULL;
370}
371
372void String8::unlockBuffer()
373{
374 unlockBuffer(strlen(mString));
375}
376
377status_t String8::unlockBuffer(size_t size)
378{
379 if (size != this->size()) {
380 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
381 ->editResize(size+1);
Jeff Brown35a154e2010-07-15 23:54:05 -0700382 if (! buf) {
383 return NO_MEMORY;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800384 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700385
386 char* str = (char*)buf->data();
387 str[size] = 0;
388 mString = str;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800389 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700390
391 return NO_ERROR;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800392}
393
394ssize_t String8::find(const char* other, size_t start) const
395{
396 size_t len = size();
397 if (start >= len) {
398 return -1;
399 }
400 const char* s = mString+start;
401 const char* p = strstr(s, other);
402 return p ? p-mString : -1;
403}
404
405void String8::toLower()
406{
407 toLower(0, size());
408}
409
410void String8::toLower(size_t start, size_t length)
411{
412 const size_t len = size();
413 if (start >= len) {
414 return;
415 }
416 if (start+length > len) {
417 length = len-start;
418 }
419 char* buf = lockBuffer(len);
420 buf += start;
421 while (length > 0) {
422 *buf = tolower(*buf);
423 buf++;
424 length--;
425 }
426 unlockBuffer(len);
427}
428
429void String8::toUpper()
430{
431 toUpper(0, size());
432}
433
434void String8::toUpper(size_t start, size_t length)
435{
436 const size_t len = size();
437 if (start >= len) {
438 return;
439 }
440 if (start+length > len) {
441 length = len-start;
442 }
443 char* buf = lockBuffer(len);
444 buf += start;
445 while (length > 0) {
446 *buf = toupper(*buf);
447 buf++;
448 length--;
449 }
450 unlockBuffer(len);
451}
452
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900453size_t String8::getUtf32Length() const
454{
Kenny Rootba0165b2010-11-09 14:37:23 -0800455 return utf8_to_utf32_length(mString, length());
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900456}
457
458int32_t String8::getUtf32At(size_t index, size_t *next_index) const
459{
Kenny Rootba0165b2010-11-09 14:37:23 -0800460 return utf32_from_utf8_at(mString, length(), index, next_index);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900461}
462
Kenny Rootba0165b2010-11-09 14:37:23 -0800463void String8::getUtf32(char32_t* dst) const
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900464{
Kenny Rootba0165b2010-11-09 14:37:23 -0800465 utf8_to_utf32(mString, length(), dst);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900466}
467
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800468// ---------------------------------------------------------------------------
469// Path functions
470
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800471void String8::setPathName(const char* name)
472{
473 setPathName(name, strlen(name));
474}
475
476void String8::setPathName(const char* name, size_t len)
477{
478 char* buf = lockBuffer(len);
479
480 memcpy(buf, name, len);
481
482 // remove trailing path separator, if present
483 if (len > 0 && buf[len-1] == OS_PATH_SEPARATOR)
484 len--;
485
486 buf[len] = '\0';
487
488 unlockBuffer(len);
489}
490
491String8 String8::getPathLeaf(void) const
492{
493 const char* cp;
494 const char*const buf = mString;
495
496 cp = strrchr(buf, OS_PATH_SEPARATOR);
497 if (cp == NULL)
498 return String8(*this);
499 else
500 return String8(cp+1);
501}
502
503String8 String8::getPathDir(void) const
504{
505 const char* cp;
506 const char*const str = mString;
507
508 cp = strrchr(str, OS_PATH_SEPARATOR);
509 if (cp == NULL)
510 return String8("");
511 else
512 return String8(str, cp - str);
513}
514
515String8 String8::walkPath(String8* outRemains) const
516{
517 const char* cp;
518 const char*const str = mString;
519 const char* buf = str;
520
521 cp = strchr(buf, OS_PATH_SEPARATOR);
522 if (cp == buf) {
523 // don't include a leading '/'.
524 buf = buf+1;
525 cp = strchr(buf, OS_PATH_SEPARATOR);
526 }
527
528 if (cp == NULL) {
529 String8 res = buf != str ? String8(buf) : *this;
530 if (outRemains) *outRemains = String8("");
531 return res;
532 }
533
534 String8 res(buf, cp-buf);
535 if (outRemains) *outRemains = String8(cp+1);
536 return res;
537}
538
539/*
540 * Helper function for finding the start of an extension in a pathname.
541 *
542 * Returns a pointer inside mString, or NULL if no extension was found.
543 */
544char* String8::find_extension(void) const
545{
546 const char* lastSlash;
547 const char* lastDot;
548 int extLen;
549 const char* const str = mString;
550
551 // only look at the filename
552 lastSlash = strrchr(str, OS_PATH_SEPARATOR);
553 if (lastSlash == NULL)
554 lastSlash = str;
555 else
556 lastSlash++;
557
558 // find the last dot
559 lastDot = strrchr(lastSlash, '.');
560 if (lastDot == NULL)
561 return NULL;
562
563 // looks good, ship it
564 return const_cast<char*>(lastDot);
565}
566
567String8 String8::getPathExtension(void) const
568{
569 char* ext;
570
571 ext = find_extension();
572 if (ext != NULL)
573 return String8(ext);
574 else
575 return String8("");
576}
577
578String8 String8::getBasePath(void) const
579{
580 char* ext;
581 const char* const str = mString;
582
583 ext = find_extension();
584 if (ext == NULL)
585 return String8(*this);
586 else
587 return String8(str, ext - str);
588}
589
590String8& String8::appendPath(const char* name)
591{
592 // TODO: The test below will fail for Win32 paths. Fix later or ignore.
593 if (name[0] != OS_PATH_SEPARATOR) {
594 if (*name == '\0') {
595 // nothing to do
596 return *this;
597 }
598
599 size_t len = length();
600 if (len == 0) {
601 // no existing filename, just use the new one
602 setPathName(name);
603 return *this;
604 }
605
606 // make room for oldPath + '/' + newPath
607 int newlen = strlen(name);
608
609 char* buf = lockBuffer(len+1+newlen);
610
611 // insert a '/' if needed
612 if (buf[len-1] != OS_PATH_SEPARATOR)
613 buf[len++] = OS_PATH_SEPARATOR;
614
615 memcpy(buf+len, name, newlen+1);
616 len += newlen;
617
618 unlockBuffer(len);
619
620 return *this;
621 } else {
622 setPathName(name);
623 return *this;
624 }
625}
626
627String8& String8::convertToResPath()
628{
629#if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR
630 size_t len = length();
631 if (len > 0) {
632 char * buf = lockBuffer(len);
633 for (char * end = buf + len; buf < end; ++buf) {
634 if (*buf == OS_PATH_SEPARATOR)
635 *buf = RES_PATH_SEPARATOR;
636 }
637 unlockBuffer(len);
638 }
639#endif
640 return *this;
641}
642
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800643}; // namespace android