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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <utils/String16.h>
|
|
|
|
|
|
|
|
#include <utils/Log.h>
|
|
|
|
|
2017-03-10 17:58:36 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2015-09-23 17:22:59 +02:00
|
|
|
#include "SharedBuffer.h"
|
2009-03-04 04:31:44 +01:00
|
|
|
|
2009-12-04 18:38:48 +01:00
|
|
|
namespace android {
|
|
|
|
|
2019-09-05 22:18:27 +02:00
|
|
|
static const StaticString16 emptyString(u"");
|
2018-03-06 18:11:29 +01:00
|
|
|
static inline char16_t* getEmptyString() {
|
2019-09-05 22:18:27 +02:00
|
|
|
return const_cast<char16_t*>(emptyString.string());
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-09-05 22:18:27 +02:00
|
|
|
void* String16::alloc(size_t size)
|
|
|
|
{
|
|
|
|
SharedBuffer* buf = SharedBuffer::alloc(size);
|
|
|
|
buf->mClientMetadata = kIsSharedBufferAllocated;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char16_t* String16::allocFromUTF8(const char* u8str, size_t u8len)
|
2009-03-04 04:31:44 +01:00
|
|
|
{
|
2010-11-09 23:37:23 +01:00
|
|
|
if (u8len == 0) return getEmptyString();
|
|
|
|
|
|
|
|
const uint8_t* u8cur = (const uint8_t*) u8str;
|
|
|
|
|
|
|
|
const ssize_t u16len = utf8_to_utf16_length(u8cur, u8len);
|
|
|
|
if (u16len < 0) {
|
|
|
|
return getEmptyString();
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
2010-11-09 23:37:23 +01:00
|
|
|
|
2019-09-05 22:18:27 +02:00
|
|
|
SharedBuffer* buf = static_cast<SharedBuffer*>(alloc(sizeof(char16_t) * (u16len + 1)));
|
2009-03-04 04:31:44 +01:00
|
|
|
if (buf) {
|
2010-11-09 23:37:23 +01:00
|
|
|
u8cur = (const uint8_t*) u8str;
|
|
|
|
char16_t* u16str = (char16_t*)buf->data();
|
|
|
|
|
2016-07-20 21:01:33 +02:00
|
|
|
utf8_to_utf16(u8cur, u8len, u16str, ((size_t) u16len) + 1);
|
2009-12-04 18:38:48 +01:00
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
//printf("Created UTF-16 string from UTF-8 \"%s\":", in);
|
|
|
|
//printHexData(1, str, buf->size(), 16, 1);
|
|
|
|
//printf("\n");
|
2016-02-17 00:17:10 +01:00
|
|
|
|
2010-11-09 23:37:23 +01:00
|
|
|
return u16str;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
2010-11-09 23:37:23 +01:00
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
return getEmptyString();
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:18:27 +02:00
|
|
|
char16_t* String16::allocFromUTF16(const char16_t* u16str, size_t u16len) {
|
2018-03-01 20:03:04 +01:00
|
|
|
if (u16len >= SIZE_MAX / sizeof(char16_t)) {
|
|
|
|
android_errorWriteLog(0x534e4554, "73826242");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:18:27 +02:00
|
|
|
SharedBuffer* buf = static_cast<SharedBuffer*>(alloc((u16len + 1) * sizeof(char16_t)));
|
2018-03-01 20:03:04 +01:00
|
|
|
ALOG_ASSERT(buf, "Unable to allocate shared buffer");
|
|
|
|
if (buf) {
|
|
|
|
char16_t* str = (char16_t*)buf->data();
|
|
|
|
memcpy(str, u16str, u16len * sizeof(char16_t));
|
|
|
|
str[u16len] = 0;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
return getEmptyString();
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
String16::String16()
|
|
|
|
: mString(getEmptyString())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String16::String16(const String16& o)
|
|
|
|
: mString(o.mString)
|
|
|
|
{
|
2019-09-05 22:18:27 +02:00
|
|
|
acquire();
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
2021-06-26 20:30:42 +02:00
|
|
|
String16::String16(String16&& o) noexcept
|
|
|
|
: mString(o.mString)
|
|
|
|
{
|
|
|
|
o.mString = getEmptyString();
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
String16::String16(const String16& o, size_t len, size_t begin)
|
|
|
|
: mString(getEmptyString())
|
|
|
|
{
|
|
|
|
setTo(o, len, begin);
|
|
|
|
}
|
|
|
|
|
2018-03-01 20:03:04 +01:00
|
|
|
String16::String16(const char16_t* o) : mString(allocFromUTF16(o, strlen16(o))) {}
|
2016-02-17 00:17:10 +01:00
|
|
|
|
2018-03-01 20:03:04 +01:00
|
|
|
String16::String16(const char16_t* o, size_t len) : mString(allocFromUTF16(o, len)) {}
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
String16::String16(const String8& o)
|
|
|
|
: mString(allocFromUTF8(o.string(), o.size()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String16::String16(const char* o)
|
|
|
|
: mString(allocFromUTF8(o, strlen(o)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String16::String16(const char* o, size_t len)
|
|
|
|
: mString(allocFromUTF8(o, len))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String16::~String16()
|
|
|
|
{
|
2019-09-05 22:18:27 +02:00
|
|
|
release();
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
2021-06-26 20:30:42 +02:00
|
|
|
String16& String16::operator=(String16&& other) noexcept {
|
|
|
|
release();
|
|
|
|
mString = other.mString;
|
|
|
|
other.mString = getEmptyString();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:22:59 +02:00
|
|
|
size_t String16::size() const
|
|
|
|
{
|
2019-09-05 22:18:27 +02:00
|
|
|
if (isStaticString()) {
|
|
|
|
return staticStringSize();
|
|
|
|
} else {
|
|
|
|
return SharedBuffer::sizeFromData(mString) / sizeof(char16_t) - 1;
|
|
|
|
}
|
2015-09-23 17:22:59 +02:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
void String16::setTo(const String16& other)
|
|
|
|
{
|
2019-09-05 22:18:27 +02:00
|
|
|
release();
|
2009-03-04 04:31:44 +01:00
|
|
|
mString = other.mString;
|
2019-09-05 22:18:27 +02:00
|
|
|
acquire();
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t String16::setTo(const String16& other, size_t len, size_t begin)
|
|
|
|
{
|
|
|
|
const size_t N = other.size();
|
|
|
|
if (begin >= N) {
|
2019-09-05 22:18:27 +02:00
|
|
|
release();
|
2009-03-04 04:31:44 +01:00
|
|
|
mString = getEmptyString();
|
2018-10-08 20:10:11 +02:00
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
if ((begin+len) > N) len = N-begin;
|
|
|
|
if (begin == 0 && len == N) {
|
|
|
|
setTo(other);
|
2018-10-08 20:10:11 +02:00
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (&other == this) {
|
|
|
|
LOG_ALWAYS_FATAL("Not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
return setTo(other.string()+begin, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t String16::setTo(const char16_t* other)
|
|
|
|
{
|
|
|
|
return setTo(other, strlen16(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t String16::setTo(const char16_t* other, size_t len)
|
|
|
|
{
|
2018-03-01 20:03:04 +01:00
|
|
|
if (len >= SIZE_MAX / sizeof(char16_t)) {
|
|
|
|
android_errorWriteLog(0x534e4554, "73826242");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:18:27 +02:00
|
|
|
SharedBuffer* buf = static_cast<SharedBuffer*>(editResize((len + 1) * sizeof(char16_t)));
|
2009-03-04 04:31:44 +01:00
|
|
|
if (buf) {
|
|
|
|
char16_t* str = (char16_t*)buf->data();
|
2009-03-05 23:34:35 +01:00
|
|
|
memmove(str, other, len*sizeof(char16_t));
|
2009-03-04 04:31:44 +01:00
|
|
|
str[len] = 0;
|
|
|
|
mString = str;
|
2018-10-08 20:10:11 +02:00
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2021-06-11 02:06:26 +02:00
|
|
|
status_t String16::append(const String16& other) {
|
|
|
|
return append(other.string(), other.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t String16::append(const char16_t* chrs, size_t otherLen) {
|
2009-03-04 04:31:44 +01:00
|
|
|
const size_t myLen = size();
|
2016-02-17 00:17:10 +01:00
|
|
|
|
2021-06-11 02:06:26 +02:00
|
|
|
if (myLen == 0) return setTo(chrs, otherLen);
|
2018-03-01 20:03:04 +01:00
|
|
|
|
2021-06-11 02:06:26 +02:00
|
|
|
if (otherLen == 0) return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
|
2021-06-11 02:06:26 +02:00
|
|
|
size_t size = myLen;
|
|
|
|
if (__builtin_add_overflow(size, otherLen, &size) ||
|
|
|
|
__builtin_add_overflow(size, 1, &size) ||
|
|
|
|
__builtin_mul_overflow(size, sizeof(char16_t), &size)) return NO_MEMORY;
|
2016-02-17 00:17:10 +01:00
|
|
|
|
2021-06-11 02:06:26 +02:00
|
|
|
SharedBuffer* buf = static_cast<SharedBuffer*>(editResize(size));
|
|
|
|
if (!buf) return NO_MEMORY;
|
2018-03-01 20:03:04 +01:00
|
|
|
|
2021-06-11 02:06:26 +02:00
|
|
|
char16_t* str = static_cast<char16_t*>(buf->data());
|
|
|
|
memcpy(str + myLen, chrs, otherLen * sizeof(char16_t));
|
|
|
|
str[myLen + otherLen] = 0;
|
|
|
|
mString = str;
|
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
2021-06-11 02:06:26 +02:00
|
|
|
status_t String16::insert(size_t pos, const char16_t* chrs) {
|
2009-03-04 04:31:44 +01:00
|
|
|
return insert(pos, chrs, strlen16(chrs));
|
|
|
|
}
|
|
|
|
|
2021-06-11 02:06:26 +02:00
|
|
|
status_t String16::insert(size_t pos, const char16_t* chrs, size_t otherLen) {
|
2009-03-04 04:31:44 +01:00
|
|
|
const size_t myLen = size();
|
2021-06-11 02:06:26 +02:00
|
|
|
|
|
|
|
if (myLen == 0) return setTo(chrs, otherLen);
|
|
|
|
|
|
|
|
if (otherLen == 0) return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
|
|
|
|
if (pos > myLen) pos = myLen;
|
|
|
|
|
2021-06-11 02:06:26 +02:00
|
|
|
size_t size = myLen;
|
|
|
|
if (__builtin_add_overflow(size, otherLen, &size) ||
|
|
|
|
__builtin_add_overflow(size, 1, &size) ||
|
|
|
|
__builtin_mul_overflow(size, sizeof(char16_t), &size)) return NO_MEMORY;
|
2009-03-04 04:31:44 +01:00
|
|
|
|
2021-06-11 02:06:26 +02:00
|
|
|
SharedBuffer* buf = static_cast<SharedBuffer*>(editResize(size));
|
|
|
|
if (!buf) return NO_MEMORY;
|
|
|
|
|
|
|
|
char16_t* str = static_cast<char16_t*>(buf->data());
|
|
|
|
if (pos < myLen) memmove(str + pos + otherLen, str + pos, (myLen - pos) * sizeof(char16_t));
|
|
|
|
memcpy(str + pos, chrs, otherLen * sizeof(char16_t));
|
|
|
|
str[myLen + otherLen] = 0;
|
|
|
|
mString = str;
|
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t String16::findFirst(char16_t c) const
|
|
|
|
{
|
|
|
|
const char16_t* str = string();
|
|
|
|
const char16_t* p = str;
|
|
|
|
const char16_t* e = p + size();
|
|
|
|
while (p < e) {
|
|
|
|
if (*p == c) {
|
|
|
|
return p-str;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t String16::findLast(char16_t c) const
|
|
|
|
{
|
|
|
|
const char16_t* str = string();
|
|
|
|
const char16_t* p = str;
|
|
|
|
const char16_t* e = p + size();
|
|
|
|
while (p < e) {
|
|
|
|
e--;
|
|
|
|
if (*e == c) {
|
|
|
|
return e-str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String16::startsWith(const String16& prefix) const
|
|
|
|
{
|
|
|
|
const size_t ps = prefix.size();
|
|
|
|
if (ps > size()) return false;
|
|
|
|
return strzcmp16(mString, ps, prefix.string(), ps) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String16::startsWith(const char16_t* prefix) const
|
|
|
|
{
|
|
|
|
const size_t ps = strlen16(prefix);
|
|
|
|
if (ps > size()) return false;
|
|
|
|
return strncmp16(mString, prefix, ps) == 0;
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:43:31 +02:00
|
|
|
bool String16::contains(const char16_t* chrs) const
|
|
|
|
{
|
|
|
|
return strstr16(mString, chrs) != nullptr;
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:18:27 +02:00
|
|
|
void* String16::edit() {
|
|
|
|
SharedBuffer* buf;
|
|
|
|
if (isStaticString()) {
|
|
|
|
buf = static_cast<SharedBuffer*>(alloc((size() + 1) * sizeof(char16_t)));
|
|
|
|
if (buf) {
|
|
|
|
memcpy(buf->data(), mString, (size() + 1) * sizeof(char16_t));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf = SharedBuffer::bufferFromData(mString)->edit();
|
|
|
|
buf->mClientMetadata = kIsSharedBufferAllocated;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* String16::editResize(size_t newSize) {
|
|
|
|
SharedBuffer* buf;
|
|
|
|
if (isStaticString()) {
|
|
|
|
size_t copySize = (size() + 1) * sizeof(char16_t);
|
|
|
|
if (newSize < copySize) {
|
|
|
|
copySize = newSize;
|
|
|
|
}
|
|
|
|
buf = static_cast<SharedBuffer*>(alloc(newSize));
|
|
|
|
if (buf) {
|
|
|
|
memcpy(buf->data(), mString, copySize);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf = SharedBuffer::bufferFromData(mString)->editResize(newSize);
|
|
|
|
buf->mClientMetadata = kIsSharedBufferAllocated;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void String16::acquire()
|
|
|
|
{
|
|
|
|
if (!isStaticString()) {
|
|
|
|
SharedBuffer::bufferFromData(mString)->acquire();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void String16::release()
|
|
|
|
{
|
|
|
|
if (!isStaticString()) {
|
|
|
|
SharedBuffer::bufferFromData(mString)->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String16::isStaticString() const {
|
|
|
|
// See String16.h for notes on the memory layout of String16::StaticData and
|
|
|
|
// SharedBuffer.
|
|
|
|
static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4);
|
|
|
|
const uint32_t* p = reinterpret_cast<const uint32_t*>(mString);
|
|
|
|
return (*(p - 1) & kIsSharedBufferAllocated) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t String16::staticStringSize() const {
|
|
|
|
// See String16.h for notes on the memory layout of String16::StaticData and
|
|
|
|
// SharedBuffer.
|
|
|
|
static_assert(sizeof(SharedBuffer) - offsetof(SharedBuffer, mClientMetadata) == 4);
|
|
|
|
const uint32_t* p = reinterpret_cast<const uint32_t*>(mString);
|
|
|
|
return static_cast<size_t>(*(p - 1));
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:31:44 +01:00
|
|
|
status_t String16::replaceAll(char16_t replaceThis, char16_t withThis)
|
|
|
|
{
|
|
|
|
const size_t N = size();
|
|
|
|
const char16_t* str = string();
|
2019-09-05 22:18:27 +02:00
|
|
|
char16_t* edited = nullptr;
|
2009-03-04 04:31:44 +01:00
|
|
|
for (size_t i=0; i<N; i++) {
|
|
|
|
if (str[i] == replaceThis) {
|
2019-09-05 22:18:27 +02:00
|
|
|
if (!edited) {
|
|
|
|
SharedBuffer* buf = static_cast<SharedBuffer*>(edit());
|
2009-03-04 04:31:44 +01:00
|
|
|
if (!buf) {
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
2019-09-05 22:18:27 +02:00
|
|
|
edited = (char16_t*)buf->data();
|
|
|
|
mString = str = edited;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
2019-09-05 22:18:27 +02:00
|
|
|
edited[i] = withThis;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 20:10:11 +02:00
|
|
|
return OK;
|
2009-03-04 04:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|