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
diff --git a/tools/aapt2/util/Util.cpp b/tools/aapt2/util/Util.cpp
index e743247..b0bec62 100644
--- a/tools/aapt2/util/Util.cpp
+++ b/tools/aapt2/util/Util.cpp
@@ -314,6 +314,9 @@
         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 @@
         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 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;
 }