blob: 94b823128b25a57e4bfb674ace7d85e0d0502fdd [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
Abhishek Aryae0dce902015-08-20 17:38:16 -070017#define __STDC_LIMIT_MACROS
18#include <stdint.h>
19
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080020#include <utils/String8.h>
21
22#include <utils/Log.h>
Kenny Rootba0165b2010-11-09 14:37:23 -080023#include <utils/Unicode.h>
24#include <utils/SharedBuffer.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025#include <utils/String16.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026#include <utils/threads.h>
27
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028#include <ctype.h>
29
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090030/*
31 * Functions outside android is below the namespace android, since they use
32 * functions and constants in android namespace.
33 */
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080034
35// ---------------------------------------------------------------------------
36
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090037namespace android {
38
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080039// Separator used by resource paths. This is not platform dependent contrary
40// to OS_PATH_SEPARATOR.
41#define RES_PATH_SEPARATOR '/'
42
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080043static SharedBuffer* gEmptyStringBuf = NULL;
44static char* gEmptyString = NULL;
45
46extern int gDarwinCantLoadAllObjects;
47int gDarwinIsReallyAnnoying;
48
Mathias Agopian9eb2a3b2013-05-06 20:20:50 -070049void initialize_string8();
50
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080051static inline char* getEmptyString()
52{
53 gEmptyStringBuf->acquire();
54 return gEmptyString;
55}
56
57void initialize_string8()
58{
Dan Egnor88753ae2010-05-06 00:55:09 -070059 // HACK: This dummy dependency forces linking libutils Static.cpp,
60 // which is needed to initialize String8/String16 classes.
61 // These variables are named for Darwin, but are needed elsewhere too,
62 // including static linking on any platform.
63 gDarwinIsReallyAnnoying = gDarwinCantLoadAllObjects;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090064
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080065 SharedBuffer* buf = SharedBuffer::alloc(1);
66 char* str = (char*)buf->data();
67 *str = 0;
68 gEmptyStringBuf = buf;
69 gEmptyString = str;
70}
71
72void terminate_string8()
73{
74 SharedBuffer::bufferFromData(gEmptyString)->release();
75 gEmptyStringBuf = NULL;
76 gEmptyString = NULL;
77}
78
79// ---------------------------------------------------------------------------
80
81static char* allocFromUTF8(const char* in, size_t len)
82{
83 if (len > 0) {
Sergio Giro5b85b1d2015-08-18 14:44:54 +010084 if (len == SIZE_MAX) {
85 return NULL;
86 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080087 SharedBuffer* buf = SharedBuffer::alloc(len+1);
Steve Blockae074452012-01-09 18:35:44 +000088 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080089 if (buf) {
90 char* str = (char*)buf->data();
91 memcpy(str, in, len);
92 str[len] = 0;
93 return str;
94 }
95 return NULL;
96 }
97
98 return getEmptyString();
99}
100
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900101static char* allocFromUTF16(const char16_t* in, size_t len)
102{
Kenny Root9a2d83e2009-12-04 09:38:48 -0800103 if (len == 0) return getEmptyString();
104
Sergio Girob0224472016-06-28 18:02:29 +0100105 // Allow for closing '\0'
106 const ssize_t resultStrLen = utf16_to_utf8_length(in, len) + 1;
107 if (resultStrLen < 1) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800108 return getEmptyString();
109 }
Kenny Root9a2d83e2009-12-04 09:38:48 -0800110
Sergio Girob0224472016-06-28 18:02:29 +0100111 SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
Steve Blockae074452012-01-09 18:35:44 +0000112 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
Kenny Rootba0165b2010-11-09 14:37:23 -0800113 if (!buf) {
114 return getEmptyString();
Kenny Root9a2d83e2009-12-04 09:38:48 -0800115 }
116
Sergio Girob0224472016-06-28 18:02:29 +0100117 char* resultStr = (char*)buf->data();
118 utf16_to_utf8(in, len, resultStr, resultStrLen);
119 return resultStr;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900120}
121
122static char* allocFromUTF32(const char32_t* in, size_t len)
123{
Kenny Rootba0165b2010-11-09 14:37:23 -0800124 if (len == 0) {
125 return getEmptyString();
126 }
127
Sergio Girob0224472016-06-28 18:02:29 +0100128 const ssize_t resultStrLen = utf32_to_utf8_length(in, len) + 1;
129 if (resultStrLen < 1) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800130 return getEmptyString();
131 }
132
Sergio Girob0224472016-06-28 18:02:29 +0100133 SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
Steve Blockae074452012-01-09 18:35:44 +0000134 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
Kenny Rootba0165b2010-11-09 14:37:23 -0800135 if (!buf) {
136 return getEmptyString();
137 }
138
Sergio Girob0224472016-06-28 18:02:29 +0100139 char* resultStr = (char*) buf->data();
140 utf32_to_utf8(in, len, resultStr, resultStrLen);
Kenny Rootba0165b2010-11-09 14:37:23 -0800141
Sergio Girob0224472016-06-28 18:02:29 +0100142 return resultStr;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900143}
144
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800145// ---------------------------------------------------------------------------
146
147String8::String8()
148 : mString(getEmptyString())
149{
150}
151
Mathias Agopian4485d0d2013-05-08 16:04:13 -0700152String8::String8(StaticLinkage)
153 : mString(0)
154{
155 // this constructor is used when we can't rely on the static-initializers
156 // having run. In this case we always allocate an empty string. It's less
157 // efficient than using getEmptyString(), but we assume it's uncommon.
158
159 char* data = static_cast<char*>(
160 SharedBuffer::alloc(sizeof(char))->data());
161 data[0] = 0;
162 mString = data;
163}
164
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800165String8::String8(const String8& o)
166 : mString(o.mString)
167{
168 SharedBuffer::bufferFromData(mString)->acquire();
169}
170
171String8::String8(const char* o)
172 : mString(allocFromUTF8(o, strlen(o)))
173{
174 if (mString == NULL) {
175 mString = getEmptyString();
176 }
177}
178
179String8::String8(const char* o, size_t len)
180 : mString(allocFromUTF8(o, len))
181{
182 if (mString == NULL) {
183 mString = getEmptyString();
184 }
185}
186
187String8::String8(const String16& o)
188 : mString(allocFromUTF16(o.string(), o.size()))
189{
190}
191
192String8::String8(const char16_t* o)
193 : mString(allocFromUTF16(o, strlen16(o)))
194{
195}
196
197String8::String8(const char16_t* o, size_t len)
198 : mString(allocFromUTF16(o, len))
199{
200}
201
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900202String8::String8(const char32_t* o)
203 : mString(allocFromUTF32(o, strlen32(o)))
204{
205}
206
207String8::String8(const char32_t* o, size_t len)
208 : mString(allocFromUTF32(o, len))
209{
210}
211
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800212String8::~String8()
213{
214 SharedBuffer::bufferFromData(mString)->release();
215}
216
Jeff Brown1d618d62010-12-02 13:50:46 -0800217String8 String8::format(const char* fmt, ...)
218{
219 va_list args;
220 va_start(args, fmt);
221
222 String8 result(formatV(fmt, args));
223
224 va_end(args);
225 return result;
226}
227
228String8 String8::formatV(const char* fmt, va_list args)
229{
230 String8 result;
231 result.appendFormatV(fmt, args);
232 return result;
233}
234
Jeff Brown48da31b2010-09-12 17:55:08 -0700235void String8::clear() {
236 SharedBuffer::bufferFromData(mString)->release();
237 mString = getEmptyString();
238}
239
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800240void String8::setTo(const String8& other)
241{
242 SharedBuffer::bufferFromData(other.mString)->acquire();
243 SharedBuffer::bufferFromData(mString)->release();
244 mString = other.mString;
245}
246
247status_t String8::setTo(const char* other)
248{
Andreas Huber10e5da52010-06-10 11:14:26 -0700249 const char *newString = allocFromUTF8(other, strlen(other));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800250 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700251 mString = newString;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800252 if (mString) return NO_ERROR;
253
254 mString = getEmptyString();
255 return NO_MEMORY;
256}
257
258status_t String8::setTo(const char* other, size_t len)
259{
Andreas Huber10e5da52010-06-10 11:14:26 -0700260 const char *newString = allocFromUTF8(other, len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800261 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700262 mString = newString;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800263 if (mString) return NO_ERROR;
264
265 mString = getEmptyString();
266 return NO_MEMORY;
267}
268
269status_t String8::setTo(const char16_t* other, size_t len)
270{
Andreas Huber10e5da52010-06-10 11:14:26 -0700271 const char *newString = allocFromUTF16(other, len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800272 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700273 mString = newString;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800274 if (mString) return NO_ERROR;
275
276 mString = getEmptyString();
277 return NO_MEMORY;
278}
279
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900280status_t String8::setTo(const char32_t* other, size_t len)
281{
Andreas Huber10e5da52010-06-10 11:14:26 -0700282 const char *newString = allocFromUTF32(other, len);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900283 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700284 mString = newString;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900285 if (mString) return NO_ERROR;
286
287 mString = getEmptyString();
288 return NO_MEMORY;
289}
290
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800291status_t String8::append(const String8& other)
292{
293 const size_t otherLen = other.bytes();
294 if (bytes() == 0) {
295 setTo(other);
296 return NO_ERROR;
297 } else if (otherLen == 0) {
298 return NO_ERROR;
299 }
300
301 return real_append(other.string(), otherLen);
302}
303
304status_t String8::append(const char* other)
305{
306 return append(other, strlen(other));
307}
308
309status_t String8::append(const char* other, size_t otherLen)
310{
311 if (bytes() == 0) {
312 return setTo(other, otherLen);
313 } else if (otherLen == 0) {
314 return NO_ERROR;
315 }
316
317 return real_append(other, otherLen);
318}
319
Jeff Brown35a154e2010-07-15 23:54:05 -0700320status_t String8::appendFormat(const char* fmt, ...)
321{
Jeff Brown647925d2010-11-10 16:03:06 -0800322 va_list args;
323 va_start(args, fmt);
Jeff Brown35a154e2010-07-15 23:54:05 -0700324
Jeff Brown647925d2010-11-10 16:03:06 -0800325 status_t result = appendFormatV(fmt, args);
326
327 va_end(args);
328 return result;
329}
330
331status_t String8::appendFormatV(const char* fmt, va_list args)
332{
Fengwei Yinfff9d112014-02-27 01:17:09 +0800333 int n, result = NO_ERROR;
334 va_list tmp_args;
335
336 /* args is undefined after vsnprintf.
337 * So we need a copy here to avoid the
338 * second vsnprintf access undefined args.
339 */
340 va_copy(tmp_args, args);
341 n = vsnprintf(NULL, 0, fmt, tmp_args);
342 va_end(tmp_args);
343
Jeff Brown35a154e2010-07-15 23:54:05 -0700344 if (n != 0) {
345 size_t oldLength = length();
346 char* buf = lockBuffer(oldLength + n);
347 if (buf) {
Jeff Brown647925d2010-11-10 16:03:06 -0800348 vsnprintf(buf + oldLength, n + 1, fmt, args);
Jeff Brown35a154e2010-07-15 23:54:05 -0700349 } else {
350 result = NO_MEMORY;
351 }
352 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700353 return result;
354}
355
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800356status_t String8::real_append(const char* other, size_t otherLen)
357{
358 const size_t myLen = bytes();
359
360 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
361 ->editResize(myLen+otherLen+1);
362 if (buf) {
363 char* str = (char*)buf->data();
364 mString = str;
365 str += myLen;
366 memcpy(str, other, otherLen);
367 str[otherLen] = '\0';
368 return NO_ERROR;
369 }
370 return NO_MEMORY;
371}
372
373char* String8::lockBuffer(size_t size)
374{
375 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
376 ->editResize(size+1);
377 if (buf) {
378 char* str = (char*)buf->data();
379 mString = str;
380 return str;
381 }
382 return NULL;
383}
384
385void String8::unlockBuffer()
386{
387 unlockBuffer(strlen(mString));
388}
389
390status_t String8::unlockBuffer(size_t size)
391{
392 if (size != this->size()) {
393 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
394 ->editResize(size+1);
Jeff Brown35a154e2010-07-15 23:54:05 -0700395 if (! buf) {
396 return NO_MEMORY;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800397 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700398
399 char* str = (char*)buf->data();
400 str[size] = 0;
401 mString = str;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800402 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700403
404 return NO_ERROR;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800405}
406
407ssize_t String8::find(const char* other, size_t start) const
408{
409 size_t len = size();
410 if (start >= len) {
411 return -1;
412 }
413 const char* s = mString+start;
414 const char* p = strstr(s, other);
415 return p ? p-mString : -1;
416}
417
Jeff Brown5ee915a2014-06-06 19:30:15 -0700418bool String8::removeAll(const char* other) {
419 ssize_t index = find(other);
420 if (index < 0) return false;
421
422 char* buf = lockBuffer(size());
423 if (!buf) return false; // out of memory
424
425 size_t skip = strlen(other);
426 size_t len = size();
427 size_t tail = index;
428 while (size_t(index) < len) {
429 ssize_t next = find(other, index + skip);
430 if (next < 0) {
431 next = len;
432 }
433
434 memcpy(buf + tail, buf + index + skip, next - index - skip);
435 tail += next - index - skip;
436 index = next;
437 }
438 unlockBuffer(tail);
439 return true;
440}
441
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800442void String8::toLower()
443{
444 toLower(0, size());
445}
446
447void String8::toLower(size_t start, size_t length)
448{
449 const size_t len = size();
450 if (start >= len) {
451 return;
452 }
453 if (start+length > len) {
454 length = len-start;
455 }
456 char* buf = lockBuffer(len);
457 buf += start;
458 while (length > 0) {
459 *buf = tolower(*buf);
460 buf++;
461 length--;
462 }
463 unlockBuffer(len);
464}
465
466void String8::toUpper()
467{
468 toUpper(0, size());
469}
470
471void String8::toUpper(size_t start, size_t length)
472{
473 const size_t len = size();
474 if (start >= len) {
475 return;
476 }
477 if (start+length > len) {
478 length = len-start;
479 }
480 char* buf = lockBuffer(len);
481 buf += start;
482 while (length > 0) {
483 *buf = toupper(*buf);
484 buf++;
485 length--;
486 }
487 unlockBuffer(len);
488}
489
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900490size_t String8::getUtf32Length() const
491{
Kenny Rootba0165b2010-11-09 14:37:23 -0800492 return utf8_to_utf32_length(mString, length());
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900493}
494
495int32_t String8::getUtf32At(size_t index, size_t *next_index) const
496{
Kenny Rootba0165b2010-11-09 14:37:23 -0800497 return utf32_from_utf8_at(mString, length(), index, next_index);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900498}
499
Kenny Rootba0165b2010-11-09 14:37:23 -0800500void String8::getUtf32(char32_t* dst) const
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900501{
Kenny Rootba0165b2010-11-09 14:37:23 -0800502 utf8_to_utf32(mString, length(), dst);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900503}
504
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800505// ---------------------------------------------------------------------------
506// Path functions
507
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800508void String8::setPathName(const char* name)
509{
510 setPathName(name, strlen(name));
511}
512
513void String8::setPathName(const char* name, size_t len)
514{
515 char* buf = lockBuffer(len);
516
517 memcpy(buf, name, len);
518
519 // remove trailing path separator, if present
520 if (len > 0 && buf[len-1] == OS_PATH_SEPARATOR)
521 len--;
522
523 buf[len] = '\0';
524
525 unlockBuffer(len);
526}
527
528String8 String8::getPathLeaf(void) const
529{
530 const char* cp;
531 const char*const buf = mString;
532
533 cp = strrchr(buf, OS_PATH_SEPARATOR);
534 if (cp == NULL)
535 return String8(*this);
536 else
537 return String8(cp+1);
538}
539
540String8 String8::getPathDir(void) const
541{
542 const char* cp;
543 const char*const str = mString;
544
545 cp = strrchr(str, OS_PATH_SEPARATOR);
546 if (cp == NULL)
547 return String8("");
548 else
549 return String8(str, cp - str);
550}
551
552String8 String8::walkPath(String8* outRemains) const
553{
554 const char* cp;
555 const char*const str = mString;
556 const char* buf = str;
557
558 cp = strchr(buf, OS_PATH_SEPARATOR);
559 if (cp == buf) {
560 // don't include a leading '/'.
561 buf = buf+1;
562 cp = strchr(buf, OS_PATH_SEPARATOR);
563 }
564
565 if (cp == NULL) {
566 String8 res = buf != str ? String8(buf) : *this;
567 if (outRemains) *outRemains = String8("");
568 return res;
569 }
570
571 String8 res(buf, cp-buf);
572 if (outRemains) *outRemains = String8(cp+1);
573 return res;
574}
575
576/*
577 * Helper function for finding the start of an extension in a pathname.
578 *
579 * Returns a pointer inside mString, or NULL if no extension was found.
580 */
581char* String8::find_extension(void) const
582{
583 const char* lastSlash;
584 const char* lastDot;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800585 const char* const str = mString;
586
587 // only look at the filename
588 lastSlash = strrchr(str, OS_PATH_SEPARATOR);
589 if (lastSlash == NULL)
590 lastSlash = str;
591 else
592 lastSlash++;
593
594 // find the last dot
595 lastDot = strrchr(lastSlash, '.');
596 if (lastDot == NULL)
597 return NULL;
598
599 // looks good, ship it
600 return const_cast<char*>(lastDot);
601}
602
603String8 String8::getPathExtension(void) const
604{
605 char* ext;
606
607 ext = find_extension();
608 if (ext != NULL)
609 return String8(ext);
610 else
611 return String8("");
612}
613
614String8 String8::getBasePath(void) const
615{
616 char* ext;
617 const char* const str = mString;
618
619 ext = find_extension();
620 if (ext == NULL)
621 return String8(*this);
622 else
623 return String8(str, ext - str);
624}
625
626String8& String8::appendPath(const char* name)
627{
628 // TODO: The test below will fail for Win32 paths. Fix later or ignore.
629 if (name[0] != OS_PATH_SEPARATOR) {
630 if (*name == '\0') {
631 // nothing to do
632 return *this;
633 }
634
635 size_t len = length();
636 if (len == 0) {
637 // no existing filename, just use the new one
638 setPathName(name);
639 return *this;
640 }
641
642 // make room for oldPath + '/' + newPath
643 int newlen = strlen(name);
644
645 char* buf = lockBuffer(len+1+newlen);
646
647 // insert a '/' if needed
648 if (buf[len-1] != OS_PATH_SEPARATOR)
649 buf[len++] = OS_PATH_SEPARATOR;
650
651 memcpy(buf+len, name, newlen+1);
652 len += newlen;
653
654 unlockBuffer(len);
655
656 return *this;
657 } else {
658 setPathName(name);
659 return *this;
660 }
661}
662
663String8& String8::convertToResPath()
664{
665#if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR
666 size_t len = length();
667 if (len > 0) {
668 char * buf = lockBuffer(len);
669 for (char * end = buf + len; buf < end; ++buf) {
670 if (*buf == OS_PATH_SEPARATOR)
671 *buf = RES_PATH_SEPARATOR;
672 }
673 unlockBuffer(len);
674 }
675#endif
676 return *this;
677}
678
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800679}; // namespace android