summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Lillqvist <tml@collabora.com>2021-01-15 03:03:41 +0200
committerTor Lillqvist <tml@collabora.com>2021-01-16 15:08:07 +0100
commit5db3e8596254cf5a2e1c66e2d9dd7e5a347fcc03 (patch)
tree2b68baa1ec32f72427db9ce674f85bcae789a752
parent2b3b94f962c10b85df4506921faf59c6c5972c6e (diff)
Make JsonWriter::writeEscapedOUString() handle surrogate pairs properly
It is wrong to iterate over UTF-16 code units one by one. We have OUString::iterateCodePoints() to iterate over Unicode code points. The two UTF-16 code units of a surrogate pair (for a non-BMP code point) should not be encoded separately to UTF-8 bytes. It is the code point that should be encoded (to four bytes). Change-Id: Ica4341308deb6618c9c2da8dcee8a11ef4e8238d Reviewed-on: https://gerrit.libreoffice.org/c/core/+/109318 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk> Reviewed-by: Tor Lillqvist <tml@collabora.com> Reviewed-on: https://gerrit.libreoffice.org/c/core/+/109425 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
-rw-r--r--tools/source/misc/json_writer.cxx18
1 files changed, 15 insertions, 3 deletions
diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx
index be891ef18423..d1e1997320a1 100644
--- a/tools/source/misc/json_writer.cxx
+++ b/tools/source/misc/json_writer.cxx
@@ -134,9 +134,10 @@ void JsonWriter::put(const char* pPropName, const OUString& rPropVal)
mPos += 4;
// Convert from UTF-16 to UTF-8 and perform escaping
- for (int i = 0; i < rPropVal.getLength(); ++i)
+ sal_Int32 i = 0;
+ while (i < rPropVal.getLength())
{
- sal_Unicode ch = rPropVal[i];
+ sal_uInt32 ch = rPropVal.iterateCodePoints(&i);
if (ch == '\\')
{
*mPos = static_cast<char>(ch);
@@ -163,7 +164,7 @@ void JsonWriter::put(const char* pPropName, const OUString& rPropVal)
*mPos = 0x80 | (ch & 0x3F); /* 10xxxxxx */
++mPos;
}
- else
+ else if (ch <= 0xFFFF)
{
*mPos = 0xE0 | (ch >> 12); /* 1110xxxx */
++mPos;
@@ -172,6 +173,17 @@ void JsonWriter::put(const char* pPropName, const OUString& rPropVal)
*mPos = 0x80 | (ch & 0x3F); /* 10xxxxxx */
++mPos;
}
+ else
+ {
+ *mPos = 0xF0 | (ch >> 18); /* 11110xxx */
+ ++mPos;
+ *mPos = 0x80 | ((ch >> 12) & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ *mPos = 0x80 | ((ch >> 6) & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ *mPos = 0x80 | (ch & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ }
}
*mPos = '"';