2009-03-04 04:31:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-08-21 02:38:16 +02:00
|
|
|
#define __STDC_LIMIT_MACROS
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
#include <utils/String8.h>
|
|
|
|
|
2015-07-29 23:02:29 +02:00
|
|
|
#include <utils/Compat.h>
|
2009-03-04 04:31:44 +01:00
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <utils/String16.h>
|
|
|
|
|
2017-03-10 17:58:36 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2021-04-16 00:18:54 +02:00
|
|
|
#include <string>
|
|
|
|
|
2015-09-23 17:22:59 +02:00
|
|
|
#include "SharedBuffer.h"
|
|
|
|
|
2009-06-30 13:40:42 +02:00
|
|
|
/*
|
|
|
|
* Functions outside android is below the namespace android, since they use
|
|
|
|
* functions and constants in android namespace.
|
|
|
|
*/
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2009-06-30 13:40:42 +02:00
|
|
|
namespace android {
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
// Separator used by resource paths. This is not platform dependent contrary
|
|
|
|
// to OS_PATH_SEPARATOR.
|
|
|
|
#define RES_PATH_SEPARATOR '/'
|
|
|
|
|
2018-03-06 18:11:29 +01:00
|
|
|
static inline char* getEmptyString() {
|
|
|
|
static SharedBuffer* gEmptyStringBuf = [] {
|
|
|
|
SharedBuffer* buf = SharedBuffer::alloc(1);
|
|
|
|
char* str = static_cast<char*>(buf->data());
|
|
|
|
*str = 0;
|
|
|
|
return buf;
|
|
|
|
}();
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
gEmptyStringBuf->acquire();
|
2018-03-06 18:11:29 +01:00
|
|
|
return static_cast<char*>(gEmptyStringBuf->data());
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static char* allocFromUTF8(const char* in, size_t len)
|
|
|
|
{
|
|
|
|
if (len > 0) {
|
2015-08-18 15:44:54 +02:00
|
|
|
if (len == SIZE_MAX) {
|
2018-07-17 03:11:34 +02:00
|
|
|
return nullptr;
|
2015-08-18 15:44:54 +02:00
|
|
|
}
|
2009-03-04 04:31:44 +01:00
|
|
|
SharedBuffer* buf = SharedBuffer::alloc(len+1);
|
2012-01-09 19:35:44 +01:00
|
|
|
ALOG_ASSERT(buf, "Unable to allocate shared buffer");
|
2009-03-04 04:31:44 +01:00
|
|
|
if (buf) {
|
|
|
|
char* str = (char*)buf->data();
|
|
|
|
memcpy(str, in, len);
|
|
|
|
str[len] = 0;
|
|
|
|
return str;
|
|
|
|
}
|
2018-07-17 03:11:34 +02:00
|
|
|
return nullptr;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return getEmptyString();
|
|
|
|
}
|
|
|
|
|
2010-11-09 23:37:23 +01:00
|
|
|
static char* allocFromUTF16(const char16_t* in, size_t len)
|
2009-03-04 04:31:44 +01:00
|
|
|
{
|
|
|
|
if (len == 0) return getEmptyString();
|
2009-06-30 13:40:42 +02:00
|
|
|
|
2016-06-28 19:02:29 +02:00
|
|
|
// Allow for closing '\0'
|
|
|
|
const ssize_t resultStrLen = utf16_to_utf8_length(in, len) + 1;
|
|
|
|
if (resultStrLen < 1) {
|
2010-11-09 23:37:23 +01:00
|
|
|
return getEmptyString();
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
2009-06-30 13:40:42 +02:00
|
|
|
|
2016-06-28 19:02:29 +02:00
|
|
|
SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
|
2012-01-09 19:35:44 +01:00
|
|
|
ALOG_ASSERT(buf, "Unable to allocate shared buffer");
|
2010-11-09 23:37:23 +01:00
|
|
|
if (!buf) {
|
|
|
|
return getEmptyString();
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
2009-06-30 13:40:42 +02:00
|
|
|
|
2016-06-28 19:02:29 +02:00
|
|
|
char* resultStr = (char*)buf->data();
|
|
|
|
utf16_to_utf8(in, len, resultStr, resultStrLen);
|
|
|
|
return resultStr;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
2010-11-09 23:37:23 +01:00
|
|
|
static char* allocFromUTF32(const char32_t* in, size_t len)
|
2009-06-30 13:40:42 +02:00
|
|
|
{
|
2010-11-09 23:37:23 +01:00
|
|
|
if (len == 0) {
|
|
|
|
return getEmptyString();
|
|
|
|
}
|
2009-12-04 18:38:48 +01:00
|
|
|
|
2016-06-28 19:02:29 +02:00
|
|
|
const ssize_t resultStrLen = utf32_to_utf8_length(in, len) + 1;
|
|
|
|
if (resultStrLen < 1) {
|
2010-11-09 23:37:23 +01:00
|
|
|
return getEmptyString();
|
|
|
|
}
|
2009-12-04 18:38:48 +01:00
|
|
|
|
2016-06-28 19:02:29 +02:00
|
|
|
SharedBuffer* buf = SharedBuffer::alloc(resultStrLen);
|
2012-01-09 19:35:44 +01:00
|
|
|
ALOG_ASSERT(buf, "Unable to allocate shared buffer");
|
2010-11-09 23:37:23 +01:00
|
|
|
if (!buf) {
|
|
|
|
return getEmptyString();
|
2009-12-04 18:38:48 +01:00
|
|
|
}
|
|
|
|
|
2016-06-28 19:02:29 +02:00
|
|
|
char* resultStr = (char*) buf->data();
|
|
|
|
utf32_to_utf8(in, len, resultStr, resultStrLen);
|
2009-06-30 13:40:42 +02:00
|
|
|
|
2016-06-28 19:02:29 +02:00
|
|
|
return resultStr;
|
2009-06-30 13:40:42 +02:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
String8::String8()
|
|
|
|
: mString(getEmptyString())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String8::String8(const String8& o)
|
|
|
|
: mString(o.mString)
|
|
|
|
{
|
|
|
|
SharedBuffer::bufferFromData(mString)->acquire();
|
|
|
|
}
|
|
|
|
|
|
|
|
String8::String8(const char* o)
|
|
|
|
: mString(allocFromUTF8(o, strlen(o)))
|
|
|
|
{
|
2018-07-17 03:11:34 +02:00
|
|
|
if (mString == nullptr) {
|
2009-03-04 04:31:44 +01:00
|
|
|
mString = getEmptyString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String8::String8(const char* o, size_t len)
|
|
|
|
: mString(allocFromUTF8(o, len))
|
|
|
|
{
|
2018-07-17 03:11:34 +02:00
|
|
|
if (mString == nullptr) {
|
2009-03-04 04:31:44 +01:00
|
|
|
mString = getEmptyString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String8::String8(const String16& o)
|
|
|
|
: mString(allocFromUTF16(o.string(), o.size()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String8::String8(const char16_t* o)
|
|
|
|
: mString(allocFromUTF16(o, strlen16(o)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String8::String8(const char16_t* o, size_t len)
|
|
|
|
: mString(allocFromUTF16(o, len))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-06-30 13:40:42 +02:00
|
|
|
String8::String8(const char32_t* o)
|
2021-04-16 00:18:54 +02:00
|
|
|
: mString(allocFromUTF32(o, std::char_traits<char32_t>::length(o))) {}
|
2009-06-30 13:40:42 +02:00
|
|
|
|
|
|
|
String8::String8(const char32_t* o, size_t len)
|
|
|
|
: mString(allocFromUTF32(o, len))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
String8::~String8()
|
|
|
|
{
|
|
|
|
SharedBuffer::bufferFromData(mString)->release();
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:22:59 +02:00
|
|
|
size_t String8::length() const
|
|
|
|
{
|
|
|
|
return SharedBuffer::sizeFromData(mString)-1;
|
|
|
|
}
|
|
|
|
|
2010-12-02 22:50:46 +01:00
|
|
|
String8 String8::format(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
String8 result(formatV(fmt, args));
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
String8 String8::formatV(const char* fmt, va_list args)
|
|
|
|
{
|
|
|
|
String8 result;
|
|
|
|
result.appendFormatV(fmt, args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-09-13 02:55:08 +02:00
|
|
|
void String8::clear() {
|
|
|
|
SharedBuffer::bufferFromData(mString)->release();
|
|
|
|
mString = getEmptyString();
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
void String8::setTo(const String8& other)
|
|
|
|
{
|
|
|
|
SharedBuffer::bufferFromData(other.mString)->acquire();
|
|
|
|
SharedBuffer::bufferFromData(mString)->release();
|
|
|
|
mString = other.mString;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t String8::setTo(const char* other)
|
|
|
|
{
|
2010-06-10 20:14:26 +02:00
|
|
|
const char *newString = allocFromUTF8(other, strlen(other));
|
2009-03-04 04:31:44 +01:00
|
|
|
SharedBuffer::bufferFromData(mString)->release();
|
2010-06-10 20:14:26 +02:00
|
|
|
mString = newString;
|
2018-10-08 20:10:11 +02:00
|
|
|
if (mString) return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
mString = getEmptyString();
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t String8::setTo(const char* other, size_t len)
|
|
|
|
{
|
2010-06-10 20:14:26 +02:00
|
|
|
const char *newString = allocFromUTF8(other, len);
|
2009-03-04 04:31:44 +01:00
|
|
|
SharedBuffer::bufferFromData(mString)->release();
|
2010-06-10 20:14:26 +02:00
|
|
|
mString = newString;
|
2018-10-08 20:10:11 +02:00
|
|
|
if (mString) return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
mString = getEmptyString();
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t String8::setTo(const char16_t* other, size_t len)
|
|
|
|
{
|
2010-06-10 20:14:26 +02:00
|
|
|
const char *newString = allocFromUTF16(other, len);
|
2009-03-04 04:31:44 +01:00
|
|
|
SharedBuffer::bufferFromData(mString)->release();
|
2010-06-10 20:14:26 +02:00
|
|
|
mString = newString;
|
2018-10-08 20:10:11 +02:00
|
|
|
if (mString) return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
mString = getEmptyString();
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2009-06-30 13:40:42 +02:00
|
|
|
status_t String8::setTo(const char32_t* other, size_t len)
|
|
|
|
{
|
2010-06-10 20:14:26 +02:00
|
|
|
const char *newString = allocFromUTF32(other, len);
|
2009-06-30 13:40:42 +02:00
|
|
|
SharedBuffer::bufferFromData(mString)->release();
|
2010-06-10 20:14:26 +02:00
|
|
|
mString = newString;
|
2018-10-08 20:10:11 +02:00
|
|
|
if (mString) return OK;
|
2009-06-30 13:40:42 +02:00
|
|
|
|
|
|
|
mString = getEmptyString();
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
status_t String8::append(const String8& other)
|
|
|
|
{
|
|
|
|
const size_t otherLen = other.bytes();
|
|
|
|
if (bytes() == 0) {
|
|
|
|
setTo(other);
|
2018-10-08 20:10:11 +02:00
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
} else if (otherLen == 0) {
|
2018-10-08 20:10:11 +02:00
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return real_append(other.string(), otherLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t String8::append(const char* other)
|
|
|
|
{
|
|
|
|
return append(other, strlen(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t String8::append(const char* other, size_t otherLen)
|
|
|
|
{
|
|
|
|
if (bytes() == 0) {
|
|
|
|
return setTo(other, otherLen);
|
|
|
|
} else if (otherLen == 0) {
|
2018-10-08 20:10:11 +02:00
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return real_append(other, otherLen);
|
|
|
|
}
|
|
|
|
|
2010-07-16 08:54:05 +02:00
|
|
|
status_t String8::appendFormat(const char* fmt, ...)
|
|
|
|
{
|
2010-11-11 01:03:06 +01:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2010-07-16 08:54:05 +02:00
|
|
|
|
2010-11-11 01:03:06 +01:00
|
|
|
status_t result = appendFormatV(fmt, args);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t String8::appendFormatV(const char* fmt, va_list args)
|
|
|
|
{
|
2018-10-08 20:10:11 +02:00
|
|
|
int n, result = OK;
|
2014-02-26 18:17:09 +01:00
|
|
|
va_list tmp_args;
|
|
|
|
|
|
|
|
/* args is undefined after vsnprintf.
|
|
|
|
* So we need a copy here to avoid the
|
|
|
|
* second vsnprintf access undefined args.
|
|
|
|
*/
|
|
|
|
va_copy(tmp_args, args);
|
2018-07-17 03:11:34 +02:00
|
|
|
n = vsnprintf(nullptr, 0, fmt, tmp_args);
|
2014-02-26 18:17:09 +01:00
|
|
|
va_end(tmp_args);
|
|
|
|
|
2020-07-28 23:41:54 +02:00
|
|
|
if (n < 0) return UNKNOWN_ERROR;
|
|
|
|
|
|
|
|
if (n > 0) {
|
2010-07-16 08:54:05 +02:00
|
|
|
size_t oldLength = length();
|
2022-03-29 02:30:34 +02:00
|
|
|
if (static_cast<size_t>(n) > std::numeric_limits<size_t>::max() - 1 ||
|
2020-07-28 23:41:54 +02:00
|
|
|
oldLength > std::numeric_limits<size_t>::max() - n - 1) {
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
2010-07-16 08:54:05 +02:00
|
|
|
char* buf = lockBuffer(oldLength + n);
|
|
|
|
if (buf) {
|
2010-11-11 01:03:06 +01:00
|
|
|
vsnprintf(buf + oldLength, n + 1, fmt, args);
|
2010-07-16 08:54:05 +02:00
|
|
|
} else {
|
|
|
|
result = NO_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-11 01:42:20 +02:00
|
|
|
status_t String8::real_append(const char* other, size_t otherLen) {
|
2009-03-04 04:31:44 +01:00
|
|
|
const size_t myLen = bytes();
|
2016-02-19 01:56:21 +01:00
|
|
|
|
2021-06-11 01:42:20 +02:00
|
|
|
SharedBuffer* buf;
|
|
|
|
size_t newLen;
|
|
|
|
if (__builtin_add_overflow(myLen, otherLen, &newLen) ||
|
|
|
|
__builtin_add_overflow(newLen, 1, &newLen) ||
|
|
|
|
(buf = SharedBuffer::bufferFromData(mString)->editResize(newLen)) == nullptr) {
|
|
|
|
return NO_MEMORY;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
2021-06-11 01:42:20 +02:00
|
|
|
|
|
|
|
char* str = (char*)buf->data();
|
|
|
|
mString = str;
|
|
|
|
str += myLen;
|
|
|
|
memcpy(str, other, otherLen);
|
|
|
|
str[otherLen] = '\0';
|
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char* String8::lockBuffer(size_t size)
|
|
|
|
{
|
|
|
|
SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
|
|
|
|
->editResize(size+1);
|
|
|
|
if (buf) {
|
|
|
|
char* str = (char*)buf->data();
|
|
|
|
mString = str;
|
|
|
|
return str;
|
|
|
|
}
|
2018-07-17 03:11:34 +02:00
|
|
|
return nullptr;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void String8::unlockBuffer()
|
|
|
|
{
|
|
|
|
unlockBuffer(strlen(mString));
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t String8::unlockBuffer(size_t size)
|
|
|
|
{
|
|
|
|
if (size != this->size()) {
|
|
|
|
SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
|
|
|
|
->editResize(size+1);
|
2010-07-16 08:54:05 +02:00
|
|
|
if (! buf) {
|
|
|
|
return NO_MEMORY;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
2010-07-16 08:54:05 +02:00
|
|
|
|
|
|
|
char* str = (char*)buf->data();
|
|
|
|
str[size] = 0;
|
|
|
|
mString = str;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
2010-07-16 08:54:05 +02:00
|
|
|
|
2018-10-08 20:10:11 +02:00
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t String8::find(const char* other, size_t start) const
|
|
|
|
{
|
|
|
|
size_t len = size();
|
|
|
|
if (start >= len) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char* s = mString+start;
|
|
|
|
const char* p = strstr(s, other);
|
|
|
|
return p ? p-mString : -1;
|
|
|
|
}
|
|
|
|
|
2014-06-07 04:30:15 +02:00
|
|
|
bool String8::removeAll(const char* other) {
|
|
|
|
ssize_t index = find(other);
|
|
|
|
if (index < 0) return false;
|
|
|
|
|
|
|
|
char* buf = lockBuffer(size());
|
|
|
|
if (!buf) return false; // out of memory
|
|
|
|
|
|
|
|
size_t skip = strlen(other);
|
|
|
|
size_t len = size();
|
|
|
|
size_t tail = index;
|
|
|
|
while (size_t(index) < len) {
|
|
|
|
ssize_t next = find(other, index + skip);
|
|
|
|
if (next < 0) {
|
|
|
|
next = len;
|
|
|
|
}
|
|
|
|
|
2014-11-14 00:50:17 +01:00
|
|
|
memmove(buf + tail, buf + index + skip, next - index - skip);
|
2014-06-07 04:30:15 +02:00
|
|
|
tail += next - index - skip;
|
|
|
|
index = next;
|
|
|
|
}
|
|
|
|
unlockBuffer(tail);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
void String8::toLower()
|
|
|
|
{
|
2021-04-08 22:26:49 +02:00
|
|
|
const size_t length = size();
|
|
|
|
if (length == 0) return;
|
2009-03-04 04:31:44 +01:00
|
|
|
|
2021-04-08 22:26:49 +02:00
|
|
|
char* buf = lockBuffer(length);
|
|
|
|
for (size_t i = length; i > 0; --i) {
|
2020-07-14 01:31:45 +02:00
|
|
|
*buf = static_cast<char>(tolower(*buf));
|
2009-03-04 04:31:44 +01:00
|
|
|
buf++;
|
|
|
|
}
|
2021-04-08 22:26:49 +02:00
|
|
|
unlockBuffer(length);
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Path functions
|
|
|
|
|
2021-07-19 22:19:44 +02:00
|
|
|
static void setPathName(String8& s, const char* name) {
|
|
|
|
size_t len = strlen(name);
|
|
|
|
char* buf = s.lockBuffer(len);
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
memcpy(buf, name, len);
|
|
|
|
|
|
|
|
// remove trailing path separator, if present
|
2021-07-19 22:19:44 +02:00
|
|
|
if (len > 0 && buf[len - 1] == OS_PATH_SEPARATOR) len--;
|
2009-03-04 04:31:44 +01:00
|
|
|
buf[len] = '\0';
|
|
|
|
|
2021-07-19 22:19:44 +02:00
|
|
|
s.unlockBuffer(len);
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String8 String8::getPathLeaf(void) const
|
|
|
|
{
|
|
|
|
const char* cp;
|
|
|
|
const char*const buf = mString;
|
|
|
|
|
|
|
|
cp = strrchr(buf, OS_PATH_SEPARATOR);
|
2018-07-17 03:11:34 +02:00
|
|
|
if (cp == nullptr)
|
2009-03-04 04:31:44 +01:00
|
|
|
return String8(*this);
|
|
|
|
else
|
|
|
|
return String8(cp+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
String8 String8::getPathDir(void) const
|
|
|
|
{
|
|
|
|
const char* cp;
|
|
|
|
const char*const str = mString;
|
|
|
|
|
|
|
|
cp = strrchr(str, OS_PATH_SEPARATOR);
|
2018-07-17 03:11:34 +02:00
|
|
|
if (cp == nullptr)
|
2009-03-04 04:31:44 +01:00
|
|
|
return String8("");
|
|
|
|
else
|
|
|
|
return String8(str, cp - str);
|
|
|
|
}
|
|
|
|
|
|
|
|
String8 String8::walkPath(String8* outRemains) const
|
|
|
|
{
|
|
|
|
const char* cp;
|
|
|
|
const char*const str = mString;
|
|
|
|
const char* buf = str;
|
|
|
|
|
|
|
|
cp = strchr(buf, OS_PATH_SEPARATOR);
|
|
|
|
if (cp == buf) {
|
|
|
|
// don't include a leading '/'.
|
|
|
|
buf = buf+1;
|
|
|
|
cp = strchr(buf, OS_PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2018-07-17 03:11:34 +02:00
|
|
|
if (cp == nullptr) {
|
2009-03-04 04:31:44 +01:00
|
|
|
String8 res = buf != str ? String8(buf) : *this;
|
|
|
|
if (outRemains) *outRemains = String8("");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
String8 res(buf, cp-buf);
|
|
|
|
if (outRemains) *outRemains = String8(cp+1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper function for finding the start of an extension in a pathname.
|
|
|
|
*
|
|
|
|
* Returns a pointer inside mString, or NULL if no extension was found.
|
|
|
|
*/
|
|
|
|
char* String8::find_extension(void) const
|
|
|
|
{
|
|
|
|
const char* lastSlash;
|
|
|
|
const char* lastDot;
|
|
|
|
const char* const str = mString;
|
|
|
|
|
|
|
|
// only look at the filename
|
|
|
|
lastSlash = strrchr(str, OS_PATH_SEPARATOR);
|
2018-07-17 03:11:34 +02:00
|
|
|
if (lastSlash == nullptr)
|
2009-03-04 04:31:44 +01:00
|
|
|
lastSlash = str;
|
|
|
|
else
|
|
|
|
lastSlash++;
|
|
|
|
|
|
|
|
// find the last dot
|
|
|
|
lastDot = strrchr(lastSlash, '.');
|
2018-07-17 03:11:34 +02:00
|
|
|
if (lastDot == nullptr)
|
|
|
|
return nullptr;
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
// looks good, ship it
|
|
|
|
return const_cast<char*>(lastDot);
|
|
|
|
}
|
|
|
|
|
|
|
|
String8 String8::getPathExtension(void) const
|
|
|
|
{
|
|
|
|
char* ext;
|
|
|
|
|
|
|
|
ext = find_extension();
|
2018-07-17 03:11:34 +02:00
|
|
|
if (ext != nullptr)
|
2009-03-04 04:31:44 +01:00
|
|
|
return String8(ext);
|
|
|
|
else
|
|
|
|
return String8("");
|
|
|
|
}
|
|
|
|
|
|
|
|
String8 String8::getBasePath(void) const
|
|
|
|
{
|
|
|
|
char* ext;
|
|
|
|
const char* const str = mString;
|
|
|
|
|
|
|
|
ext = find_extension();
|
2018-07-17 03:11:34 +02:00
|
|
|
if (ext == nullptr)
|
2009-03-04 04:31:44 +01:00
|
|
|
return String8(*this);
|
|
|
|
else
|
|
|
|
return String8(str, ext - str);
|
|
|
|
}
|
|
|
|
|
|
|
|
String8& String8::appendPath(const char* name)
|
|
|
|
{
|
|
|
|
// TODO: The test below will fail for Win32 paths. Fix later or ignore.
|
|
|
|
if (name[0] != OS_PATH_SEPARATOR) {
|
|
|
|
if (*name == '\0') {
|
|
|
|
// nothing to do
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t len = length();
|
|
|
|
if (len == 0) {
|
|
|
|
// no existing filename, just use the new one
|
2021-07-19 22:19:44 +02:00
|
|
|
setPathName(*this, name);
|
2009-03-04 04:31:44 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make room for oldPath + '/' + newPath
|
|
|
|
int newlen = strlen(name);
|
|
|
|
|
|
|
|
char* buf = lockBuffer(len+1+newlen);
|
|
|
|
|
|
|
|
// insert a '/' if needed
|
|
|
|
if (buf[len-1] != OS_PATH_SEPARATOR)
|
|
|
|
buf[len++] = OS_PATH_SEPARATOR;
|
|
|
|
|
|
|
|
memcpy(buf+len, name, newlen+1);
|
|
|
|
len += newlen;
|
|
|
|
|
|
|
|
unlockBuffer(len);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
} else {
|
2021-07-19 22:19:44 +02:00
|
|
|
setPathName(*this, name);
|
2009-03-04 04:31:44 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String8& String8::convertToResPath()
|
|
|
|
{
|
|
|
|
#if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR
|
|
|
|
size_t len = length();
|
|
|
|
if (len > 0) {
|
|
|
|
char * buf = lockBuffer(len);
|
|
|
|
for (char * end = buf + len; buf < end; ++buf) {
|
|
|
|
if (*buf == OS_PATH_SEPARATOR)
|
|
|
|
*buf = RES_PATH_SEPARATOR;
|
|
|
|
}
|
|
|
|
unlockBuffer(len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|