blob: fad130b1b052c3f9693c128e1803321f72337c1d [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
128String8::String8(const String8& o)
129 : mString(o.mString)
130{
131 SharedBuffer::bufferFromData(mString)->acquire();
132}
133
134String8::String8(const char* o)
135 : mString(allocFromUTF8(o, strlen(o)))
136{
Yi Konge1731a42018-07-16 18:11:34 -0700137 if (mString == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800138 mString = getEmptyString();
139 }
140}
141
142String8::String8(const char* o, size_t len)
143 : mString(allocFromUTF8(o, len))
144{
Yi Konge1731a42018-07-16 18:11:34 -0700145 if (mString == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800146 mString = getEmptyString();
147 }
148}
149
150String8::String8(const String16& o)
151 : mString(allocFromUTF16(o.string(), o.size()))
152{
153}
154
155String8::String8(const char16_t* o)
156 : mString(allocFromUTF16(o, strlen16(o)))
157{
158}
159
160String8::String8(const char16_t* o, size_t len)
161 : mString(allocFromUTF16(o, len))
162{
163}
164
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900165String8::String8(const char32_t* o)
166 : mString(allocFromUTF32(o, strlen32(o)))
167{
168}
169
170String8::String8(const char32_t* o, size_t len)
171 : mString(allocFromUTF32(o, len))
172{
173}
174
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800175String8::~String8()
176{
177 SharedBuffer::bufferFromData(mString)->release();
178}
179
Sergio Girod2529f22015-09-23 16:22:59 +0100180size_t String8::length() const
181{
182 return SharedBuffer::sizeFromData(mString)-1;
183}
184
Jeff Brown1d618d62010-12-02 13:50:46 -0800185String8 String8::format(const char* fmt, ...)
186{
187 va_list args;
188 va_start(args, fmt);
189
190 String8 result(formatV(fmt, args));
191
192 va_end(args);
193 return result;
194}
195
196String8 String8::formatV(const char* fmt, va_list args)
197{
198 String8 result;
199 result.appendFormatV(fmt, args);
200 return result;
201}
202
Jeff Brown48da31b2010-09-12 17:55:08 -0700203void String8::clear() {
204 SharedBuffer::bufferFromData(mString)->release();
205 mString = getEmptyString();
206}
207
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800208void String8::setTo(const String8& other)
209{
210 SharedBuffer::bufferFromData(other.mString)->acquire();
211 SharedBuffer::bufferFromData(mString)->release();
212 mString = other.mString;
213}
214
215status_t String8::setTo(const char* other)
216{
Andreas Huber10e5da52010-06-10 11:14:26 -0700217 const char *newString = allocFromUTF8(other, strlen(other));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800218 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700219 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700220 if (mString) return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800221
222 mString = getEmptyString();
223 return NO_MEMORY;
224}
225
226status_t String8::setTo(const char* other, size_t len)
227{
Andreas Huber10e5da52010-06-10 11:14:26 -0700228 const char *newString = allocFromUTF8(other, len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800229 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700230 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700231 if (mString) return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800232
233 mString = getEmptyString();
234 return NO_MEMORY;
235}
236
237status_t String8::setTo(const char16_t* other, size_t len)
238{
Andreas Huber10e5da52010-06-10 11:14:26 -0700239 const char *newString = allocFromUTF16(other, len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800240 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700241 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700242 if (mString) return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800243
244 mString = getEmptyString();
245 return NO_MEMORY;
246}
247
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900248status_t String8::setTo(const char32_t* other, size_t len)
249{
Andreas Huber10e5da52010-06-10 11:14:26 -0700250 const char *newString = allocFromUTF32(other, len);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900251 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700252 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700253 if (mString) return OK;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900254
255 mString = getEmptyString();
256 return NO_MEMORY;
257}
258
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800259status_t String8::append(const String8& other)
260{
261 const size_t otherLen = other.bytes();
262 if (bytes() == 0) {
263 setTo(other);
Elliott Hughes643268f2018-10-08 11:10:11 -0700264 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800265 } else if (otherLen == 0) {
Elliott Hughes643268f2018-10-08 11:10:11 -0700266 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800267 }
268
269 return real_append(other.string(), otherLen);
270}
271
272status_t String8::append(const char* other)
273{
274 return append(other, strlen(other));
275}
276
277status_t String8::append(const char* other, size_t otherLen)
278{
279 if (bytes() == 0) {
280 return setTo(other, otherLen);
281 } else if (otherLen == 0) {
Elliott Hughes643268f2018-10-08 11:10:11 -0700282 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800283 }
284
285 return real_append(other, otherLen);
286}
287
Jeff Brown35a154e2010-07-15 23:54:05 -0700288status_t String8::appendFormat(const char* fmt, ...)
289{
Jeff Brown647925d2010-11-10 16:03:06 -0800290 va_list args;
291 va_start(args, fmt);
Jeff Brown35a154e2010-07-15 23:54:05 -0700292
Jeff Brown647925d2010-11-10 16:03:06 -0800293 status_t result = appendFormatV(fmt, args);
294
295 va_end(args);
296 return result;
297}
298
299status_t String8::appendFormatV(const char* fmt, va_list args)
300{
Elliott Hughes643268f2018-10-08 11:10:11 -0700301 int n, result = OK;
Fengwei Yinfff9d112014-02-27 01:17:09 +0800302 va_list tmp_args;
303
304 /* args is undefined after vsnprintf.
305 * So we need a copy here to avoid the
306 * second vsnprintf access undefined args.
307 */
308 va_copy(tmp_args, args);
Yi Konge1731a42018-07-16 18:11:34 -0700309 n = vsnprintf(nullptr, 0, fmt, tmp_args);
Fengwei Yinfff9d112014-02-27 01:17:09 +0800310 va_end(tmp_args);
311
Steven Morelandbad50ed2020-07-28 21:41:54 +0000312 if (n < 0) return UNKNOWN_ERROR;
313
314 if (n > 0) {
Jeff Brown35a154e2010-07-15 23:54:05 -0700315 size_t oldLength = length();
Steven Morelandbad50ed2020-07-28 21:41:54 +0000316 if ((size_t)n > SIZE_MAX - 1 ||
317 oldLength > SIZE_MAX - (size_t)n - 1) {
318 return NO_MEMORY;
319 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700320 char* buf = lockBuffer(oldLength + n);
321 if (buf) {
Jeff Brown647925d2010-11-10 16:03:06 -0800322 vsnprintf(buf + oldLength, n + 1, fmt, args);
Jeff Brown35a154e2010-07-15 23:54:05 -0700323 } else {
324 result = NO_MEMORY;
325 }
326 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700327 return result;
328}
329
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800330status_t String8::real_append(const char* other, size_t otherLen)
331{
332 const size_t myLen = bytes();
Samuel Tan95fd5272016-02-18 16:56:21 -0800333
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800334 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
335 ->editResize(myLen+otherLen+1);
336 if (buf) {
337 char* str = (char*)buf->data();
338 mString = str;
339 str += myLen;
340 memcpy(str, other, otherLen);
341 str[otherLen] = '\0';
Elliott Hughes643268f2018-10-08 11:10:11 -0700342 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800343 }
344 return NO_MEMORY;
345}
346
347char* String8::lockBuffer(size_t size)
348{
349 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
350 ->editResize(size+1);
351 if (buf) {
352 char* str = (char*)buf->data();
353 mString = str;
354 return str;
355 }
Yi Konge1731a42018-07-16 18:11:34 -0700356 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800357}
358
359void String8::unlockBuffer()
360{
361 unlockBuffer(strlen(mString));
362}
363
364status_t String8::unlockBuffer(size_t size)
365{
366 if (size != this->size()) {
367 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
368 ->editResize(size+1);
Jeff Brown35a154e2010-07-15 23:54:05 -0700369 if (! buf) {
370 return NO_MEMORY;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800371 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700372
373 char* str = (char*)buf->data();
374 str[size] = 0;
375 mString = str;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800376 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700377
Elliott Hughes643268f2018-10-08 11:10:11 -0700378 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800379}
380
381ssize_t String8::find(const char* other, size_t start) const
382{
383 size_t len = size();
384 if (start >= len) {
385 return -1;
386 }
387 const char* s = mString+start;
388 const char* p = strstr(s, other);
389 return p ? p-mString : -1;
390}
391
Jeff Brown5ee915a2014-06-06 19:30:15 -0700392bool String8::removeAll(const char* other) {
393 ssize_t index = find(other);
394 if (index < 0) return false;
395
396 char* buf = lockBuffer(size());
397 if (!buf) return false; // out of memory
398
399 size_t skip = strlen(other);
400 size_t len = size();
401 size_t tail = index;
402 while (size_t(index) < len) {
403 ssize_t next = find(other, index + skip);
404 if (next < 0) {
405 next = len;
406 }
407
Andreas Gampedd060f02014-11-13 15:50:17 -0800408 memmove(buf + tail, buf + index + skip, next - index - skip);
Jeff Brown5ee915a2014-06-06 19:30:15 -0700409 tail += next - index - skip;
410 index = next;
411 }
412 unlockBuffer(tail);
413 return true;
414}
415
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800416void String8::toLower()
417{
Elliott Hughesa8583952021-04-08 13:26:49 -0700418 const size_t length = size();
419 if (length == 0) return;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800420
Elliott Hughesa8583952021-04-08 13:26:49 -0700421 char* buf = lockBuffer(length);
422 for (size_t i = length; i > 0; --i) {
Steven Morelandfdbc5652020-07-13 23:31:45 +0000423 *buf = static_cast<char>(tolower(*buf));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800424 buf++;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800425 }
Elliott Hughesa8583952021-04-08 13:26:49 -0700426 unlockBuffer(length);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800427}
428
429void String8::toUpper()
430{
Elliott Hughesa8583952021-04-08 13:26:49 -0700431 const size_t length = size();
432 if (length == 0) return;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800433
Elliott Hughesa8583952021-04-08 13:26:49 -0700434 char* buf = lockBuffer(length);
435 for (size_t i = length; i > 0; --i) {
Steven Morelandfdbc5652020-07-13 23:31:45 +0000436 *buf = static_cast<char>(toupper(*buf));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800437 buf++;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800438 }
Elliott Hughesa8583952021-04-08 13:26:49 -0700439 unlockBuffer(length);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800440}
441
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800442// ---------------------------------------------------------------------------
443// Path functions
444
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800445void String8::setPathName(const char* name)
446{
447 setPathName(name, strlen(name));
448}
449
450void String8::setPathName(const char* name, size_t len)
451{
452 char* buf = lockBuffer(len);
453
454 memcpy(buf, name, len);
455
456 // remove trailing path separator, if present
457 if (len > 0 && buf[len-1] == OS_PATH_SEPARATOR)
458 len--;
459
460 buf[len] = '\0';
461
462 unlockBuffer(len);
463}
464
465String8 String8::getPathLeaf(void) const
466{
467 const char* cp;
468 const char*const buf = mString;
469
470 cp = strrchr(buf, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700471 if (cp == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800472 return String8(*this);
473 else
474 return String8(cp+1);
475}
476
477String8 String8::getPathDir(void) const
478{
479 const char* cp;
480 const char*const str = mString;
481
482 cp = strrchr(str, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700483 if (cp == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800484 return String8("");
485 else
486 return String8(str, cp - str);
487}
488
489String8 String8::walkPath(String8* outRemains) const
490{
491 const char* cp;
492 const char*const str = mString;
493 const char* buf = str;
494
495 cp = strchr(buf, OS_PATH_SEPARATOR);
496 if (cp == buf) {
497 // don't include a leading '/'.
498 buf = buf+1;
499 cp = strchr(buf, OS_PATH_SEPARATOR);
500 }
501
Yi Konge1731a42018-07-16 18:11:34 -0700502 if (cp == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800503 String8 res = buf != str ? String8(buf) : *this;
504 if (outRemains) *outRemains = String8("");
505 return res;
506 }
507
508 String8 res(buf, cp-buf);
509 if (outRemains) *outRemains = String8(cp+1);
510 return res;
511}
512
513/*
514 * Helper function for finding the start of an extension in a pathname.
515 *
516 * Returns a pointer inside mString, or NULL if no extension was found.
517 */
518char* String8::find_extension(void) const
519{
520 const char* lastSlash;
521 const char* lastDot;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800522 const char* const str = mString;
523
524 // only look at the filename
525 lastSlash = strrchr(str, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700526 if (lastSlash == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800527 lastSlash = str;
528 else
529 lastSlash++;
530
531 // find the last dot
532 lastDot = strrchr(lastSlash, '.');
Yi Konge1731a42018-07-16 18:11:34 -0700533 if (lastDot == nullptr)
534 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800535
536 // looks good, ship it
537 return const_cast<char*>(lastDot);
538}
539
540String8 String8::getPathExtension(void) const
541{
542 char* ext;
543
544 ext = find_extension();
Yi Konge1731a42018-07-16 18:11:34 -0700545 if (ext != nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800546 return String8(ext);
547 else
548 return String8("");
549}
550
551String8 String8::getBasePath(void) const
552{
553 char* ext;
554 const char* const str = mString;
555
556 ext = find_extension();
Yi Konge1731a42018-07-16 18:11:34 -0700557 if (ext == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800558 return String8(*this);
559 else
560 return String8(str, ext - str);
561}
562
563String8& String8::appendPath(const char* name)
564{
565 // TODO: The test below will fail for Win32 paths. Fix later or ignore.
566 if (name[0] != OS_PATH_SEPARATOR) {
567 if (*name == '\0') {
568 // nothing to do
569 return *this;
570 }
571
572 size_t len = length();
573 if (len == 0) {
574 // no existing filename, just use the new one
575 setPathName(name);
576 return *this;
577 }
578
579 // make room for oldPath + '/' + newPath
580 int newlen = strlen(name);
581
582 char* buf = lockBuffer(len+1+newlen);
583
584 // insert a '/' if needed
585 if (buf[len-1] != OS_PATH_SEPARATOR)
586 buf[len++] = OS_PATH_SEPARATOR;
587
588 memcpy(buf+len, name, newlen+1);
589 len += newlen;
590
591 unlockBuffer(len);
592
593 return *this;
594 } else {
595 setPathName(name);
596 return *this;
597 }
598}
599
600String8& String8::convertToResPath()
601{
602#if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR
603 size_t len = length();
604 if (len > 0) {
605 char * buf = lockBuffer(len);
606 for (char * end = buf + len; buf < end; ++buf) {
607 if (*buf == OS_PATH_SEPARATOR)
608 *buf = RES_PATH_SEPARATOR;
609 }
610 unlockBuffer(len);
611 }
612#endif
613 return *this;
614}
615
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800616}; // namespace android