blob: 369038901d47796c7c346bb7008d0d932f4e34ad [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
Elliott Hughes4b7b4d62021-04-15 15:18:54 -070028#include <string>
29
Sergio Girod2529f22015-09-23 16:22:59 +010030#include "SharedBuffer.h"
31
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090032/*
33 * Functions outside android is below the namespace android, since they use
34 * functions and constants in android namespace.
35 */
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080036
37// ---------------------------------------------------------------------------
38
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090039namespace android {
40
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080041// Separator used by resource paths. This is not platform dependent contrary
42// to OS_PATH_SEPARATOR.
43#define RES_PATH_SEPARATOR '/'
44
Steven Moreland241b93c2018-03-06 09:11:29 -080045static inline char* getEmptyString() {
46 static SharedBuffer* gEmptyStringBuf = [] {
47 SharedBuffer* buf = SharedBuffer::alloc(1);
48 char* str = static_cast<char*>(buf->data());
49 *str = 0;
50 return buf;
51 }();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080052
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080053 gEmptyStringBuf->acquire();
Steven Moreland241b93c2018-03-06 09:11:29 -080054 return static_cast<char*>(gEmptyStringBuf->data());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080055}
56
57// ---------------------------------------------------------------------------
58
59static char* allocFromUTF8(const char* in, size_t len)
60{
61 if (len > 0) {
Sergio Giroebabef22015-08-18 14:44:54 +010062 if (len == SIZE_MAX) {
Yi Konge1731a42018-07-16 18:11:34 -070063 return nullptr;
Sergio Giroebabef22015-08-18 14:44:54 +010064 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080065 SharedBuffer* buf = SharedBuffer::alloc(len+1);
Steve Blockae074452012-01-09 18:35:44 +000066 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080067 if (buf) {
68 char* str = (char*)buf->data();
69 memcpy(str, in, len);
70 str[len] = 0;
71 return str;
72 }
Yi Konge1731a42018-07-16 18:11:34 -070073 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080074 }
75
76 return getEmptyString();
77}
78
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090079static char* allocFromUTF16(const char16_t* in, size_t len)
80{
Kenny Root9a2d83e2009-12-04 09:38:48 -080081 if (len == 0) return getEmptyString();
82
Sergio Giroc4966a32016-06-28 18:02:29 +010083 // Allow for closing '\0'
84 const ssize_t resultStrLen = utf16_to_utf8_length(in, len) + 1;
85 if (resultStrLen < 1) {
Kenny Rootba0165b2010-11-09 14:37:23 -080086 return getEmptyString();
87 }
Kenny Root9a2d83e2009-12-04 09:38:48 -080088
Sergio Giroc4966a32016-06-28 18:02:29 +010089 SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
Steve Blockae074452012-01-09 18:35:44 +000090 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
Kenny Rootba0165b2010-11-09 14:37:23 -080091 if (!buf) {
92 return getEmptyString();
Kenny Root9a2d83e2009-12-04 09:38:48 -080093 }
94
Sergio Giroc4966a32016-06-28 18:02:29 +010095 char* resultStr = (char*)buf->data();
96 utf16_to_utf8(in, len, resultStr, resultStrLen);
97 return resultStr;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090098}
99
100static char* allocFromUTF32(const char32_t* in, size_t len)
101{
Kenny Rootba0165b2010-11-09 14:37:23 -0800102 if (len == 0) {
103 return getEmptyString();
104 }
105
Sergio Giroc4966a32016-06-28 18:02:29 +0100106 const ssize_t resultStrLen = utf32_to_utf8_length(in, len) + 1;
107 if (resultStrLen < 1) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800108 return getEmptyString();
109 }
110
Sergio Giroc4966a32016-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();
115 }
116
Sergio Giroc4966a32016-06-28 18:02:29 +0100117 char* resultStr = (char*) buf->data();
118 utf32_to_utf8(in, len, resultStr, resultStrLen);
Kenny Rootba0165b2010-11-09 14:37:23 -0800119
Sergio Giroc4966a32016-06-28 18:02:29 +0100120 return resultStr;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900121}
122
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800123// ---------------------------------------------------------------------------
124
125String8::String8()
126 : mString(getEmptyString())
127{
128}
129
130String8::String8(const String8& o)
131 : mString(o.mString)
132{
133 SharedBuffer::bufferFromData(mString)->acquire();
134}
135
136String8::String8(const char* o)
137 : mString(allocFromUTF8(o, strlen(o)))
138{
Yi Konge1731a42018-07-16 18:11:34 -0700139 if (mString == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800140 mString = getEmptyString();
141 }
142}
143
144String8::String8(const char* o, size_t len)
145 : mString(allocFromUTF8(o, len))
146{
Yi Konge1731a42018-07-16 18:11:34 -0700147 if (mString == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800148 mString = getEmptyString();
149 }
150}
151
152String8::String8(const String16& o)
153 : mString(allocFromUTF16(o.string(), o.size()))
154{
155}
156
157String8::String8(const char16_t* o)
158 : mString(allocFromUTF16(o, strlen16(o)))
159{
160}
161
162String8::String8(const char16_t* o, size_t len)
163 : mString(allocFromUTF16(o, len))
164{
165}
166
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900167String8::String8(const char32_t* o)
Elliott Hughes4b7b4d62021-04-15 15:18:54 -0700168 : mString(allocFromUTF32(o, std::char_traits<char32_t>::length(o))) {}
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900169
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 Moreland0f0cb952020-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();
Andrei Homescu1a867dc2022-03-29 00:30:34 +0000316 if (static_cast<size_t>(n) > std::numeric_limits<size_t>::max() - 1 ||
Steven Moreland0f0cb952020-07-28 21:41:54 +0000317 oldLength > std::numeric_limits<size_t>::max() - 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
Elliott Hughes59682762021-06-10 16:42:20 -0700330status_t String8::real_append(const char* other, size_t otherLen) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800331 const size_t myLen = bytes();
Samuel Tan95fd5272016-02-18 16:56:21 -0800332
Elliott Hughes59682762021-06-10 16:42:20 -0700333 SharedBuffer* buf;
334 size_t newLen;
335 if (__builtin_add_overflow(myLen, otherLen, &newLen) ||
336 __builtin_add_overflow(newLen, 1, &newLen) ||
337 (buf = SharedBuffer::bufferFromData(mString)->editResize(newLen)) == nullptr) {
338 return NO_MEMORY;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800339 }
Elliott Hughes59682762021-06-10 16:42:20 -0700340
341 char* str = (char*)buf->data();
342 mString = str;
343 str += myLen;
344 memcpy(str, other, otherLen);
345 str[otherLen] = '\0';
346 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800347}
348
349char* String8::lockBuffer(size_t size)
350{
351 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
352 ->editResize(size+1);
353 if (buf) {
354 char* str = (char*)buf->data();
355 mString = str;
356 return str;
357 }
Yi Konge1731a42018-07-16 18:11:34 -0700358 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800359}
360
361void String8::unlockBuffer()
362{
363 unlockBuffer(strlen(mString));
364}
365
366status_t String8::unlockBuffer(size_t size)
367{
368 if (size != this->size()) {
369 SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
370 ->editResize(size+1);
Jeff Brown35a154e2010-07-15 23:54:05 -0700371 if (! buf) {
372 return NO_MEMORY;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800373 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700374
375 char* str = (char*)buf->data();
376 str[size] = 0;
377 mString = str;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800378 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700379
Elliott Hughes643268f2018-10-08 11:10:11 -0700380 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800381}
382
383ssize_t String8::find(const char* other, size_t start) const
384{
385 size_t len = size();
386 if (start >= len) {
387 return -1;
388 }
389 const char* s = mString+start;
390 const char* p = strstr(s, other);
391 return p ? p-mString : -1;
392}
393
Jeff Brown5ee915a2014-06-06 19:30:15 -0700394bool String8::removeAll(const char* other) {
395 ssize_t index = find(other);
396 if (index < 0) return false;
397
398 char* buf = lockBuffer(size());
399 if (!buf) return false; // out of memory
400
401 size_t skip = strlen(other);
402 size_t len = size();
403 size_t tail = index;
404 while (size_t(index) < len) {
405 ssize_t next = find(other, index + skip);
406 if (next < 0) {
407 next = len;
408 }
409
Andreas Gampedd060f02014-11-13 15:50:17 -0800410 memmove(buf + tail, buf + index + skip, next - index - skip);
Jeff Brown5ee915a2014-06-06 19:30:15 -0700411 tail += next - index - skip;
412 index = next;
413 }
414 unlockBuffer(tail);
415 return true;
416}
417
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800418void String8::toLower()
419{
Elliott Hughesa8583952021-04-08 13:26:49 -0700420 const size_t length = size();
421 if (length == 0) return;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800422
Elliott Hughesa8583952021-04-08 13:26:49 -0700423 char* buf = lockBuffer(length);
424 for (size_t i = length; i > 0; --i) {
Steven Morelandfdbc5652020-07-13 23:31:45 +0000425 *buf = static_cast<char>(tolower(*buf));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800426 buf++;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800427 }
Elliott Hughesa8583952021-04-08 13:26:49 -0700428 unlockBuffer(length);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800429}
430
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800431// ---------------------------------------------------------------------------
432// Path functions
433
Greg Kaiserd03851e2021-07-19 20:19:44 +0000434static void setPathName(String8& s, const char* name) {
435 size_t len = strlen(name);
436 char* buf = s.lockBuffer(len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800437
438 memcpy(buf, name, len);
439
440 // remove trailing path separator, if present
Greg Kaiserd03851e2021-07-19 20:19:44 +0000441 if (len > 0 && buf[len - 1] == OS_PATH_SEPARATOR) len--;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800442 buf[len] = '\0';
443
Greg Kaiserd03851e2021-07-19 20:19:44 +0000444 s.unlockBuffer(len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800445}
446
447String8 String8::getPathLeaf(void) const
448{
449 const char* cp;
450 const char*const buf = mString;
451
452 cp = strrchr(buf, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700453 if (cp == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800454 return String8(*this);
455 else
456 return String8(cp+1);
457}
458
459String8 String8::getPathDir(void) const
460{
461 const char* cp;
462 const char*const str = mString;
463
464 cp = strrchr(str, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700465 if (cp == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800466 return String8("");
467 else
468 return String8(str, cp - str);
469}
470
471String8 String8::walkPath(String8* outRemains) const
472{
473 const char* cp;
474 const char*const str = mString;
475 const char* buf = str;
476
477 cp = strchr(buf, OS_PATH_SEPARATOR);
478 if (cp == buf) {
479 // don't include a leading '/'.
480 buf = buf+1;
481 cp = strchr(buf, OS_PATH_SEPARATOR);
482 }
483
Yi Konge1731a42018-07-16 18:11:34 -0700484 if (cp == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800485 String8 res = buf != str ? String8(buf) : *this;
486 if (outRemains) *outRemains = String8("");
487 return res;
488 }
489
490 String8 res(buf, cp-buf);
491 if (outRemains) *outRemains = String8(cp+1);
492 return res;
493}
494
495/*
496 * Helper function for finding the start of an extension in a pathname.
497 *
498 * Returns a pointer inside mString, or NULL if no extension was found.
499 */
500char* String8::find_extension(void) const
501{
502 const char* lastSlash;
503 const char* lastDot;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800504 const char* const str = mString;
505
506 // only look at the filename
507 lastSlash = strrchr(str, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700508 if (lastSlash == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800509 lastSlash = str;
510 else
511 lastSlash++;
512
513 // find the last dot
514 lastDot = strrchr(lastSlash, '.');
Yi Konge1731a42018-07-16 18:11:34 -0700515 if (lastDot == nullptr)
516 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800517
518 // looks good, ship it
519 return const_cast<char*>(lastDot);
520}
521
522String8 String8::getPathExtension(void) const
523{
524 char* ext;
525
526 ext = find_extension();
Yi Konge1731a42018-07-16 18:11:34 -0700527 if (ext != nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800528 return String8(ext);
529 else
530 return String8("");
531}
532
533String8 String8::getBasePath(void) const
534{
535 char* ext;
536 const char* const str = mString;
537
538 ext = find_extension();
Yi Konge1731a42018-07-16 18:11:34 -0700539 if (ext == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800540 return String8(*this);
541 else
542 return String8(str, ext - str);
543}
544
545String8& String8::appendPath(const char* name)
546{
547 // TODO: The test below will fail for Win32 paths. Fix later or ignore.
548 if (name[0] != OS_PATH_SEPARATOR) {
549 if (*name == '\0') {
550 // nothing to do
551 return *this;
552 }
553
554 size_t len = length();
555 if (len == 0) {
556 // no existing filename, just use the new one
Greg Kaiserd03851e2021-07-19 20:19:44 +0000557 setPathName(*this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800558 return *this;
559 }
560
561 // make room for oldPath + '/' + newPath
562 int newlen = strlen(name);
563
564 char* buf = lockBuffer(len+1+newlen);
565
566 // insert a '/' if needed
567 if (buf[len-1] != OS_PATH_SEPARATOR)
568 buf[len++] = OS_PATH_SEPARATOR;
569
570 memcpy(buf+len, name, newlen+1);
571 len += newlen;
572
573 unlockBuffer(len);
574
575 return *this;
576 } else {
Greg Kaiserd03851e2021-07-19 20:19:44 +0000577 setPathName(*this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800578 return *this;
579 }
580}
581
582String8& String8::convertToResPath()
583{
584#if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR
585 size_t len = length();
586 if (len > 0) {
587 char * buf = lockBuffer(len);
588 for (char * end = buf + len; buf < end; ++buf) {
589 if (*buf == OS_PATH_SEPARATOR)
590 *buf = RES_PATH_SEPARATOR;
591 }
592 unlockBuffer(len);
593 }
594#endif
595 return *this;
596}
597
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800598}; // namespace android