Loading tools/aapt2/Logger.h +1 −6 Original line number Diff line number Diff line Loading @@ -18,11 +18,11 @@ #define AAPT_LOGGER_H #include "Source.h" #include "StringPiece.h" #include <memory> #include <ostream> #include <string> #include <utils/String8.h> namespace aapt { Loading Loading @@ -71,11 +71,6 @@ private: Source mSource; }; inline ::std::ostream& operator<<(::std::ostream& out, const std::u16string& str) { android::String8 utf8(str.data(), str.size()); return out.write(utf8.string(), utf8.size()); } } // namespace aapt #endif // AAPT_LOGGER_H tools/aapt2/StringPiece.h +5 −0 Original line number Diff line number Diff line Loading @@ -229,4 +229,9 @@ inline ::std::ostream& operator<<(::std::ostream& out, const BasicStringPiece<ch } // namespace aapt inline ::std::ostream& operator<<(::std::ostream& out, const std::u16string& str) { android::String8 utf8(str.data(), str.size()); return out.write(utf8.string(), utf8.size()); } #endif // AAPT_STRING_PIECE_H tools/aapt2/Util.cpp +46 −47 Original line number Diff line number Diff line Loading @@ -175,42 +175,7 @@ StringBuilder& StringBuilder::append(const StringPiece16& str) { const char16_t* start = str.begin(); const char16_t* current = start; while (current != end) { if (*current == u'"') { if (!mQuote && mTrailingSpace) { // We found an opening quote, and we have // trailing space, so we should append that // space now. if (mTrailingSpace) { // We had trailing whitespace, so // replace with a single space. if (!mStr.empty()) { mStr += u' '; } mTrailingSpace = false; } } mQuote = !mQuote; mStr.append(start, current - start); start = current + 1; } else if (*current == u'\'' && !mQuote) { // This should be escaped. mError = "unescaped apostrophe"; return *this; } else if (*current == u'\\') { // This is an escape sequence, convert to the real value. if (!mQuote && mTrailingSpace) { // We had trailing whitespace, so // replace with a single space. if (!mStr.empty()) { mStr += u' '; } mTrailingSpace = false; } mStr.append(start, current - start); start = current + 1; current++; if (current != end) { if (mLastCharWasEscape) { switch (*current) { case u't': mStr += u'\t'; Loading Loading @@ -252,8 +217,42 @@ StringBuilder& StringBuilder::append(const StringPiece16& str) { // Ignore. break; } mLastCharWasEscape = false; start = current + 1; } else if (*current == u'"') { if (!mQuote && mTrailingSpace) { // We found an opening quote, and we have // trailing space, so we should append that // space now. if (mTrailingSpace) { // We had trailing whitespace, so // replace with a single space. if (!mStr.empty()) { mStr += u' '; } mTrailingSpace = false; } } mQuote = !mQuote; mStr.append(start, current - start); start = current + 1; } else if (*current == u'\'' && !mQuote) { // This should be escaped. mError = "unescaped apostrophe"; return *this; } else if (*current == u'\\') { // This is an escape sequence, convert to the real value. if (!mQuote && mTrailingSpace) { // We had trailing whitespace, so // replace with a single space. if (!mStr.empty()) { mStr += u' '; } mTrailingSpace = false; } mStr.append(start, current - start); start = current + 1; mLastCharWasEscape = true; } else if (!mQuote) { // This is not quoted text, so look for whitespace. if (isspace16(*current)) { Loading tools/aapt2/Util.h +1 −0 Original line number Diff line number Diff line Loading @@ -162,6 +162,7 @@ private: std::u16string mStr; bool mQuote = false; bool mTrailingSpace = false; bool mLastCharWasEscape = false; std::string mError; }; Loading tools/aapt2/Util_test.cpp +7 −0 Original line number Diff line number Diff line Loading @@ -38,6 +38,13 @@ TEST(UtilTest, StringStartsWith) { EXPECT_TRUE(util::stringStartsWith<char>("hello.xml", "he")); } TEST(UtilTest, StringBuilderSplitEscapeSequence) { EXPECT_EQ(StringPiece16(u"this is a new\nline."), util::StringBuilder().append(u"this is a new\\") .append(u"nline.") .str()); } TEST(UtilTest, StringBuilderWhitespaceRemoval) { EXPECT_EQ(StringPiece16(u"hey guys this is so cool"), util::StringBuilder().append(u" hey guys ") Loading Loading
tools/aapt2/Logger.h +1 −6 Original line number Diff line number Diff line Loading @@ -18,11 +18,11 @@ #define AAPT_LOGGER_H #include "Source.h" #include "StringPiece.h" #include <memory> #include <ostream> #include <string> #include <utils/String8.h> namespace aapt { Loading Loading @@ -71,11 +71,6 @@ private: Source mSource; }; inline ::std::ostream& operator<<(::std::ostream& out, const std::u16string& str) { android::String8 utf8(str.data(), str.size()); return out.write(utf8.string(), utf8.size()); } } // namespace aapt #endif // AAPT_LOGGER_H
tools/aapt2/StringPiece.h +5 −0 Original line number Diff line number Diff line Loading @@ -229,4 +229,9 @@ inline ::std::ostream& operator<<(::std::ostream& out, const BasicStringPiece<ch } // namespace aapt inline ::std::ostream& operator<<(::std::ostream& out, const std::u16string& str) { android::String8 utf8(str.data(), str.size()); return out.write(utf8.string(), utf8.size()); } #endif // AAPT_STRING_PIECE_H
tools/aapt2/Util.cpp +46 −47 Original line number Diff line number Diff line Loading @@ -175,42 +175,7 @@ StringBuilder& StringBuilder::append(const StringPiece16& str) { const char16_t* start = str.begin(); const char16_t* current = start; while (current != end) { if (*current == u'"') { if (!mQuote && mTrailingSpace) { // We found an opening quote, and we have // trailing space, so we should append that // space now. if (mTrailingSpace) { // We had trailing whitespace, so // replace with a single space. if (!mStr.empty()) { mStr += u' '; } mTrailingSpace = false; } } mQuote = !mQuote; mStr.append(start, current - start); start = current + 1; } else if (*current == u'\'' && !mQuote) { // This should be escaped. mError = "unescaped apostrophe"; return *this; } else if (*current == u'\\') { // This is an escape sequence, convert to the real value. if (!mQuote && mTrailingSpace) { // We had trailing whitespace, so // replace with a single space. if (!mStr.empty()) { mStr += u' '; } mTrailingSpace = false; } mStr.append(start, current - start); start = current + 1; current++; if (current != end) { if (mLastCharWasEscape) { switch (*current) { case u't': mStr += u'\t'; Loading Loading @@ -252,8 +217,42 @@ StringBuilder& StringBuilder::append(const StringPiece16& str) { // Ignore. break; } mLastCharWasEscape = false; start = current + 1; } else if (*current == u'"') { if (!mQuote && mTrailingSpace) { // We found an opening quote, and we have // trailing space, so we should append that // space now. if (mTrailingSpace) { // We had trailing whitespace, so // replace with a single space. if (!mStr.empty()) { mStr += u' '; } mTrailingSpace = false; } } mQuote = !mQuote; mStr.append(start, current - start); start = current + 1; } else if (*current == u'\'' && !mQuote) { // This should be escaped. mError = "unescaped apostrophe"; return *this; } else if (*current == u'\\') { // This is an escape sequence, convert to the real value. if (!mQuote && mTrailingSpace) { // We had trailing whitespace, so // replace with a single space. if (!mStr.empty()) { mStr += u' '; } mTrailingSpace = false; } mStr.append(start, current - start); start = current + 1; mLastCharWasEscape = true; } else if (!mQuote) { // This is not quoted text, so look for whitespace. if (isspace16(*current)) { Loading
tools/aapt2/Util.h +1 −0 Original line number Diff line number Diff line Loading @@ -162,6 +162,7 @@ private: std::u16string mStr; bool mQuote = false; bool mTrailingSpace = false; bool mLastCharWasEscape = false; std::string mError; }; Loading
tools/aapt2/Util_test.cpp +7 −0 Original line number Diff line number Diff line Loading @@ -38,6 +38,13 @@ TEST(UtilTest, StringStartsWith) { EXPECT_TRUE(util::stringStartsWith<char>("hello.xml", "he")); } TEST(UtilTest, StringBuilderSplitEscapeSequence) { EXPECT_EQ(StringPiece16(u"this is a new\nline."), util::StringBuilder().append(u"this is a new\\") .append(u"nline.") .str()); } TEST(UtilTest, StringBuilderWhitespaceRemoval) { EXPECT_EQ(StringPiece16(u"hey guys this is so cool"), util::StringBuilder().append(u" hey guys ") Loading