add util::escape 'escapeUnicode' option

This commit is contained in:
MihailRis
2024-11-27 21:40:48 +03:00
parent 637dfa66c2
commit 6e90568d2a
3 changed files with 9 additions and 5 deletions
+1 -1
View File
@@ -73,7 +73,7 @@ void stringifyValue(
break; break;
} }
case value_type::string: case value_type::string:
ss << util::escape(value.asString()); ss << util::escape(value.asString(), !nice);
break; break;
case value_type::number: case value_type::number:
ss << std::setprecision(15) << value.asNumber(); ss << std::setprecision(15) << value.asNumber();
+7 -3
View File
@@ -7,7 +7,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
std::string util::escape(std::string_view s) { std::string util::escape(std::string_view s, bool escapeUnicode) {
std::stringstream ss; std::stringstream ss;
ss << '"'; ss << '"';
size_t pos = 0; size_t pos = 0;
@@ -39,8 +39,12 @@ std::string util::escape(std::string_view s) {
if (c & 0x80) { if (c & 0x80) {
uint cpsize; uint cpsize;
int codepoint = decode_utf8(cpsize, s.data() + pos); int codepoint = decode_utf8(cpsize, s.data() + pos);
if (escapeUnicode) {
ss << "\\u" << std::hex << codepoint;
} else {
ss << std::string(s.data() + pos, cpsize);
}
pos += cpsize-1; pos += cpsize-1;
ss << "\\u" << std::hex << codepoint;
break; break;
} }
if (c < ' ') { if (c < ' ') {
@@ -57,7 +61,7 @@ std::string util::escape(std::string_view s) {
} }
std::string util::quote(const std::string& s) { std::string util::quote(const std::string& s) {
return escape(s); return escape(s, false);
} }
std::wstring util::lfill(std::wstring s, uint length, wchar_t c) { std::wstring util::lfill(std::wstring s, uint length, wchar_t c) {
+1 -1
View File
@@ -8,7 +8,7 @@
namespace util { namespace util {
/// @brief Function used for string serialization in text formats /// @brief Function used for string serialization in text formats
std::string escape(std::string_view s); std::string escape(std::string_view s, bool escapeUnicode=true);
/// @brief Function used for error messages /// @brief Function used for error messages
std::string quote(const std::string& s); std::string quote(const std::string& s);