blob: 9d50e0bc51d812f78e7f1a88e64d7ba631101d7d [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
Elliott Hughes1f8bc862015-07-29 14:02:29 -070022#include <utils/Compat.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023#include <utils/Log.h>
24#include <utils/String16.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025
Steven Morelandd21cfab2017-03-10 08:58:36 -080026#include <ctype.h>
27
Sergio Girod2529f22015-09-23 16:22:59 +010028#include "SharedBuffer.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
Steven Moreland241b93c2018-03-06 09:11:29 -080043static inline char* getEmptyString() {
44 static SharedBuffer* gEmptyStringBuf = [] {
45 SharedBuffer* buf = SharedBuffer::alloc(1);
46 char* str = static_cast<char*>(buf->data());
47 *str = 0;
48 return buf;
49 }();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080051 gEmptyStringBuf->acquire();
Steven Moreland241b93c2018-03-06 09:11:29 -080052 return static_cast<char*>(gEmptyStringBuf->data());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080053}
54
55// ---------------------------------------------------------------------------
56
57static char* allocFromUTF8(const char* in, size_t len)
58{
59 if (len > 0) {
Sergio Giroebabef22015-08-18 14:44:54 +010060 if (len == SIZE_MAX) {
Yi Konge1731a42018-07-16 18:11:34 -070061 return nullptr;
Sergio Giroebabef22015-08-18 14:44:54 +010062 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080063 SharedBuffer* buf = SharedBuffer::alloc(len+1);
Steve Blockae074452012-01-09 18:35:44 +000064 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080065 if (buf) {
66 char* str = (char*)buf->data();
67 memcpy(str, in, len);
68 str[len] = 0;
69 return str;
70 }
Yi Konge1731a42018-07-16 18:11:34 -070071 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080072 }
73
74 return getEmptyString();
75}
76
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090077static char* allocFromUTF16(const char16_t* in, size_t len)
78{
Kenny Root9a2d83e2009-12-04 09:38:48 -080079 if (len == 0) return getEmptyString();
80
Sergio Giroc4966a32016-06-28 18:02:29 +010081 // Allow for closing '\0'
82 const ssize_t resultStrLen = utf16_to_utf8_length(in, len) + 1;
83 if (resultStrLen < 1) {
Kenny Rootba0165b2010-11-09 14:37:23 -080084 return getEmptyString();
85 }
Kenny Root9a2d83e2009-12-04 09:38:48 -080086
Sergio Giroc4966a32016-06-28 18:02:29 +010087 SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
Steve Blockae074452012-01-09 18:35:44 +000088 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
Kenny Rootba0165b2010-11-09 14:37:23 -080089 if (!buf) {
90 return getEmptyString();
Kenny Root9a2d83e2009-12-04 09:38:48 -080091 }
92
Sergio Giroc4966a32016-06-28 18:02:29 +010093 char* resultStr = (char*)buf->data();
94 utf16_to_utf8(in, len, resultStr, resultStrLen);
95 return resultStr;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090096}
97
98static char* allocFromUTF32(const char32_t* in, size_t len)
99{
Kenny Rootba0165b2010-11-09 14:37:23 -0800100 if (len == 0) {
101 return getEmptyString();
102 }
103
Sergio Giroc4966a32016-06-28 18:02:29 +0100104 const ssize_t resultStrLen = utf32_to_utf8_length(in, len) + 1;
105 if (resultStrLen < 1) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800106 return getEmptyString();
107 }
108
Sergio Giroc4966a32016-06-28 18:02:29 +0100109 SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
Steve Blockae074452012-01-09 18:35:44 +0000110 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
Kenny Rootba0165b2010-11-09 14:37:23 -0800111 if (!buf) {
112 return getEmptyString();
113 }
114
Sergio Giroc4966a32016-06-28 18:02:29 +0100115 char* resultStr = (char*) buf->data();
116 utf32_to_utf8(in, len, resultStr, resultStrLen);
Kenny Rootba0165b2010-11-09 14:37:23 -0800117
Sergio Giroc4966a32016-06-28 18:02:29 +0100118 return resultStr;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900119}
120
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800121// ---------------------------------------------------------------------------
122
123String8::String8()
124 : mString(getEmptyString())
125{
126}
127
Mathias Agopian4485d0d2013-05-08 16:04:13 -0700128String8::String8(StaticLinkage)
Yi Konge1731a42018-07-16 18:11:34 -0700129 : mString(nullptr)
Mathias Agopian4485d0d2013-05-08 16:04:13 -0700130{
131 // this constructor is used when we can't rely on the static-initializers
132 // having run. In this case we always allocate an empty string. It's less
133 // efficient than using getEmptyString(), but we assume it's uncommon.
134
135 char* data = static_cast<char*>(
136 SharedBuffer::alloc(sizeof(char))->data());
137 data[0] = 0;
138 mString = data;
139}
140
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800141String8::String8(const String8& o)
142 : mString(o.mString)
143{
144 SharedBuffer::bufferFromData(mString)->acquire();
145}
146
147String8::String8(const char* o)
148 : mString(allocFromUTF8(o, strlen(o)))
149{
Yi Konge1731a42018-07-16 18:11:34 -0700150 if (mString == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800151 mString = getEmptyString();
152 }
153}
154
155String8::String8(const char* o, size_t len)
156 : mString(allocFromUTF8(o, len))
157{
Yi Konge1731a42018-07-16 18:11:34 -0700158 if (mString == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800159 mString = getEmptyString();
160 }
161}
162
163String8::String8(const String16& o)
164 : mString(allocFromUTF16(o.string(), o.size()))
165{
166}
167
168String8::String8(const char16_t* o)
169 : mString(allocFromUTF16(o, strlen16(o)))
170{
171}
172
173String8::String8(const char16_t* o, size_t len)
174 : mString(allocFromUTF16(o, len))
175{
176}
177
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900178String8::String8(const char32_t* o)
179 : mString(allocFromUTF32(o, strlen32(o)))
180{
181}
182
183String8::String8(const char32_t* o, size_t len)
184 : mString(allocFromUTF32(o, len))
185{
186}
187
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800188String8::~String8()
189{
190 SharedBuffer::bufferFromData(mString)->release();
191}
192
Sergio Girod2529f22015-09-23 16:22:59 +0100193size_t String8::length() const
194{
195 return SharedBuffer::sizeFromData(mString)-1;
196}
197
Jeff Brown1d618d62010-12-02 13:50:46 -0800198String8 String8::format(const char* fmt, ...)
199{
200 va_list args;
201 va_start(args, fmt);
202
203 String8 result(formatV(fmt, args));
204
205 va_end(args);
206 return result;
207}
208
209String8 String8::formatV(const char* fmt, va_list args)
210{
211 String8 result;
212 result.appendFormatV(fmt, args);
213 return result;
214}
215
Jeff Brown48da31b2010-09-12 17:55:08 -0700216void String8::clear() {
217 SharedBuffer::bufferFromData(mString)->release();
218 mString = getEmptyString();
219}
220
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800221void String8::setTo(const String8& other)
222{
223 SharedBuffer::bufferFromData(other.mString)->acquire();
224 SharedBuffer::bufferFromData(mString)->release();
225 mString = other.mString;
226}
227
228status_t String8::setTo(const char* other)
229{
Andreas Huber10e5da52010-06-10 11:14:26 -0700230 const char *newString = allocFromUTF8(other, strlen(other));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800231 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700232 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700233 if (mString) return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800234
235 mString = getEmptyString();
236 return NO_MEMORY;
237}
238
239status_t String8::setTo(const char* other, size_t len)
240{
Andreas Huber10e5da52010-06-10 11:14:26 -0700241 const char *newString = allocFromUTF8(other, len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800242 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700243 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700244 if (mString) return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800245
246 mString = getEmptyString();
247 return NO_MEMORY;
248}
249
250status_t String8::setTo(const char16_t* other, size_t len)
251{
Andreas Huber10e5da52010-06-10 11:14:26 -0700252 const char *newString = allocFromUTF16(other, len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800253 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700254 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700255 if (mString) return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800256
257 mString = getEmptyString();
258 return NO_MEMORY;
259}
260
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900261status_t String8::setTo(const char32_t* other, size_t len)
262{
Andreas Huber10e5da52010-06-10 11:14:26 -0700263 const char *newString = allocFromUTF32(other, len);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900264 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700265 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700266 if (mString) return OK;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900267
268 mString = getEmptyString();
269 return NO_MEMORY;
270}
271
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800272status_t String8::append(const String8& other)
273{
274 const size_t otherLen = other.bytes();
275 if (bytes() == 0) {
276 setTo(other);
Elliott Hughes643268f2018-10-08 11:10:11 -0700277 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800278 } else if (otherLen == 0) {
Elliott Hughes643268f2018-10-08 11:10:11 -0700279 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800280 }
281
282 return real_append(other.string(), otherLen);
283}
284
285status_t String8::append(const char* other)
286{
287 return append(other, strlen(other));
288}
289
290status_t String8::append(const char* other, size_t otherLen)
291{
292 if (bytes() == 0) {
293 return setTo(other, otherLen);
294 } else if (otherLen == 0) {
Elliott Hughes643268f2018-10-08 11:10:11 -0700295 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800296 }
297
298 return real_append(other, otherLen);
299}
300
Jeff Brown35a154e2010-07-15 23:54:05 -0700301status_t String8::appendFormat(const char* fmt, ...)
302{
Jeff Brown647925d2010-11-10 16:03:06 -0800303 va_list args;
304 va_start(args, fmt);
Jeff Brown35a154e2010-07-15 23:54:05 -0700305
Jeff Brown647925d2010-11-10 16:03:06 -0800306 status_t result = appendFormatV(fmt, args);
307
308 va_end(args);
309 return result;
310}
311
312status_t String8::appendFormatV(const char* fmt, va_list args)
313{
Elliott Hughes643268f2018-10-08 11:10:11 -0700314 int n, result = OK;
Fengwei Yinfff9d112014-02-27 01:17:09 +0800315 va_list tmp_args;
316
317 /* args is undefined after vsnprintf.
318 * So we need a copy here to avoid the
319 * second vsnprintf access undefined args.
320 */
321 va_copy(tmp_args, args);
Yi Konge1731a42018-07-16 18:11:34 -0700322 n = vsnprintf(nullptr, 0, fmt, tmp_args);
Fengwei Yinfff9d112014-02-27 01:17:09 +0800323 va_end(tmp_args);
324
Steven Morelandbad50ed2020-07-28 21:41:54 +0000325 if (n < 0) return UNKNOWN_ERROR;
326
327 if (n > 0) {
Jeff Brown35a154e2010-07-15 23:54:05 -0700328 size_t oldLength = length();
Steven Morelandbad50ed2020-07-28 21:41:54 +0000329 if ((size_t)n > SIZE_MAX - 1 ||
330 oldLength > SIZE_MAX - (size_t)n - 1) {
331 return NO_MEMORY;
332 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700333 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();
Samuel Tan95fd5272016-02-18 16:56:21 -0800346
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800347 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';
Elliott Hughes643268f2018-10-08 11:10:11 -0700355 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800356 }
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 }
Yi Konge1731a42018-07-16 18:11:34 -0700369 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800370}
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
Elliott Hughes643268f2018-10-08 11:10:11 -0700391 return OK;
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
Jeff Brown5ee915a2014-06-06 19:30:15 -0700405bool String8::removeAll(const char* other) {
406 ssize_t index = find(other);
407 if (index < 0) return false;
408
409 char* buf = lockBuffer(size());
410 if (!buf) return false; // out of memory
411
412 size_t skip = strlen(other);
413 size_t len = size();
414 size_t tail = index;
415 while (size_t(index) < len) {
416 ssize_t next = find(other, index + skip);
417 if (next < 0) {
418 next = len;
419 }
420
Andreas Gampedd060f02014-11-13 15:50:17 -0800421 memmove(buf + tail, buf + index + skip, next - index - skip);
Jeff Brown5ee915a2014-06-06 19:30:15 -0700422 tail += next - index - skip;
423 index = next;
424 }
425 unlockBuffer(tail);
426 return true;
427}
428
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800429void String8::toLower()
430{
431 toLower(0, size());
432}
433
434void String8::toLower(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 = tolower(*buf);
447 buf++;
448 length--;
449 }
450 unlockBuffer(len);
451}
452
453void String8::toUpper()
454{
455 toUpper(0, size());
456}
457
458void String8::toUpper(size_t start, size_t length)
459{
460 const size_t len = size();
461 if (start >= len) {
462 return;
463 }
464 if (start+length > len) {
465 length = len-start;
466 }
467 char* buf = lockBuffer(len);
468 buf += start;
469 while (length > 0) {
470 *buf = toupper(*buf);
471 buf++;
472 length--;
473 }
474 unlockBuffer(len);
475}
476
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800477// ---------------------------------------------------------------------------
478// Path functions
479
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800480void String8::setPathName(const char* name)
481{
482 setPathName(name, strlen(name));
483}
484
485void String8::setPathName(const char* name, size_t len)
486{
487 char* buf = lockBuffer(len);
488
489 memcpy(buf, name, len);
490
491 // remove trailing path separator, if present
492 if (len > 0 && buf[len-1] == OS_PATH_SEPARATOR)
493 len--;
494
495 buf[len] = '\0';
496
497 unlockBuffer(len);
498}
499
500String8 String8::getPathLeaf(void) const
501{
502 const char* cp;
503 const char*const buf = mString;
504
505 cp = strrchr(buf, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700506 if (cp == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800507 return String8(*this);
508 else
509 return String8(cp+1);
510}
511
512String8 String8::getPathDir(void) const
513{
514 const char* cp;
515 const char*const str = mString;
516
517 cp = strrchr(str, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700518 if (cp == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800519 return String8("");
520 else
521 return String8(str, cp - str);
522}
523
524String8 String8::walkPath(String8* outRemains) const
525{
526 const char* cp;
527 const char*const str = mString;
528 const char* buf = str;
529
530 cp = strchr(buf, OS_PATH_SEPARATOR);
531 if (cp == buf) {
532 // don't include a leading '/'.
533 buf = buf+1;
534 cp = strchr(buf, OS_PATH_SEPARATOR);
535 }
536
Yi Konge1731a42018-07-16 18:11:34 -0700537 if (cp == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800538 String8 res = buf != str ? String8(buf) : *this;
539 if (outRemains) *outRemains = String8("");
540 return res;
541 }
542
543 String8 res(buf, cp-buf);
544 if (outRemains) *outRemains = String8(cp+1);
545 return res;
546}
547
548/*
549 * Helper function for finding the start of an extension in a pathname.
550 *
551 * Returns a pointer inside mString, or NULL if no extension was found.
552 */
553char* String8::find_extension(void) const
554{
555 const char* lastSlash;
556 const char* lastDot;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800557 const char* const str = mString;
558
559 // only look at the filename
560 lastSlash = strrchr(str, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700561 if (lastSlash == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800562 lastSlash = str;
563 else
564 lastSlash++;
565
566 // find the last dot
567 lastDot = strrchr(lastSlash, '.');
Yi Konge1731a42018-07-16 18:11:34 -0700568 if (lastDot == nullptr)
569 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800570
571 // looks good, ship it
572 return const_cast<char*>(lastDot);
573}
574
575String8 String8::getPathExtension(void) const
576{
577 char* ext;
578
579 ext = find_extension();
Yi Konge1731a42018-07-16 18:11:34 -0700580 if (ext != nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800581 return String8(ext);
582 else
583 return String8("");
584}
585
586String8 String8::getBasePath(void) const
587{
588 char* ext;
589 const char* const str = mString;
590
591 ext = find_extension();
Yi Konge1731a42018-07-16 18:11:34 -0700592 if (ext == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800593 return String8(*this);
594 else
595 return String8(str, ext - str);
596}
597
598String8& String8::appendPath(const char* name)
599{
600 // TODO: The test below will fail for Win32 paths. Fix later or ignore.
601 if (name[0] != OS_PATH_SEPARATOR) {
602 if (*name == '\0') {
603 // nothing to do
604 return *this;
605 }
606
607 size_t len = length();
608 if (len == 0) {
609 // no existing filename, just use the new one
610 setPathName(name);
611 return *this;
612 }
613
614 // make room for oldPath + '/' + newPath
615 int newlen = strlen(name);
616
617 char* buf = lockBuffer(len+1+newlen);
618
619 // insert a '/' if needed
620 if (buf[len-1] != OS_PATH_SEPARATOR)
621 buf[len++] = OS_PATH_SEPARATOR;
622
623 memcpy(buf+len, name, newlen+1);
624 len += newlen;
625
626 unlockBuffer(len);
627
628 return *this;
629 } else {
630 setPathName(name);
631 return *this;
632 }
633}
634
635String8& String8::convertToResPath()
636{
637#if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR
638 size_t len = length();
639 if (len > 0) {
640 char * buf = lockBuffer(len);
641 for (char * end = buf + len; buf < end; ++buf) {
642 if (*buf == OS_PATH_SEPARATOR)
643 *buf = RES_PATH_SEPARATOR;
644 }
645 unlockBuffer(len);
646 }
647#endif
648 return *this;
649}
650
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800651}; // namespace android