Changed AAPT2 to abide by AAPT resource whitespace triming.
Bug: b/74331008
Test: Created tests in ResourceParser_test.cpp
Change-Id: Id7b387692b795774cd77452ca8cf06a8447bf3be
diff --git a/tools/aapt2/ResourceParser_test.cpp b/tools/aapt2/ResourceParser_test.cpp
index c98c0b9..fc1aeaa 100644
--- a/tools/aapt2/ResourceParser_test.cpp
+++ b/tools/aapt2/ResourceParser_test.cpp
@@ -164,6 +164,39 @@
EXPECT_THAT(*str, StrValueEq(" This is what I think "));
}
+TEST_F(ResourceParserTest, ParseStringTruncateASCII) {
+ // Tuncate leading and trailing whitespace
+ EXPECT_TRUE(TestParse(R"(<string name="foo"> Hello </string>)"));
+
+ String* str = test::GetValue<String>(&table_, "string/foo");
+ ASSERT_THAT(str, NotNull());
+ EXPECT_THAT(*str->value, StrEq("Hello"));
+ EXPECT_THAT(str->untranslatable_sections, IsEmpty());
+
+ // AAPT does not truncate unicode whitespace
+ EXPECT_TRUE(TestParse(R"(<string name="foo2">\u0020\Hello\u0020</string>)"));
+
+ str = test::GetValue<String>(&table_, "string/foo2");
+ ASSERT_THAT(str, NotNull());
+ EXPECT_THAT(*str->value, StrEq(" Hello "));
+ EXPECT_THAT(str->untranslatable_sections, IsEmpty());
+
+ // Preserve non-ASCII whitespace including extended ASCII characters
+ EXPECT_TRUE(TestParse(R"(<string name="foo3"> Hello </string>)"));
+
+ str = test::GetValue<String>(&table_, "string/foo3");
+ ASSERT_THAT(str, NotNull());
+ EXPECT_THAT(*str->value, StrEq("\xC2\xA0Hello\xC2\xA0"));
+ EXPECT_THAT(str->untranslatable_sections, IsEmpty());
+
+ EXPECT_TRUE(TestParse(R"(<string name="foo4">2005年6月1日</string>)"));
+
+ str = test::GetValue<String>(&table_, "string/foo4");
+ ASSERT_THAT(str, NotNull());
+ EXPECT_THAT(*str->value, StrEq("2005年6月1日"));
+ EXPECT_THAT(str->untranslatable_sections, IsEmpty());
+}
+
TEST_F(ResourceParserTest, ParseStyledStringWithWhitespace) {
std::string input = R"(<string name="foo"> <b> My <i> favorite</i> string </b> </string>)";
ASSERT_TRUE(TestParse(input));
diff --git a/tools/aapt2/ResourceUtils.cpp b/tools/aapt2/ResourceUtils.cpp
index 8fc3d65..560077c 100644
--- a/tools/aapt2/ResourceUtils.cpp
+++ b/tools/aapt2/ResourceUtils.cpp
@@ -30,7 +30,6 @@
#include "util/Files.h"
#include "util/Util.h"
-using ::aapt::text::IsWhitespace;
using ::aapt::text::Utf8Iterator;
using ::android::StringPiece;
using ::android::StringPiece16;
@@ -807,7 +806,7 @@
Utf8Iterator iter(text);
while (iter.HasNext()) {
char32_t codepoint = iter.Next();
- if (!quote_ && text::IsWhitespace(codepoint)) {
+ if (!quote_ && iswspace(codepoint)) {
if (!last_codepoint_was_space_) {
// Emit a space if it's the first.
xml_string_.text += ' ';