From 41c4589c0a7d7ba290ab99775a009979c95d2c2d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 20 Jul 2026 10:15:29 +0000 Subject: [PATCH] Fix number fast path for custom string types without assign() The contiguous number fast path materialized token_buffer with token_buffer.assign(data, len), but string_t is only required to provide the minimal interface the rest of the lexer uses (push_back, append, clear, operator[], ...). Custom string types such as the test's alt_string do not implement assign(), so scan_number_bulk_contiguous() failed to compile for them (unit-alt-string), breaking the gcc/clang standards and old-compiler CI jobs. reset() already clears token_buffer, so fill it with append() - which alt_string and std::string both provide and which the string fast path already relies on - instead of assign(). Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01AXcDtEma2PjxgmPS9cQGzA Signed-off-by: Niels Lohmann --- include/nlohmann/detail/input/lexer.hpp | 6 ++++-- single_include/nlohmann/json.hpp | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 7ebec2493..5776afb67 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -1490,9 +1490,11 @@ scan_number_done: const std::size_t len = i; // materialize the token exactly as scan_number() would, substituting the - // locale decimal point so convert_number()'s strtof fallback stays valid + // locale decimal point so convert_number()'s strtof fallback stays valid. + // reset() already cleared token_buffer, so append() fills it (assign() is + // avoided because custom string_t types need not provide it) reset(); - token_buffer.assign(data, len); + token_buffer.append(reinterpret_cast(data), len); if (dot_index != std::string::npos) { token_buffer[dot_index] = static_cast(decimal_point_char); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 33442ab76..be3da54ca 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -9738,9 +9738,11 @@ scan_number_done: const std::size_t len = i; // materialize the token exactly as scan_number() would, substituting the - // locale decimal point so convert_number()'s strtof fallback stays valid + // locale decimal point so convert_number()'s strtof fallback stays valid. + // reset() already cleared token_buffer, so append() fills it (assign() is + // avoided because custom string_t types need not provide it) reset(); - token_buffer.assign(data, len); + token_buffer.append(reinterpret_cast(data), len); if (dot_index != std::string::npos) { token_buffer[dot_index] = static_cast(decimal_point_char);