🚧 add support for ensure_ascii

This commit is contained in:
Niels Lohmann
2024-12-22 13:12:34 +01:00
parent a6a06b76e7
commit 3cd50255a8
3 changed files with 73 additions and 9 deletions

View File

@@ -399,8 +399,8 @@ class serializer
std::size_t bytes_after_last_accept = 0;
std::size_t undumped_chars = 0;
// copy string as-is if error handler is set to keep
if (error_handler == error_handler_t::keep)
// copy string as-is if error handler is set to keep, and we don't want to ensure ASCII
if (error_handler == error_handler_t::keep && !ensure_ascii)
{
o->write_characters(s.data(), s.size());
return;
@@ -575,7 +575,22 @@ class serializer
break;
}
case error_handler_t::keep: // LCOV_EXCL_LINE
case error_handler_t::keep:
{
// copy undumped chars to string buffer
for (int j = 0; j < undumped_chars; ++j)
{
string_buffer[bytes++] = s[bytes_after_last_accept + j];
}
// add erroneous byte to string buffer
string_buffer[bytes++] = s[i];
// continue processing the string
state = UTF8_ACCEPT;
break;
}
default: // LCOV_EXCL_LINE
JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
}
@@ -614,6 +629,20 @@ class serializer
JSON_THROW(type_error::create(316, concat("incomplete UTF-8 string; last byte: 0x", hex_bytes(static_cast<std::uint8_t>(s.back() | 0))), nullptr));
}
case error_handler_t::keep:
{
// copy undumped chars to string buffer
for (int j = 0; j < undumped_chars; ++j)
{
string_buffer[bytes++] = s[bytes_after_last_accept + j];
}
undumped_chars = 0;
// write all accepted bytes
o->write_characters(string_buffer.data(), bytes);
break;
}
case error_handler_t::ignore:
{
// write all accepted bytes
@@ -637,7 +666,6 @@ class serializer
break;
}
case error_handler_t::keep: // LCOV_EXCL_LINE
default: // LCOV_EXCL_LINE
JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
}