diff options
| author | 2015-07-06 18:09:18 -0700 | |
|---|---|---|
| committer | 2015-09-15 17:42:15 +0000 | |
| commit | 90959887e9a61ff83097b2789f8b3243ad817dec (patch) | |
| tree | 638cf706e8d96bef9e86e7446f6cf94ff90a49ef | |
| parent | f8def4d40b100c114a19c7093040b3077039d30b (diff) | |
AAPT2: Fix issue with parsing escape sequences when the parser only gives us part at a time
Change-Id: I671955f350b57577a991dd88f6a7319ec0b36c07
| -rw-r--r-- | tools/aapt2/Logger.h | 7 | ||||
| -rw-r--r-- | tools/aapt2/StringPiece.h | 5 | ||||
| -rw-r--r-- | tools/aapt2/Util.cpp | 93 | ||||
| -rw-r--r-- | tools/aapt2/Util.h | 1 | ||||
| -rw-r--r-- | tools/aapt2/Util_test.cpp | 7 |
5 files changed, 60 insertions, 53 deletions
diff --git a/tools/aapt2/Logger.h b/tools/aapt2/Logger.h index 1d437ebe6492..eed58b83ffc4 100644 --- a/tools/aapt2/Logger.h +++ b/tools/aapt2/Logger.h @@ -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 { @@ -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 diff --git a/tools/aapt2/StringPiece.h b/tools/aapt2/StringPiece.h index e2a1597caeda..8cbdeae5e892 100644 --- a/tools/aapt2/StringPiece.h +++ b/tools/aapt2/StringPiece.h @@ -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 diff --git a/tools/aapt2/Util.cpp b/tools/aapt2/Util.cpp index 03ecd1aca310..ca352e0e9b61 100644 --- a/tools/aapt2/Util.cpp +++ b/tools/aapt2/Util.cpp @@ -175,7 +175,51 @@ StringBuilder& StringBuilder::append(const StringPiece16& str) { const char16_t* start = str.begin(); const char16_t* current = start; while (current != end) { - if (*current == u'"') { + if (mLastCharWasEscape) { + switch (*current) { + case u't': + mStr += u'\t'; + break; + case u'n': + mStr += u'\n'; + break; + case u'#': + mStr += u'#'; + break; + case u'@': + mStr += u'@'; + break; + case u'?': + mStr += u'?'; + break; + case u'"': + mStr += u'"'; + break; + case u'\'': + mStr += u'\''; + break; + case u'\\': + mStr += u'\\'; + break; + case u'u': { + current++; + Maybe<char16_t> c = parseUnicodeCodepoint(¤t, end); + if (!c) { + mError = "invalid unicode escape sequence"; + return *this; + } + mStr += c.value(); + current -= 1; + break; + } + + default: + // 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 @@ -208,52 +252,7 @@ StringBuilder& StringBuilder::append(const StringPiece16& str) { } mStr.append(start, current - start); start = current + 1; - - current++; - if (current != end) { - switch (*current) { - case u't': - mStr += u'\t'; - break; - case u'n': - mStr += u'\n'; - break; - case u'#': - mStr += u'#'; - break; - case u'@': - mStr += u'@'; - break; - case u'?': - mStr += u'?'; - break; - case u'"': - mStr += u'"'; - break; - case u'\'': - mStr += u'\''; - break; - case u'\\': - mStr += u'\\'; - break; - case u'u': { - current++; - Maybe<char16_t> c = parseUnicodeCodepoint(¤t, end); - if (!c) { - mError = "invalid unicode escape sequence"; - return *this; - } - mStr += c.value(); - current -= 1; - break; - } - - default: - // Ignore. - break; - } - start = current + 1; - } + mLastCharWasEscape = true; } else if (!mQuote) { // This is not quoted text, so look for whitespace. if (isspace16(*current)) { diff --git a/tools/aapt2/Util.h b/tools/aapt2/Util.h index 9cdb152bf41f..7ec6b030fd85 100644 --- a/tools/aapt2/Util.h +++ b/tools/aapt2/Util.h @@ -162,6 +162,7 @@ private: std::u16string mStr; bool mQuote = false; bool mTrailingSpace = false; + bool mLastCharWasEscape = false; std::string mError; }; diff --git a/tools/aapt2/Util_test.cpp b/tools/aapt2/Util_test.cpp index 0b08d240cad3..92f2a1c0f1d1 100644 --- a/tools/aapt2/Util_test.cpp +++ b/tools/aapt2/Util_test.cpp @@ -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 ") |