blob: 79b7edfe9a12b0d8ded2a45df4a0ac04a59cb26f [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
Biswapriyo Nath9e25bf02022-10-21 10:26:01 +053028#include <limits>
Elliott Hughes4b7b4d62021-04-15 15:18:54 -070029#include <string>
30
Sergio Girod2529f22015-09-23 16:22:59 +010031#include "SharedBuffer.h"
32
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090033/*
34 * Functions outside android is below the namespace android, since they use
35 * functions and constants in android namespace.
36 */
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080037
38// ---------------------------------------------------------------------------
39
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090040namespace android {
41
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080042// Separator used by resource paths. This is not platform dependent contrary
43// to OS_PATH_SEPARATOR.
44#define RES_PATH_SEPARATOR '/'
45
Steven Moreland241b93c2018-03-06 09:11:29 -080046static inline char* getEmptyString() {
47 static SharedBuffer* gEmptyStringBuf = [] {
48 SharedBuffer* buf = SharedBuffer::alloc(1);
49 char* str = static_cast<char*>(buf->data());
50 *str = 0;
51 return buf;
52 }();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080053
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080054 gEmptyStringBuf->acquire();
Steven Moreland241b93c2018-03-06 09:11:29 -080055 return static_cast<char*>(gEmptyStringBuf->data());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080056}
57
58// ---------------------------------------------------------------------------
59
60static char* allocFromUTF8(const char* in, size_t len)
61{
62 if (len > 0) {
Sergio Giroebabef22015-08-18 14:44:54 +010063 if (len == SIZE_MAX) {
Yi Konge1731a42018-07-16 18:11:34 -070064 return nullptr;
Sergio Giroebabef22015-08-18 14:44:54 +010065 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080066 SharedBuffer* buf = SharedBuffer::alloc(len+1);
Steve Blockae074452012-01-09 18:35:44 +000067 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080068 if (buf) {
69 char* str = (char*)buf->data();
70 memcpy(str, in, len);
71 str[len] = 0;
72 return str;
73 }
Yi Konge1731a42018-07-16 18:11:34 -070074 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080075 }
76
77 return getEmptyString();
78}
79
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090080static char* allocFromUTF16(const char16_t* in, size_t len)
81{
Kenny Root9a2d83e2009-12-04 09:38:48 -080082 if (len == 0) return getEmptyString();
83
Sergio Giroc4966a32016-06-28 18:02:29 +010084 // Allow for closing '\0'
85 const ssize_t resultStrLen = utf16_to_utf8_length(in, len) + 1;
86 if (resultStrLen < 1) {
Kenny Rootba0165b2010-11-09 14:37:23 -080087 return getEmptyString();
88 }
Kenny Root9a2d83e2009-12-04 09:38:48 -080089
Sergio Giroc4966a32016-06-28 18:02:29 +010090 SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
Steve Blockae074452012-01-09 18:35:44 +000091 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
Kenny Rootba0165b2010-11-09 14:37:23 -080092 if (!buf) {
93 return getEmptyString();
Kenny Root9a2d83e2009-12-04 09:38:48 -080094 }
95
Sergio Giroc4966a32016-06-28 18:02:29 +010096 char* resultStr = (char*)buf->data();
97 utf16_to_utf8(in, len, resultStr, resultStrLen);
98 return resultStr;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +090099}
100
101static char* allocFromUTF32(const char32_t* in, size_t len)
102{
Kenny Rootba0165b2010-11-09 14:37:23 -0800103 if (len == 0) {
104 return getEmptyString();
105 }
106
Sergio Giroc4966a32016-06-28 18:02:29 +0100107 const ssize_t resultStrLen = utf32_to_utf8_length(in, len) + 1;
108 if (resultStrLen < 1) {
Kenny Rootba0165b2010-11-09 14:37:23 -0800109 return getEmptyString();
110 }
111
Sergio Giroc4966a32016-06-28 18:02:29 +0100112 SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
Steve Blockae074452012-01-09 18:35:44 +0000113 ALOG_ASSERT(buf, "Unable to allocate shared buffer");
Kenny Rootba0165b2010-11-09 14:37:23 -0800114 if (!buf) {
115 return getEmptyString();
116 }
117
Sergio Giroc4966a32016-06-28 18:02:29 +0100118 char* resultStr = (char*) buf->data();
119 utf32_to_utf8(in, len, resultStr, resultStrLen);
Kenny Rootba0165b2010-11-09 14:37:23 -0800120
Sergio Giroc4966a32016-06-28 18:02:29 +0100121 return resultStr;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900122}
123
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800124// ---------------------------------------------------------------------------
125
126String8::String8()
127 : mString(getEmptyString())
128{
129}
130
131String8::String8(const String8& o)
132 : mString(o.mString)
133{
134 SharedBuffer::bufferFromData(mString)->acquire();
135}
136
137String8::String8(const char* o)
138 : mString(allocFromUTF8(o, strlen(o)))
139{
Yi Konge1731a42018-07-16 18:11:34 -0700140 if (mString == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800141 mString = getEmptyString();
142 }
143}
144
145String8::String8(const char* o, size_t len)
146 : mString(allocFromUTF8(o, len))
147{
Yi Konge1731a42018-07-16 18:11:34 -0700148 if (mString == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800149 mString = getEmptyString();
150 }
151}
152
Tomasz Wasilczyk90af4152023-08-11 02:12:16 +0000153String8::String8(const String16& o) : mString(allocFromUTF16(o.c_str(), o.size())) {}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800154
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)
Elliott Hughes4b7b4d62021-04-15 15:18:54 -0700166 : mString(allocFromUTF32(o, std::char_traits<char32_t>::length(o))) {}
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900167
168String8::String8(const char32_t* o, size_t len)
169 : mString(allocFromUTF32(o, len))
170{
171}
172
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800173String8::~String8()
174{
175 SharedBuffer::bufferFromData(mString)->release();
176}
177
Sergio Girod2529f22015-09-23 16:22:59 +0100178size_t String8::length() const
179{
180 return SharedBuffer::sizeFromData(mString)-1;
181}
182
Jeff Brown1d618d62010-12-02 13:50:46 -0800183String8 String8::format(const char* fmt, ...)
184{
185 va_list args;
186 va_start(args, fmt);
187
188 String8 result(formatV(fmt, args));
189
190 va_end(args);
191 return result;
192}
193
194String8 String8::formatV(const char* fmt, va_list args)
195{
196 String8 result;
197 result.appendFormatV(fmt, args);
198 return result;
199}
200
Jeff Brown48da31b2010-09-12 17:55:08 -0700201void String8::clear() {
202 SharedBuffer::bufferFromData(mString)->release();
203 mString = getEmptyString();
204}
205
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800206void String8::setTo(const String8& other)
207{
208 SharedBuffer::bufferFromData(other.mString)->acquire();
209 SharedBuffer::bufferFromData(mString)->release();
210 mString = other.mString;
211}
212
213status_t String8::setTo(const char* other)
214{
Andreas Huber10e5da52010-06-10 11:14:26 -0700215 const char *newString = allocFromUTF8(other, strlen(other));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800216 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700217 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700218 if (mString) return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800219
220 mString = getEmptyString();
221 return NO_MEMORY;
222}
223
224status_t String8::setTo(const char* other, size_t len)
225{
Andreas Huber10e5da52010-06-10 11:14:26 -0700226 const char *newString = allocFromUTF8(other, len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800227 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700228 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700229 if (mString) return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800230
231 mString = getEmptyString();
232 return NO_MEMORY;
233}
234
235status_t String8::setTo(const char16_t* other, size_t len)
236{
Andreas Huber10e5da52010-06-10 11:14:26 -0700237 const char *newString = allocFromUTF16(other, len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800238 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700239 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700240 if (mString) return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800241
242 mString = getEmptyString();
243 return NO_MEMORY;
244}
245
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900246status_t String8::setTo(const char32_t* other, size_t len)
247{
Andreas Huber10e5da52010-06-10 11:14:26 -0700248 const char *newString = allocFromUTF32(other, len);
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900249 SharedBuffer::bufferFromData(mString)->release();
Andreas Huber10e5da52010-06-10 11:14:26 -0700250 mString = newString;
Elliott Hughes643268f2018-10-08 11:10:11 -0700251 if (mString) return OK;
Daisuke Miyakawa44dad3e2009-06-30 20:40:42 +0900252
253 mString = getEmptyString();
254 return NO_MEMORY;
255}
256
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800257status_t String8::append(const String8& other)
258{
259 const size_t otherLen = other.bytes();
260 if (bytes() == 0) {
261 setTo(other);
Elliott Hughes643268f2018-10-08 11:10:11 -0700262 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800263 } else if (otherLen == 0) {
Elliott Hughes643268f2018-10-08 11:10:11 -0700264 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800265 }
266
Tomasz Wasilczyk90af4152023-08-11 02:12:16 +0000267 return real_append(other.c_str(), otherLen);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800268}
269
270status_t String8::append(const char* other)
271{
272 return append(other, strlen(other));
273}
274
275status_t String8::append(const char* other, size_t otherLen)
276{
277 if (bytes() == 0) {
278 return setTo(other, otherLen);
279 } else if (otherLen == 0) {
Elliott Hughes643268f2018-10-08 11:10:11 -0700280 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800281 }
282
283 return real_append(other, otherLen);
284}
285
Jeff Brown35a154e2010-07-15 23:54:05 -0700286status_t String8::appendFormat(const char* fmt, ...)
287{
Jeff Brown647925d2010-11-10 16:03:06 -0800288 va_list args;
289 va_start(args, fmt);
Jeff Brown35a154e2010-07-15 23:54:05 -0700290
Jeff Brown647925d2010-11-10 16:03:06 -0800291 status_t result = appendFormatV(fmt, args);
292
293 va_end(args);
294 return result;
295}
296
297status_t String8::appendFormatV(const char* fmt, va_list args)
298{
Elliott Hughes643268f2018-10-08 11:10:11 -0700299 int n, result = OK;
Fengwei Yinfff9d112014-02-27 01:17:09 +0800300 va_list tmp_args;
301
302 /* args is undefined after vsnprintf.
303 * So we need a copy here to avoid the
304 * second vsnprintf access undefined args.
305 */
306 va_copy(tmp_args, args);
Yi Konge1731a42018-07-16 18:11:34 -0700307 n = vsnprintf(nullptr, 0, fmt, tmp_args);
Fengwei Yinfff9d112014-02-27 01:17:09 +0800308 va_end(tmp_args);
309
Steven Moreland0f0cb952020-07-28 21:41:54 +0000310 if (n < 0) return UNKNOWN_ERROR;
311
312 if (n > 0) {
Jeff Brown35a154e2010-07-15 23:54:05 -0700313 size_t oldLength = length();
Andrei Homescu1a867dc2022-03-29 00:30:34 +0000314 if (static_cast<size_t>(n) > std::numeric_limits<size_t>::max() - 1 ||
Steven Moreland0f0cb952020-07-28 21:41:54 +0000315 oldLength > std::numeric_limits<size_t>::max() - n - 1) {
316 return NO_MEMORY;
317 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700318 char* buf = lockBuffer(oldLength + n);
319 if (buf) {
Jeff Brown647925d2010-11-10 16:03:06 -0800320 vsnprintf(buf + oldLength, n + 1, fmt, args);
Jeff Brown35a154e2010-07-15 23:54:05 -0700321 } else {
322 result = NO_MEMORY;
323 }
324 }
Jeff Brown35a154e2010-07-15 23:54:05 -0700325 return result;
326}
327
Elliott Hughes59682762021-06-10 16:42:20 -0700328status_t String8::real_append(const char* other, size_t otherLen) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800329 const size_t myLen = bytes();
Samuel Tan95fd5272016-02-18 16:56:21 -0800330
Elliott Hughes59682762021-06-10 16:42:20 -0700331 SharedBuffer* buf;
332 size_t newLen;
333 if (__builtin_add_overflow(myLen, otherLen, &newLen) ||
334 __builtin_add_overflow(newLen, 1, &newLen) ||
335 (buf = SharedBuffer::bufferFromData(mString)->editResize(newLen)) == nullptr) {
336 return NO_MEMORY;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800337 }
Elliott Hughes59682762021-06-10 16:42:20 -0700338
339 char* str = (char*)buf->data();
340 mString = str;
341 str += myLen;
342 memcpy(str, other, otherLen);
343 str[otherLen] = '\0';
344 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800345}
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) {
Eric Miaoc6ce48e2023-07-14 16:35:09 -0700393 ALOG_ASSERT(other, "String8::removeAll() requires a non-NULL string");
394
395 if (*other == '\0')
396 return true;
397
Jeff Brown5ee915a2014-06-06 19:30:15 -0700398 ssize_t index = find(other);
399 if (index < 0) return false;
400
401 char* buf = lockBuffer(size());
402 if (!buf) return false; // out of memory
403
404 size_t skip = strlen(other);
405 size_t len = size();
406 size_t tail = index;
407 while (size_t(index) < len) {
408 ssize_t next = find(other, index + skip);
409 if (next < 0) {
410 next = len;
411 }
412
Andreas Gampedd060f02014-11-13 15:50:17 -0800413 memmove(buf + tail, buf + index + skip, next - index - skip);
Jeff Brown5ee915a2014-06-06 19:30:15 -0700414 tail += next - index - skip;
415 index = next;
416 }
417 unlockBuffer(tail);
418 return true;
419}
420
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800421void String8::toLower()
422{
Elliott Hughesa8583952021-04-08 13:26:49 -0700423 const size_t length = size();
424 if (length == 0) return;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800425
Elliott Hughesa8583952021-04-08 13:26:49 -0700426 char* buf = lockBuffer(length);
427 for (size_t i = length; i > 0; --i) {
Steven Morelandfdbc5652020-07-13 23:31:45 +0000428 *buf = static_cast<char>(tolower(*buf));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800429 buf++;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800430 }
Elliott Hughesa8583952021-04-08 13:26:49 -0700431 unlockBuffer(length);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800432}
433
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800434// ---------------------------------------------------------------------------
435// Path functions
436
Greg Kaiserd03851e2021-07-19 20:19:44 +0000437static void setPathName(String8& s, const char* name) {
438 size_t len = strlen(name);
439 char* buf = s.lockBuffer(len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800440
441 memcpy(buf, name, len);
442
443 // remove trailing path separator, if present
Greg Kaiserd03851e2021-07-19 20:19:44 +0000444 if (len > 0 && buf[len - 1] == OS_PATH_SEPARATOR) len--;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800445 buf[len] = '\0';
446
Greg Kaiserd03851e2021-07-19 20:19:44 +0000447 s.unlockBuffer(len);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800448}
449
450String8 String8::getPathLeaf(void) const
451{
452 const char* cp;
453 const char*const buf = mString;
454
455 cp = strrchr(buf, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700456 if (cp == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800457 return String8(*this);
458 else
459 return String8(cp+1);
460}
461
462String8 String8::getPathDir(void) const
463{
464 const char* cp;
465 const char*const str = mString;
466
467 cp = strrchr(str, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700468 if (cp == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800469 return String8("");
470 else
471 return String8(str, cp - str);
472}
473
474String8 String8::walkPath(String8* outRemains) const
475{
476 const char* cp;
477 const char*const str = mString;
478 const char* buf = str;
479
480 cp = strchr(buf, OS_PATH_SEPARATOR);
481 if (cp == buf) {
482 // don't include a leading '/'.
483 buf = buf+1;
484 cp = strchr(buf, OS_PATH_SEPARATOR);
485 }
486
Yi Konge1731a42018-07-16 18:11:34 -0700487 if (cp == nullptr) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800488 String8 res = buf != str ? String8(buf) : *this;
489 if (outRemains) *outRemains = String8("");
490 return res;
491 }
492
493 String8 res(buf, cp-buf);
494 if (outRemains) *outRemains = String8(cp+1);
495 return res;
496}
497
498/*
499 * Helper function for finding the start of an extension in a pathname.
500 *
501 * Returns a pointer inside mString, or NULL if no extension was found.
502 */
503char* String8::find_extension(void) const
504{
505 const char* lastSlash;
506 const char* lastDot;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800507 const char* const str = mString;
508
509 // only look at the filename
510 lastSlash = strrchr(str, OS_PATH_SEPARATOR);
Yi Konge1731a42018-07-16 18:11:34 -0700511 if (lastSlash == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800512 lastSlash = str;
513 else
514 lastSlash++;
515
516 // find the last dot
517 lastDot = strrchr(lastSlash, '.');
Yi Konge1731a42018-07-16 18:11:34 -0700518 if (lastDot == nullptr)
519 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800520
521 // looks good, ship it
522 return const_cast<char*>(lastDot);
523}
524
525String8 String8::getPathExtension(void) const
526{
527 char* ext;
528
529 ext = find_extension();
Yi Konge1731a42018-07-16 18:11:34 -0700530 if (ext != nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800531 return String8(ext);
532 else
533 return String8("");
534}
535
536String8 String8::getBasePath(void) const
537{
538 char* ext;
539 const char* const str = mString;
540
541 ext = find_extension();
Yi Konge1731a42018-07-16 18:11:34 -0700542 if (ext == nullptr)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800543 return String8(*this);
544 else
545 return String8(str, ext - str);
546}
547
548String8& String8::appendPath(const char* name)
549{
550 // TODO: The test below will fail for Win32 paths. Fix later or ignore.
551 if (name[0] != OS_PATH_SEPARATOR) {
552 if (*name == '\0') {
553 // nothing to do
554 return *this;
555 }
556
557 size_t len = length();
558 if (len == 0) {
559 // no existing filename, just use the new one
Greg Kaiserd03851e2021-07-19 20:19:44 +0000560 setPathName(*this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800561 return *this;
562 }
563
564 // make room for oldPath + '/' + newPath
565 int newlen = strlen(name);
566
567 char* buf = lockBuffer(len+1+newlen);
568
569 // insert a '/' if needed
570 if (buf[len-1] != OS_PATH_SEPARATOR)
571 buf[len++] = OS_PATH_SEPARATOR;
572
573 memcpy(buf+len, name, newlen+1);
574 len += newlen;
575
576 unlockBuffer(len);
577
578 return *this;
579 } else {
Greg Kaiserd03851e2021-07-19 20:19:44 +0000580 setPathName(*this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800581 return *this;
582 }
583}
584
585String8& String8::convertToResPath()
586{
587#if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR
588 size_t len = length();
589 if (len > 0) {
590 char * buf = lockBuffer(len);
591 for (char * end = buf + len; buf < end; ++buf) {
592 if (*buf == OS_PATH_SEPARATOR)
593 *buf = RES_PATH_SEPARATOR;
594 }
595 unlockBuffer(len);
596 }
597#endif
598 return *this;
599}
600
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800601}; // namespace android