Commit 4157c3b3 authored by Alexei Filippov's avatar Alexei Filippov Committed by Commit Bot

[tracing] Fix Unicode values in TracedValue

Do not encode single byte characters with \uXXXX.

BUG=chromium:895974

Change-Id: If8e0bdb19d326228a7d3d0c9559d4457c8bfd270
Reviewed-on: https://chromium-review.googlesource.com/c/1292679Reviewed-by: 's avatarAleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Alexei Filippov <alph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56835}
parent 4423c061
...@@ -26,9 +26,8 @@ const bool kStackTypeArray = true; ...@@ -26,9 +26,8 @@ const bool kStackTypeArray = true;
void EscapeAndAppendString(const char* value, std::string* result) { void EscapeAndAppendString(const char* value, std::string* result) {
*result += '"'; *result += '"';
char number_buffer[10];
while (*value) { while (*value) {
char c = *value++; unsigned char c = *value++;
switch (c) { switch (c) {
case '\t': case '\t':
*result += "\\t"; *result += "\\t";
...@@ -43,9 +42,10 @@ void EscapeAndAppendString(const char* value, std::string* result) { ...@@ -43,9 +42,10 @@ void EscapeAndAppendString(const char* value, std::string* result) {
*result += "\\\\"; *result += "\\\\";
break; break;
default: default:
if (c < '\x20') { if (c < '\x20' || c == '\x7F') {
char number_buffer[8];
base::OS::SNPrintF( base::OS::SNPrintF(
number_buffer, arraysize(number_buffer), "\\u%04X", number_buffer, arraysize(number_buffer), "\\x%02X",
static_cast<unsigned>(static_cast<unsigned char>(c))); static_cast<unsigned>(static_cast<unsigned char>(c)));
*result += number_buffer; *result += number_buffer;
} else { } else {
......
...@@ -116,11 +116,24 @@ TEST(Escaping) { ...@@ -116,11 +116,24 @@ TEST(Escaping) {
// Cannot use the expected value literal directly in CHECK_EQ // Cannot use the expected value literal directly in CHECK_EQ
// as it fails to process the # character on Windows. // as it fails to process the # character on Windows.
const char* expected = const char* expected =
"{\"a\":\"abc\\\"\'\\\\\\\\x\\\"y\'z\\n\\t\\u0017\",\"b\":" R"({"a":"abc\"'\\\\x\"y'z\n\t\x17","b":"\x01\x02\x03\x04\x05\x06\x07\x08)"
"\"\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\u0008\\t\\n\\u000B" R"(\t\n\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A)"
"\\u000C\\u000D\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\" R"(\x1B\x1C\x1D\x1E\x1F !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO)"
"u0016\\u0017\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F " R"(PQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F"})";
"!\\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`" CHECK_EQ(expected, json);
"abcdefghijklmnopqrstuvwxyz{|}~\x7F\"}"; }
TEST(Utf8) {
const char* string1 = "Люблю тебя, Петра творенье";
const char* string2 = "☀\u2600\u26FF";
auto value = TracedValue::Create();
value->SetString("a", string1);
value->SetString("b", string2);
std::string json;
value->AppendAsTraceFormat(&json);
const char* expected =
"{\"a\":\"\u041b\u044e\u0431\u043b\u044e \u0442\u0435\u0431\u044f, \u041f"
"\u0435\u0442\u0440\u0430 \u0442\u0432\u043e\u0440\u0435\u043d\u044c"
"\u0435\",\"b\":\"\u2600\u2600\u26ff\"}";
CHECK_EQ(expected, json); CHECK_EQ(expected, json);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment