add util::escape 'escapeUnicode' option
This commit is contained in:
parent
637dfa66c2
commit
6e90568d2a
@ -73,7 +73,7 @@ void stringifyValue(
|
||||
break;
|
||||
}
|
||||
case value_type::string:
|
||||
ss << util::escape(value.asString());
|
||||
ss << util::escape(value.asString(), !nice);
|
||||
break;
|
||||
case value_type::number:
|
||||
ss << std::setprecision(15) << value.asNumber();
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
std::string util::escape(std::string_view s) {
|
||||
std::string util::escape(std::string_view s, bool escapeUnicode) {
|
||||
std::stringstream ss;
|
||||
ss << '"';
|
||||
size_t pos = 0;
|
||||
@ -39,8 +39,12 @@ std::string util::escape(std::string_view s) {
|
||||
if (c & 0x80) {
|
||||
uint cpsize;
|
||||
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;
|
||||
ss << "\\u" << std::hex << codepoint;
|
||||
break;
|
||||
}
|
||||
if (c < ' ') {
|
||||
@ -57,7 +61,7 @@ std::string util::escape(std::string_view 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) {
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
|
||||
namespace util {
|
||||
/// @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
|
||||
std::string quote(const std::string& s);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user