summaryrefslogtreecommitdiff
path: root/tools/aapt2/util/Util.cpp
diff options
context:
space:
mode:
authorAdam Lesinski <adamlesinski@google.com>2016-09-07 13:45:13 -0700
committerAdam Lesinski <adamlesinski@google.com>2016-09-07 13:45:13 -0700
commit8c3f31f022f7e094fd227ef0c2987e0846cb3e43 (patch)
treed9550e3643a3eea0d0e323d3234edb52274a96fc /tools/aapt2/util/Util.cpp
parent43321e02bc3553bdd7ea5f2af823080414d59b54 (diff)
AAPT2: Fix issue with styled string indices
Styled strings use spans to denote which part is styled (<b>, <i>, etc). Spans are simply a range of indices into the original string. In Java, we use String and its internal representation, meaning we must encode the indices using UTF16 lengths. When the internal AAPT2 representation of strings switched to UTF8, the indices also began to index into the UTF8 string. This change reverts the indices to use UTF16 lengths. Bug:31170115 Change-Id: I07b8b5b67d2542c7e0a855b601cdbd3ac4ebffb0
Diffstat (limited to 'tools/aapt2/util/Util.cpp')
-rw-r--r--tools/aapt2/util/Util.cpp20
1 files changed, 15 insertions, 5 deletions
diff --git a/tools/aapt2/util/Util.cpp b/tools/aapt2/util/Util.cpp
index e743247be8a9..b0bec624cc9c 100644
--- a/tools/aapt2/util/Util.cpp
+++ b/tools/aapt2/util/Util.cpp
@@ -314,6 +314,9 @@ StringBuilder& StringBuilder::append(const StringPiece& str) {
return *this;
}
+ // Where the new data will be appended to.
+ size_t newDataIndex = mStr.size();
+
const char* const end = str.end();
const char* start = str.begin();
const char* current = start;
@@ -422,6 +425,16 @@ StringBuilder& StringBuilder::append(const StringPiece& str) {
current++;
}
mStr.append(start, end - start);
+
+ // Accumulate the added string's UTF-16 length.
+ ssize_t len = utf8_to_utf16_length(
+ reinterpret_cast<const uint8_t*>(mStr.data()) + newDataIndex,
+ mStr.size() - newDataIndex);
+ if (len < 0) {
+ mError = "invalid unicode code point";
+ return *this;
+ }
+ mUtf16Len += len;
return *this;
}
@@ -434,11 +447,8 @@ std::u16string utf8ToUtf16(const StringPiece& utf8) {
std::u16string utf16;
utf16.resize(utf16Length);
- utf8_to_utf16(
- reinterpret_cast<const uint8_t*>(utf8.data()),
- utf8.length(),
- &*utf16.begin(),
- (size_t) utf16Length + 1);
+ utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(),
+ &*utf16.begin(), utf16Length + 1);
return utf16;
}