diff --git a/nth/format/common_formatters.h b/nth/format/common_formatters.h index 76e7680..46ae317 100644 --- a/nth/format/common_formatters.h +++ b/nth/format/common_formatters.h @@ -91,7 +91,7 @@ struct byte_formatter { uint8_t n = static_cast(b); buffer[0] = hex[n >> 4]; buffer[1] = hex[n & 0x0f]; - io::write_text(w, buffer); + io::write_text(w, std::string_view(buffer, 2)); } }; @@ -126,21 +126,15 @@ struct quote_formatter { i = 0; io::write_text(w, R"(\\)"); break; - case '\0': - io::write_text(w, s.substr(0, i)); - s.remove_prefix(i + 1); - i = 0; - io::write_text(w, R"(\0)"); - break; default: if (std::isprint(c)) { ++i; continue; } else { io::write_text(w, s.substr(0, i)); - io::write_text(w, "\\x"); + s.remove_prefix(i + 1); + io::write_text(w, "\\u00"); byte_formatter{}.format(w, static_cast(c)); - s.remove_prefix(i); i = 0; } } diff --git a/nth/format/json_test.cc b/nth/format/json_test.cc index 1485564..33e54a1 100644 --- a/nth/format/json_test.cc +++ b/nth/format/json_test.cc @@ -44,7 +44,11 @@ NTH_TEST("format/json/string") { NTH_EXPECT(json(std::string("\"hello\n\"")) == R"("\"hello\n\"")"); NTH_EXPECT(json(std::string("hello\\world")) == R"("hello\\world")"); NTH_EXPECT(json(std::string("hello\0world", 11)) == - std::string_view(R"("hello\0world")", 14)); + std::string_view(R"("hello\u0000world")")); + NTH_EXPECT(json(std::string("hello\u0000world", 11)) == + std::string_view(R"("hello\u0000world")")); + NTH_EXPECT(json(std::string("hello\7world")) == + std::string_view(R"("hello\u0007world")")); NTH_EXPECT(json(std::string("hello\\0world")) == std::string_view(R"("hello\\0world")")); }