mirror of
https://github.com/nlohmann/json.git
synced 2026-10-23 14:28:54 +00:00
Add shared detail helpers for the #5891 cleanup
Add detail::is_primitive(value_t), detail::hex4_lower(), detail::utf8_shape(), detail::input_format_name(), detail::empty_input_message(), the exception ids 116, 319, 320 and 416 to detail::exception_id, and make detail::is_ascii_copyable() constexpr. Later commits switch the duplicated code over to them. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
5 files changed
+142
-1
No files matched your search
@@ -48,6 +48,24 @@ inline std::string hex_byte(const std::uint8_t byte)
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief write a UTF-16 code unit as four lowercase hexadecimal digits
|
||||
|
||||
The lowercase counterpart of hex_byte(), used for the `\\uXXXX` escapes of the
|
||||
serializers, whose output is lowercase. Writes exactly four characters to
|
||||
@a out and returns the pointer past them.
|
||||
*/
|
||||
JSON_HEDLEY_ALWAYS_INLINE char* hex4_lower(const std::uint16_t codeunit, char* out) noexcept
|
||||
{
|
||||
constexpr const char* nibble_to_hex = "0123456789abcdef";
|
||||
const unsigned int unit = codeunit;
|
||||
out[0] = nibble_to_hex[(unit >> 12u) & 0x0Fu];
|
||||
out[1] = nibble_to_hex[(unit >> 8u) & 0x0Fu];
|
||||
out[2] = nibble_to_hex[(unit >> 4u) & 0x0Fu];
|
||||
out[3] = nibble_to_hex[unit & 0x0Fu];
|
||||
return out + 4;
|
||||
}
|
||||
|
||||
///////////////////
|
||||
// UTF-8 encoding //
|
||||
///////////////////
|
||||
@@ -103,6 +121,66 @@ void encode_utf8(std::uint32_t cp, const Out& out)
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the shape of a well-formed UTF-8 sequence, by its lead byte
|
||||
|
||||
@a length is 1 for ASCII, 2 to 4 for a valid lead byte, and 0 for a byte that
|
||||
cannot start a well-formed sequence (a continuation byte, 0xC0, 0xC1, or 0xF5
|
||||
to 0xFF). For a multibyte sequence, the second byte must lie in
|
||||
[@a second_lo, @a second_hi], and every later byte in [0x80, 0xBF]
|
||||
(RFC 3629, section 4; Unicode Table 3-7). The narrower second-byte ranges
|
||||
exclude overlong forms (0xE0, 0xF0), surrogates (0xED), and code points
|
||||
beyond U+10FFFF (0xF4).
|
||||
*/
|
||||
struct utf8_sequence_shape
|
||||
{
|
||||
std::uint8_t length;
|
||||
std::uint8_t second_lo;
|
||||
std::uint8_t second_hi;
|
||||
};
|
||||
|
||||
/// @return the shape of a UTF-8 sequence starting with @a lead, see utf8_sequence_shape
|
||||
inline utf8_sequence_shape utf8_shape(const std::uint8_t lead) noexcept
|
||||
{
|
||||
if (lead < 0x80u)
|
||||
{
|
||||
return {1, 0, 0};
|
||||
}
|
||||
if (lead < 0xC2u)
|
||||
{
|
||||
return {0, 0, 0};
|
||||
}
|
||||
if (lead <= 0xDFu)
|
||||
{
|
||||
return {2, 0x80u, 0xBFu};
|
||||
}
|
||||
if (lead == 0xE0u)
|
||||
{
|
||||
return {3, 0xA0u, 0xBFu};
|
||||
}
|
||||
if (lead == 0xEDu)
|
||||
{
|
||||
return {3, 0x80u, 0x9Fu};
|
||||
}
|
||||
if (lead <= 0xEFu)
|
||||
{
|
||||
return {3, 0x80u, 0xBFu};
|
||||
}
|
||||
if (lead == 0xF0u)
|
||||
{
|
||||
return {4, 0x90u, 0xBFu};
|
||||
}
|
||||
if (lead <= 0xF3u)
|
||||
{
|
||||
return {4, 0x80u, 0xBFu};
|
||||
}
|
||||
if (lead == 0xF4u)
|
||||
{
|
||||
return {4, 0x80u, 0x8Fu};
|
||||
}
|
||||
return {0, 0, 0};
|
||||
}
|
||||
|
||||
///////////////////
|
||||
// UTF-8 decoding //
|
||||
///////////////////
|
||||
|
||||
Reference in new issue
Block a user