Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1270e4fb authored by Kevin Han's avatar Kevin Han Committed by Steven Moreland
Browse files

Revert "Reland "libutils: Introduce StaticString16"""

This reverts commit c55ac92b.

Reason for revert: Breaks down-stream branches. See b/140315617

Change-Id: I4937fdf4bdcc7a44d5f10700ecf2d5e96aef7d27
Merged-In: I8da93f2c9b95183e32d4a2ea895f90c449abbe4d
parent c55ac92b
Loading
Loading
Loading
Loading
+0 −1
Original line number Original line Diff line number Diff line
@@ -205,7 +205,6 @@ cc_test {
        "Mutex_test.cpp",
        "Mutex_test.cpp",
        "SharedBuffer_test.cpp",
        "SharedBuffer_test.cpp",
        "String8_test.cpp",
        "String8_test.cpp",
        "String16_test.cpp",
        "StrongPointer_test.cpp",
        "StrongPointer_test.cpp",
        "Unicode_test.cpp",
        "Unicode_test.cpp",
        "Vector_test.cpp",
        "Vector_test.cpp",
+0 −1
Original line number Original line Diff line number Diff line
@@ -41,7 +41,6 @@ SharedBuffer* SharedBuffer::alloc(size_t size)
        // The following is OK on Android-supported platforms.
        // The following is OK on Android-supported platforms.
        sb->mRefs.store(1, std::memory_order_relaxed);
        sb->mRefs.store(1, std::memory_order_relaxed);
        sb->mSize = size;
        sb->mSize = size;
        sb->mClientMetadata = 0;
    }
    }
    return sb;
    return sb;
}
}
+1 −6
Original line number Original line Diff line number Diff line
@@ -102,12 +102,7 @@ private:
        // Must be sized to preserve correct alignment.
        // Must be sized to preserve correct alignment.
        mutable std::atomic<int32_t>        mRefs;
        mutable std::atomic<int32_t>        mRefs;
                size_t                      mSize;
                size_t                      mSize;
                uint32_t                    mReserved;
                uint32_t                    mReserved[2];
public:
        // mClientMetadata is reserved for client use.  It is initialized to 0
        // and the clients can do whatever they want with it.  Note that this is
        // placed last so that it is adjcent to the buffer allocated.
                uint32_t                    mClientMetadata;
};
};


static_assert(sizeof(SharedBuffer) % 8 == 0
static_assert(sizeof(SharedBuffer) % 8 == 0
+46 −111
Original line number Original line Diff line number Diff line
@@ -24,21 +24,21 @@


namespace android {
namespace android {


static const StaticString16 emptyString(u"");
static inline char16_t* getEmptyString() {
static inline char16_t* getEmptyString() {
    return const_cast<char16_t*>(emptyString.string());
    static SharedBuffer* gEmptyStringBuf = [] {
        SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t));
        char16_t* str = static_cast<char16_t*>(buf->data());
        *str = 0;
        return buf;
    }();

    gEmptyStringBuf->acquire();
    return static_cast<char16_t*>(gEmptyStringBuf->data());
}
}


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------


void* String16::alloc(size_t size)
static char16_t* allocFromUTF8(const char* u8str, size_t u8len)
{
    SharedBuffer* buf = SharedBuffer::alloc(size);
    buf->mClientMetadata = kIsSharedBufferAllocated;
    return buf;
}

char16_t* String16::allocFromUTF8(const char* u8str, size_t u8len)
{
{
    if (u8len == 0) return getEmptyString();
    if (u8len == 0) return getEmptyString();


@@ -49,7 +49,7 @@ char16_t* String16::allocFromUTF8(const char* u8str, size_t u8len)
        return getEmptyString();
        return getEmptyString();
    }
    }


    SharedBuffer* buf = static_cast<SharedBuffer*>(alloc(sizeof(char16_t) * (u16len + 1)));
    SharedBuffer* buf = SharedBuffer::alloc(sizeof(char16_t)*(u16len+1));
    if (buf) {
    if (buf) {
        u8cur = (const uint8_t*) u8str;
        u8cur = (const uint8_t*) u8str;
        char16_t* u16str = (char16_t*)buf->data();
        char16_t* u16str = (char16_t*)buf->data();
@@ -66,13 +66,13 @@ char16_t* String16::allocFromUTF8(const char* u8str, size_t u8len)
    return getEmptyString();
    return getEmptyString();
}
}


char16_t* String16::allocFromUTF16(const char16_t* u16str, size_t u16len) {
static char16_t* allocFromUTF16(const char16_t* u16str, size_t u16len) {
    if (u16len >= SIZE_MAX / sizeof(char16_t)) {
    if (u16len >= SIZE_MAX / sizeof(char16_t)) {
        android_errorWriteLog(0x534e4554, "73826242");
        android_errorWriteLog(0x534e4554, "73826242");
        abort();
        abort();
    }
    }


    SharedBuffer* buf = static_cast<SharedBuffer*>(alloc((u16len + 1) * sizeof(char16_t)));
    SharedBuffer* buf = SharedBuffer::alloc((u16len + 1) * sizeof(char16_t));
    ALOG_ASSERT(buf, "Unable to allocate shared buffer");
    ALOG_ASSERT(buf, "Unable to allocate shared buffer");
    if (buf) {
    if (buf) {
        char16_t* str = (char16_t*)buf->data();
        char16_t* str = (char16_t*)buf->data();
@@ -97,8 +97,8 @@ String16::String16(StaticLinkage)
    // having run. In this case we always allocate an empty string. It's less
    // having run. In this case we always allocate an empty string. It's less
    // efficient than using getEmptyString(), but we assume it's uncommon.
    // efficient than using getEmptyString(), but we assume it's uncommon.


    SharedBuffer* buf = static_cast<SharedBuffer*>(alloc(sizeof(char16_t)));
    char16_t* data = static_cast<char16_t*>(
    char16_t* data = static_cast<char16_t*>(buf->data());
            SharedBuffer::alloc(sizeof(char16_t))->data());
    data[0] = 0;
    data[0] = 0;
    mString = data;
    mString = data;
}
}
@@ -106,7 +106,7 @@ String16::String16(StaticLinkage)
String16::String16(const String16& o)
String16::String16(const String16& o)
    : mString(o.mString)
    : mString(o.mString)
{
{
    acquire();
    SharedBuffer::bufferFromData(mString)->acquire();
}
}


String16::String16(const String16& o, size_t len, size_t begin)
String16::String16(const String16& o, size_t len, size_t begin)
@@ -136,30 +136,26 @@ String16::String16(const char* o, size_t len)


String16::~String16()
String16::~String16()
{
{
    release();
    SharedBuffer::bufferFromData(mString)->release();
}
}


size_t String16::size() const
size_t String16::size() const
{
{
    if (isStaticString()) {
        return staticStringSize();
    } else {
    return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1;
    return SharedBuffer::sizeFromData(mString)/sizeof(char16_t)-1;
}
}
}


void String16::setTo(const String16& other)
void String16::setTo(const String16& other)
{
{
    release();
    SharedBuffer::bufferFromData(other.mString)->acquire();
    SharedBuffer::bufferFromData(mString)->release();
    mString = other.mString;
    mString = other.mString;
    acquire();
}
}


status_t String16::setTo(const String16& other, size_t len, size_t begin)
status_t String16::setTo(const String16& other, size_t len, size_t begin)
{
{
    const size_t N = other.size();
    const size_t N = other.size();
    if (begin >= N) {
    if (begin >= N) {
        release();
        SharedBuffer::bufferFromData(mString)->release();
        mString = getEmptyString();
        mString = getEmptyString();
        return OK;
        return OK;
    }
    }
@@ -188,7 +184,8 @@ status_t String16::setTo(const char16_t* other, size_t len)
        abort();
        abort();
    }
    }


    SharedBuffer* buf = static_cast<SharedBuffer*>(editResize((len + 1) * sizeof(char16_t)));
    SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
        ->editResize((len+1)*sizeof(char16_t));
    if (buf) {
    if (buf) {
        char16_t* str = (char16_t*)buf->data();
        char16_t* str = (char16_t*)buf->data();
        memmove(str, other, len*sizeof(char16_t));
        memmove(str, other, len*sizeof(char16_t));
@@ -215,8 +212,8 @@ status_t String16::append(const String16& other)
        abort();
        abort();
    }
    }


    SharedBuffer* buf =
    SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
            static_cast<SharedBuffer*>(editResize((myLen + otherLen + 1) * sizeof(char16_t)));
        ->editResize((myLen+otherLen+1)*sizeof(char16_t));
    if (buf) {
    if (buf) {
        char16_t* str = (char16_t*)buf->data();
        char16_t* str = (char16_t*)buf->data();
        memcpy(str+myLen, other, (otherLen+1)*sizeof(char16_t));
        memcpy(str+myLen, other, (otherLen+1)*sizeof(char16_t));
@@ -241,8 +238,8 @@ status_t String16::append(const char16_t* chrs, size_t otherLen)
        abort();
        abort();
    }
    }


    SharedBuffer* buf =
    SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
            static_cast<SharedBuffer*>(editResize((myLen + otherLen + 1) * sizeof(char16_t)));
        ->editResize((myLen+otherLen+1)*sizeof(char16_t));
    if (buf) {
    if (buf) {
        char16_t* str = (char16_t*)buf->data();
        char16_t* str = (char16_t*)buf->data();
        memcpy(str+myLen, chrs, otherLen*sizeof(char16_t));
        memcpy(str+myLen, chrs, otherLen*sizeof(char16_t));
@@ -276,8 +273,8 @@ status_t String16::insert(size_t pos, const char16_t* chrs, size_t len)
           len, myLen, String8(chrs, len).string());
           len, myLen, String8(chrs, len).string());
    #endif
    #endif


    SharedBuffer* buf =
    SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
            static_cast<SharedBuffer*>(editResize((myLen + len + 1) * sizeof(char16_t)));
        ->editResize((myLen+len+1)*sizeof(char16_t));
    if (buf) {
    if (buf) {
        char16_t* str = (char16_t*)buf->data();
        char16_t* str = (char16_t*)buf->data();
        if (pos < myLen) {
        if (pos < myLen) {
@@ -341,87 +338,23 @@ bool String16::contains(const char16_t* chrs) const
    return strstr16(mString, chrs) != nullptr;
    return strstr16(mString, chrs) != nullptr;
}
}


void* String16::edit() {
    SharedBuffer* buf;
    if (isStaticString()) {
        buf = static_cast<SharedBuffer*>(alloc((size() + 1) * sizeof(char16_t)));
        if (buf) {
            buf->acquire();
            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) {
            buf->acquire();
            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));
}

status_t String16::makeLower()
status_t String16::makeLower()
{
{
    const size_t N = size();
    const size_t N = size();
    const char16_t* str = string();
    const char16_t* str = string();
    char16_t* edited = nullptr;
    char16_t* edit = nullptr;
    for (size_t i=0; i<N; i++) {
    for (size_t i=0; i<N; i++) {
        const char16_t v = str[i];
        const char16_t v = str[i];
        if (v >= 'A' && v <= 'Z') {
        if (v >= 'A' && v <= 'Z') {
            if (!edited) {
            if (!edit) {
                SharedBuffer* buf = static_cast<SharedBuffer*>(edit());
                SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
                if (!buf) {
                if (!buf) {
                    return NO_MEMORY;
                    return NO_MEMORY;
                }
                }
                edited = (char16_t*)buf->data();
                edit = (char16_t*)buf->data();
                mString = str = edited;
                mString = str = edit;
            }
            }
            edited[i] = tolower((char)v);
            edit[i] = tolower((char)v);
        }
        }
    }
    }
    return OK;
    return OK;
@@ -431,18 +364,18 @@ status_t String16::replaceAll(char16_t replaceThis, char16_t withThis)
{
{
    const size_t N = size();
    const size_t N = size();
    const char16_t* str = string();
    const char16_t* str = string();
    char16_t* edited = nullptr;
    char16_t* edit = nullptr;
    for (size_t i=0; i<N; i++) {
    for (size_t i=0; i<N; i++) {
        if (str[i] == replaceThis) {
        if (str[i] == replaceThis) {
            if (!edited) {
            if (!edit) {
                SharedBuffer* buf = static_cast<SharedBuffer*>(edit());
                SharedBuffer* buf = SharedBuffer::bufferFromData(mString)->edit();
                if (!buf) {
                if (!buf) {
                    return NO_MEMORY;
                    return NO_MEMORY;
                }
                }
                edited = (char16_t*)buf->data();
                edit = (char16_t*)buf->data();
                mString = str = edited;
                mString = str = edit;
            }
            }
            edited[i] = withThis;
            edit[i] = withThis;
        }
        }
    }
    }
    return OK;
    return OK;
@@ -452,7 +385,7 @@ status_t String16::remove(size_t len, size_t begin)
{
{
    const size_t N = size();
    const size_t N = size();
    if (begin >= N) {
    if (begin >= N) {
        release();
        SharedBuffer::bufferFromData(mString)->release();
        mString = getEmptyString();
        mString = getEmptyString();
        return OK;
        return OK;
    }
    }
@@ -462,7 +395,8 @@ status_t String16::remove(size_t len, size_t begin)
    }
    }


    if (begin > 0) {
    if (begin > 0) {
        SharedBuffer* buf = static_cast<SharedBuffer*>(editResize((N + 1) * sizeof(char16_t)));
        SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
            ->editResize((N+1)*sizeof(char16_t));
        if (!buf) {
        if (!buf) {
            return NO_MEMORY;
            return NO_MEMORY;
        }
        }
@@ -470,7 +404,8 @@ status_t String16::remove(size_t len, size_t begin)
        memmove(str, str+begin, (N-begin+1)*sizeof(char16_t));
        memmove(str, str+begin, (N-begin+1)*sizeof(char16_t));
        mString = str;
        mString = str;
    }
    }
    SharedBuffer* buf = static_cast<SharedBuffer*>(editResize((len + 1) * sizeof(char16_t)));
    SharedBuffer* buf = SharedBuffer::bufferFromData(mString)
        ->editResize((len+1)*sizeof(char16_t));
    if (buf) {
    if (buf) {
        char16_t* str = (char16_t*)buf->data();
        char16_t* str = (char16_t*)buf->data();
        str[len] = 0;
        str[len] = 0;

libutils/String16_test.cpp

deleted100644 → 0
+0 −218
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2019 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/String8.h>

#include <gtest/gtest.h>

namespace android {

::testing::AssertionResult Char16_tStringEquals(const char16_t* a, const char16_t* b) {
    if (strcmp16(a, b) != 0) {
        return ::testing::AssertionFailure()
               << "\"" << String8(a).c_str() << "\" not equal to \"" << String8(b).c_str() << "\"";
    }
    return ::testing::AssertionSuccess();
}

#define EXPECT_STR16EQ(a, b) EXPECT_TRUE(Char16_tStringEquals(a, b))

TEST(String16Test, FromChar16_t) {
    String16 tmp(u"Verify me");
    EXPECT_STR16EQ(u"Verify me", tmp);
}

TEST(String16Test, FromChar16_tSized) {
    String16 tmp(u"Verify me", 7);
    EXPECT_STR16EQ(u"Verify ", tmp);
}

TEST(String16Test, FromChar) {
    String16 tmp("Verify me");
    EXPECT_STR16EQ(u"Verify me", tmp);
}

TEST(String16Test, FromCharSized) {
    String16 tmp("Verify me", 7);
    EXPECT_STR16EQ(u"Verify ", tmp);
}

TEST(String16Test, Copy) {
    String16 tmp("Verify me");
    String16 another = tmp;
    EXPECT_STR16EQ(u"Verify me", tmp);
    EXPECT_STR16EQ(u"Verify me", another);
}

TEST(String16Test, Move) {
    String16 tmp("Verify me");
    String16 another(std::move(tmp));
    EXPECT_STR16EQ(u"Verify me", another);
}

TEST(String16Test, Size) {
    String16 tmp("Verify me");
    EXPECT_EQ(9U, tmp.size());
}

TEST(String16Test, setTo) {
    String16 tmp("Verify me");
    tmp.setTo(u"New content");
    EXPECT_EQ(11U, tmp.size());
    EXPECT_STR16EQ(u"New content", tmp);
}

TEST(String16Test, Append) {
    String16 tmp("Verify me");
    tmp.append(String16("Hello"));
    EXPECT_EQ(14U, tmp.size());
    EXPECT_STR16EQ(u"Verify meHello", tmp);
}

TEST(String16Test, Insert) {
    String16 tmp("Verify me");
    tmp.insert(6, u"Insert");
    EXPECT_EQ(15U, tmp.size());
    EXPECT_STR16EQ(u"VerifyInsert me", tmp);
}

TEST(String16Test, Remove) {
    String16 tmp("Verify me");
    tmp.remove(2, 6);
    EXPECT_EQ(2U, tmp.size());
    EXPECT_STR16EQ(u" m", tmp);
}

TEST(String16Test, MakeLower) {
    String16 tmp("Verify Me!");
    tmp.makeLower();
    EXPECT_EQ(10U, tmp.size());
    EXPECT_STR16EQ(u"verify me!", tmp);
}

TEST(String16Test, ReplaceAll) {
    String16 tmp("Verify verify Verify");
    tmp.replaceAll(u'r', u'!');
    EXPECT_STR16EQ(u"Ve!ify ve!ify Ve!ify", tmp);
}

TEST(String16Test, Compare) {
    String16 tmp("Verify me");
    EXPECT_EQ(String16(u"Verify me"), tmp);
}

TEST(String16Test, StaticString) {
    String16 nonStaticString("NonStatic");
    StaticString16 staticString(u"Static");

    EXPECT_TRUE(staticString.isStaticString());
    EXPECT_FALSE(nonStaticString.isStaticString());
}

TEST(String16Test, StaticStringCopy) {
    StaticString16 tmp(u"Verify me");
    String16 another = tmp;
    EXPECT_STR16EQ(u"Verify me", tmp);
    EXPECT_STR16EQ(u"Verify me", another);
    EXPECT_TRUE(tmp.isStaticString());
    EXPECT_TRUE(another.isStaticString());
}

TEST(String16Test, StaticStringMove) {
    StaticString16 tmp(u"Verify me");
    String16 another(std::move(tmp));
    EXPECT_STR16EQ(u"Verify me", another);
    EXPECT_TRUE(another.isStaticString());
}

TEST(String16Test, StaticStringSize) {
    StaticString16 tmp(u"Verify me");
    EXPECT_EQ(9U, tmp.size());
}

TEST(String16Test, StaticStringSetTo) {
    StaticString16 tmp(u"Verify me");
    tmp.setTo(u"New content");
    EXPECT_EQ(11U, tmp.size());
    EXPECT_STR16EQ(u"New content", tmp);
    EXPECT_FALSE(tmp.isStaticString());
}

TEST(String16Test, StaticStringAppend) {
    StaticString16 tmp(u"Verify me");
    tmp.append(String16("Hello"));
    EXPECT_EQ(14U, tmp.size());
    EXPECT_STR16EQ(u"Verify meHello", tmp);
    EXPECT_FALSE(tmp.isStaticString());
}

TEST(String16Test, StaticStringInsert) {
    StaticString16 tmp(u"Verify me");
    tmp.insert(6, u"Insert");
    EXPECT_EQ(15U, tmp.size());
    EXPECT_STR16EQ(u"VerifyInsert me", tmp);
    EXPECT_FALSE(tmp.isStaticString());
}

TEST(String16Test, StaticStringRemove) {
    StaticString16 tmp(u"Verify me");
    tmp.remove(2, 6);
    EXPECT_EQ(2U, tmp.size());
    EXPECT_STR16EQ(u" m", tmp);
    EXPECT_FALSE(tmp.isStaticString());
}

TEST(String16Test, StaticStringMakeLower) {
    StaticString16 tmp(u"Verify me!");
    tmp.makeLower();
    EXPECT_EQ(10U, tmp.size());
    EXPECT_STR16EQ(u"verify me!", tmp);
    EXPECT_FALSE(tmp.isStaticString());
}

TEST(String16Test, StaticStringReplaceAll) {
    StaticString16 tmp(u"Verify verify Verify");
    tmp.replaceAll(u'r', u'!');
    EXPECT_STR16EQ(u"Ve!ify ve!ify Ve!ify", tmp);
    EXPECT_FALSE(tmp.isStaticString());
}

TEST(String16Test, StaticStringCompare) {
    StaticString16 tmp(u"Verify me");
    EXPECT_EQ(String16(u"Verify me"), tmp);
}

TEST(String16Test, StringSetToStaticString) {
    StaticString16 tmp(u"Verify me");
    String16 another(u"nonstatic");
    another = tmp;
    EXPECT_STR16EQ(u"Verify me", tmp);
    EXPECT_STR16EQ(u"Verify me", another);
}

TEST(String16Test, StringMoveFromStaticString) {
    StaticString16 tmp(u"Verify me");
    String16 another(std::move(tmp));
    EXPECT_STR16EQ(u"Verify me", another);
}

TEST(String16Test, EmptyStringIsStatic) {
    String16 tmp("");
    EXPECT_TRUE(tmp.isStaticString());
}

}  // namespace android
Loading