summaryrefslogtreecommitdiff
path: root/src/support/string.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/support/string.cpp')
-rw-r--r--src/support/string.cpp41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/support/string.cpp b/src/support/string.cpp
index 68249f51e..31d0e9170 100644
--- a/src/support/string.cpp
+++ b/src/support/string.cpp
@@ -213,7 +213,8 @@ std::optional<uint16_t> takeWTF16CodeUnit(std::string_view& str) {
return u;
}
-std::optional<uint32_t> takeWTF16CodePoint(std::string_view& str) {
+std::optional<uint32_t> takeWTF16CodePoint(std::string_view& str,
+ bool allowWTF = true) {
auto u = takeWTF16CodeUnit(str);
if (!u) {
return std::nullopt;
@@ -228,7 +229,13 @@ std::optional<uint32_t> takeWTF16CodePoint(std::string_view& str) {
uint16_t highBits = *u - 0xD800;
uint16_t lowBits = *low - 0xDC00;
return 0x10000 + ((highBits << 10) | lowBits);
+ } else if (!allowWTF) {
+ // Unpaired high surrogate.
+ return std::nullopt;
}
+ } else if (!allowWTF && 0xDC00 <= *u && *u < 0xE000) {
+ // Unpaired low surrogate.
+ return std::nullopt;
}
return *u;
@@ -242,6 +249,23 @@ void writeWTF16CodeUnit(std::ostream& os, uint16_t u) {
constexpr uint32_t replacementCharacter = 0xFFFD;
+bool doConvertWTF16ToWTF8(std::ostream& os,
+ std::string_view str,
+ bool allowWTF) {
+ bool valid = true;
+
+ while (str.size()) {
+ auto u = takeWTF16CodePoint(str, allowWTF);
+ if (!u) {
+ valid = false;
+ u = replacementCharacter;
+ }
+ writeWTF8CodePoint(os, *u);
+ }
+
+ return valid;
+}
+
} // anonymous namespace
std::ostream& writeWTF8CodePoint(std::ostream& os, uint32_t u) {
@@ -308,18 +332,11 @@ bool convertWTF8ToWTF16(std::ostream& os, std::string_view str) {
}
bool convertWTF16ToWTF8(std::ostream& os, std::string_view str) {
- bool valid = true;
-
- while (str.size()) {
- auto u = takeWTF16CodePoint(str);
- if (!u) {
- valid = false;
- u = replacementCharacter;
- }
- writeWTF8CodePoint(os, *u);
- }
+ return doConvertWTF16ToWTF8(os, str, true);
+}
- return valid;
+bool convertUTF16ToUTF8(std::ostream& os, std::string_view str) {
+ return doConvertWTF16ToWTF8(os, str, false);
}
std::ostream& printEscapedJSON(std::ostream& os, std::string_view str) {