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

Commit b16e7469 authored by Adam Lesinski's avatar Adam Lesinski Committed by Android (Google) Code Review
Browse files

Merge "AAPT2: Fix issue with parsing escape sequences when the parser only gives us part at a time"

parents 0ba68c38 90959887
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -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
+5 −0
Original line number Diff line number Diff line
@@ -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
+46 −47
Original line number Diff line number Diff line
@@ -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';
@@ -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)) {
+1 −0
Original line number Diff line number Diff line
@@ -162,6 +162,7 @@ private:
    std::u16string mStr;
    bool mQuote = false;
    bool mTrailingSpace = false;
    bool mLastCharWasEscape = false;
    std::string mError;
};

+7 −0
Original line number Diff line number Diff line
@@ -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 ")