Drop unneeded NOLINTs and reuse the pointer from utf8_dfa_prefix()

The check cppcoreguidelines-avoid-non-const-global-variables is disabled in
.clang-tidy, and both tables are function-local constants. In
valid_utf8_prefix(), test the byte through the returned pointer instead of
indexing data again.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-30 20:02:26 +01:00
1 parent 62b238dce8
commit 5612cc3967
2 files changed
+10 -8

No files matched your search

@@ -242,7 +242,7 @@ unit-class_lexer.cpp checks every entry of the table.
*/ */
inline const std::uint64_t* utf8_dfa_table() noexcept inline const std::uint64_t* utf8_dfa_table() noexcept
{ {
static const std::array<std::uint64_t, 256> table = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) static const std::array<std::uint64_t, 256> table =
{ {
{ {
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
@@ -401,8 +401,9 @@ inline std::size_t valid_utf8_prefix(const unsigned char* data, std::size_t n) n
continue; continue;
} }
pos = static_cast<std::size_t>(utf8_dfa_prefix(data + pos, data + n) - data); const unsigned char* const last = utf8_dfa_prefix(data + pos, data + n);
if (pos != n && data[pos] >= 0x80u) pos = static_cast<std::size_t>(last - data);
if (pos != n && *last >= 0x80u)
{ {
break; // ill-formed or truncated break; // ill-formed or truncated
} }
@@ -702,7 +703,7 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t lexer_string_bulk_run(const unsigned char*
// unaffected by this fast path. // unaffected by this fast path.
inline int hex_codepoint(const unsigned char* data) noexcept inline int hex_codepoint(const unsigned char* data) noexcept
{ {
static const std::array<std::uint8_t, 256> hex_digit_table = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) static const std::array<std::uint8_t, 256> hex_digit_table =
{ {
{ {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 00..0F 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 00..0F
+5 -4
View File
@@ -13191,7 +13191,7 @@ unit-class_lexer.cpp checks every entry of the table.
*/ */
inline const std::uint64_t* utf8_dfa_table() noexcept inline const std::uint64_t* utf8_dfa_table() noexcept
{ {
static const std::array<std::uint64_t, 256> table = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) static const std::array<std::uint64_t, 256> table =
{ {
{ {
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
@@ -13350,8 +13350,9 @@ inline std::size_t valid_utf8_prefix(const unsigned char* data, std::size_t n) n
continue; continue;
} }
pos = static_cast<std::size_t>(utf8_dfa_prefix(data + pos, data + n) - data); const unsigned char* const last = utf8_dfa_prefix(data + pos, data + n);
if (pos != n && data[pos] >= 0x80u) pos = static_cast<std::size_t>(last - data);
if (pos != n && *last >= 0x80u)
{ {
break; // ill-formed or truncated break; // ill-formed or truncated
} }
@@ -13651,7 +13652,7 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t lexer_string_bulk_run(const unsigned char*
// unaffected by this fast path. // unaffected by this fast path.
inline int hex_codepoint(const unsigned char* data) noexcept inline int hex_codepoint(const unsigned char* data) noexcept
{ {
static const std::array<std::uint8_t, 256> hex_digit_table = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) static const std::array<std::uint8_t, 256> hex_digit_table =
{ {
{ {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 00..0F 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 00..0F