From d5083f6f6b9bc76bbe64052bcec639eee752a321 Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Mon, 16 Jan 2017 15:07:21 -0800 Subject: Move StringPiece to libandroidfw libandroidfw needs to make use of StringPiece, so move it to libandroidfw and update all code referencing StringPiece in aapt2. Test: make libandroidfw_tests libaapt2_tests Change-Id: I68d7f0fc7c651b048d9d1f5e7971f10ef5349fa1 --- libs/androidfw/include/androidfw/StringPiece.h | 279 +++++++++++++++++++++++++ libs/androidfw/tests/Android.mk | 1 + libs/androidfw/tests/StringPiece_test.cpp | 95 +++++++++ 3 files changed, 375 insertions(+) create mode 100644 libs/androidfw/include/androidfw/StringPiece.h create mode 100644 libs/androidfw/tests/StringPiece_test.cpp (limited to 'libs') diff --git a/libs/androidfw/include/androidfw/StringPiece.h b/libs/androidfw/include/androidfw/StringPiece.h new file mode 100644 index 000000000000..c9effd1a5112 --- /dev/null +++ b/libs/androidfw/include/androidfw/StringPiece.h @@ -0,0 +1,279 @@ +/* + * Copyright (C) 2015 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. + */ + +#ifndef ANDROIDFW_STRING_PIECE_H +#define ANDROIDFW_STRING_PIECE_H + +#include +#include + +#include "utils/JenkinsHash.h" +#include "utils/Unicode.h" + +namespace android { + +// Read only wrapper around basic C strings. Prevents excessive copying. +// StringPiece does not own the data it is wrapping. The lifetime of the underlying +// data must outlive this StringPiece. +// +// WARNING: When creating from std::basic_string<>, moving the original +// std::basic_string<> will invalidate the data held in a BasicStringPiece<>. +// BasicStringPiece<> should only be used transitively. +template +class BasicStringPiece { + public: + using const_iterator = const TChar*; + using difference_type = size_t; + + // End of string marker. + constexpr static const size_t npos = static_cast(-1); + + BasicStringPiece(); + BasicStringPiece(const BasicStringPiece& str); + BasicStringPiece(const std::basic_string& str); // NOLINT(implicit) + BasicStringPiece(const TChar* str); // NOLINT(implicit) + BasicStringPiece(const TChar* str, size_t len); + + BasicStringPiece& operator=(const BasicStringPiece& rhs); + BasicStringPiece& assign(const TChar* str, size_t len); + + BasicStringPiece substr(size_t start, size_t len = npos) const; + BasicStringPiece substr(BasicStringPiece::const_iterator begin, + BasicStringPiece::const_iterator end) const; + + const TChar* data() const; + size_t length() const; + size_t size() const; + bool empty() const; + std::basic_string to_string() const; + + bool contains(const BasicStringPiece& rhs) const; + int compare(const BasicStringPiece& rhs) const; + bool operator<(const BasicStringPiece& rhs) const; + bool operator>(const BasicStringPiece& rhs) const; + bool operator==(const BasicStringPiece& rhs) const; + bool operator!=(const BasicStringPiece& rhs) const; + + const_iterator begin() const; + const_iterator end() const; + + private: + const TChar* data_; + size_t length_; +}; + +using StringPiece = BasicStringPiece; +using StringPiece16 = BasicStringPiece; + +// +// BasicStringPiece implementation. +// + +template +constexpr const size_t BasicStringPiece::npos; + +template +inline BasicStringPiece::BasicStringPiece() : data_(nullptr), length_(0) {} + +template +inline BasicStringPiece::BasicStringPiece(const BasicStringPiece& str) + : data_(str.data_), length_(str.length_) {} + +template +inline BasicStringPiece::BasicStringPiece(const std::basic_string& str) + : data_(str.data()), length_(str.length()) {} + +template <> +inline BasicStringPiece::BasicStringPiece(const char* str) + : data_(str), length_(str != nullptr ? strlen(str) : 0) {} + +template <> +inline BasicStringPiece::BasicStringPiece(const char16_t* str) + : data_(str), length_(str != nullptr ? strlen16(str) : 0) {} + +template +inline BasicStringPiece::BasicStringPiece(const TChar* str, size_t len) + : data_(str), length_(len) {} + +template +inline BasicStringPiece& BasicStringPiece::operator=( + const BasicStringPiece& rhs) { + data_ = rhs.data_; + length_ = rhs.length_; + return *this; +} + +template +inline BasicStringPiece& BasicStringPiece::assign(const TChar* str, size_t len) { + data_ = str; + length_ = len; + return *this; +} + +template +inline BasicStringPiece BasicStringPiece::substr(size_t start, size_t len) const { + if (len == npos) { + len = length_ - start; + } + + if (start > length_ || start + len > length_) { + return BasicStringPiece(); + } + return BasicStringPiece(data_ + start, len); +} + +template +inline BasicStringPiece BasicStringPiece::substr( + BasicStringPiece::const_iterator begin, + BasicStringPiece::const_iterator end) const { + return BasicStringPiece(begin, end - begin); +} + +template +inline const TChar* BasicStringPiece::data() const { + return data_; +} + +template +inline size_t BasicStringPiece::length() const { + return length_; +} + +template +inline size_t BasicStringPiece::size() const { + return length_; +} + +template +inline bool BasicStringPiece::empty() const { + return length_ == 0; +} + +template +inline std::basic_string BasicStringPiece::to_string() const { + return std::basic_string(data_, length_); +} + +template <> +inline bool BasicStringPiece::contains(const BasicStringPiece& rhs) const { + if (!data_ || !rhs.data_) { + return false; + } + if (rhs.length_ > length_) { + return false; + } + return strstr(data_, rhs.data_) != nullptr; +} + +template <> +inline int BasicStringPiece::compare(const BasicStringPiece& rhs) const { + const char nullStr = '\0'; + const char* b1 = data_ != nullptr ? data_ : &nullStr; + const char* e1 = b1 + length_; + const char* b2 = rhs.data_ != nullptr ? rhs.data_ : &nullStr; + const char* e2 = b2 + rhs.length_; + + while (b1 < e1 && b2 < e2) { + const int d = static_cast(*b1++) - static_cast(*b2++); + if (d) { + return d; + } + } + return static_cast(length_ - rhs.length_); +} + +inline ::std::ostream& operator<<(::std::ostream& out, const BasicStringPiece& str) { + const ssize_t result_len = utf16_to_utf8_length(str.data(), str.size()); + if (result_len < 0) { + // Empty string. + return out; + } + + std::string result; + result.resize(static_cast(result_len)); + utf16_to_utf8(str.data(), str.length(), &*result.begin(), static_cast(result_len) + 1); + return out << result; +} + +template <> +inline bool BasicStringPiece::contains(const BasicStringPiece& rhs) const { + if (!data_ || !rhs.data_) { + return false; + } + if (rhs.length_ > length_) { + return false; + } + return strstr16(data_, rhs.data_) != nullptr; +} + +template <> +inline int BasicStringPiece::compare(const BasicStringPiece& rhs) const { + const char16_t nullStr = u'\0'; + const char16_t* b1 = data_ != nullptr ? data_ : &nullStr; + const char16_t* b2 = rhs.data_ != nullptr ? rhs.data_ : &nullStr; + return strzcmp16(b1, length_, b2, rhs.length_); +} + +template +inline bool BasicStringPiece::operator<(const BasicStringPiece& rhs) const { + return compare(rhs) < 0; +} + +template +inline bool BasicStringPiece::operator>(const BasicStringPiece& rhs) const { + return compare(rhs) > 0; +} + +template +inline bool BasicStringPiece::operator==(const BasicStringPiece& rhs) const { + return compare(rhs) == 0; +} + +template +inline bool BasicStringPiece::operator!=(const BasicStringPiece& rhs) const { + return compare(rhs) != 0; +} + +template +inline typename BasicStringPiece::const_iterator BasicStringPiece::begin() const { + return data_; +} + +template +inline typename BasicStringPiece::const_iterator BasicStringPiece::end() const { + return data_ + length_; +} + +inline ::std::ostream& operator<<(::std::ostream& out, const BasicStringPiece& str) { + return out.write(str.data(), str.size()); +} + +} // namespace android + +namespace std { + +template +struct hash> { + size_t operator()(const android::BasicStringPiece& str) const { + uint32_t hashCode = android::JenkinsHashMixBytes( + 0, reinterpret_cast(str.data()), sizeof(TChar) * str.size()); + return static_cast(hashCode); + } +}; + +} // namespace std + +#endif // ANDROIDFW_STRING_PIECE_H diff --git a/libs/androidfw/tests/Android.mk b/libs/androidfw/tests/Android.mk index d91a1338c848..2f394c457da1 100644 --- a/libs/androidfw/tests/Android.mk +++ b/libs/androidfw/tests/Android.mk @@ -32,6 +32,7 @@ testFiles := \ Main.cpp \ ResTable_test.cpp \ Split_test.cpp \ + StringPiece_test.cpp \ TestHelpers.cpp \ Theme_test.cpp \ TypeWrappers_test.cpp \ diff --git a/libs/androidfw/tests/StringPiece_test.cpp b/libs/androidfw/tests/StringPiece_test.cpp new file mode 100644 index 000000000000..316a5c1bf40e --- /dev/null +++ b/libs/androidfw/tests/StringPiece_test.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2015 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 "androidfw/StringPiece.h" + +#include +#include +#include + +#include "TestHelpers.h" + +namespace android { + +TEST(StringPieceTest, CompareNonNullTerminatedPiece) { + StringPiece a("hello world", 5); + StringPiece b("hello moon", 5); + EXPECT_EQ(a, b); + + StringPiece16 a16(u"hello world", 5); + StringPiece16 b16(u"hello moon", 5); + EXPECT_EQ(a16, b16); +} + +TEST(StringPieceTest, PiecesHaveCorrectSortOrder) { + std::string testing("testing"); + std::string banana("banana"); + std::string car("car"); + + EXPECT_TRUE(StringPiece(testing) > banana); + EXPECT_TRUE(StringPiece(testing) > car); + EXPECT_TRUE(StringPiece(banana) < testing); + EXPECT_TRUE(StringPiece(banana) < car); + EXPECT_TRUE(StringPiece(car) < testing); + EXPECT_TRUE(StringPiece(car) > banana); +} + +TEST(StringPieceTest, PiecesHaveCorrectSortOrderUtf8) { + std::string testing("testing"); + std::string banana("banana"); + std::string car("car"); + + EXPECT_TRUE(StringPiece(testing) > banana); + EXPECT_TRUE(StringPiece(testing) > car); + EXPECT_TRUE(StringPiece(banana) < testing); + EXPECT_TRUE(StringPiece(banana) < car); + EXPECT_TRUE(StringPiece(car) < testing); + EXPECT_TRUE(StringPiece(car) > banana); +} + +TEST(StringPieceTest, ContainsOtherStringPiece) { + StringPiece text("I am a leaf on the wind."); + StringPiece start_needle("I am"); + StringPiece end_needle("wind."); + StringPiece middle_needle("leaf"); + StringPiece empty_needle(""); + StringPiece missing_needle("soar"); + StringPiece long_needle("This string is longer than the text."); + + EXPECT_TRUE(text.contains(start_needle)); + EXPECT_TRUE(text.contains(end_needle)); + EXPECT_TRUE(text.contains(middle_needle)); + EXPECT_TRUE(text.contains(empty_needle)); + EXPECT_FALSE(text.contains(missing_needle)); + EXPECT_FALSE(text.contains(long_needle)); + + StringPiece16 text16(u"I am a leaf on the wind."); + StringPiece16 start_needle16(u"I am"); + StringPiece16 end_needle16(u"wind."); + StringPiece16 middle_needle16(u"leaf"); + StringPiece16 empty_needle16(u""); + StringPiece16 missing_needle16(u"soar"); + StringPiece16 long_needle16(u"This string is longer than the text."); + + EXPECT_TRUE(text16.contains(start_needle16)); + EXPECT_TRUE(text16.contains(end_needle16)); + EXPECT_TRUE(text16.contains(middle_needle16)); + EXPECT_TRUE(text16.contains(empty_needle16)); + EXPECT_FALSE(text16.contains(missing_needle16)); + EXPECT_FALSE(text16.contains(long_needle16)); +} + +} // namespace android -- cgit v1.2.3-59-g8ed1b