From c48ed1ed9a9426690cdf60a92eb61d70af9f9345 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 2 Aug 2026 19:31:46 -0400 Subject: [PATCH] Share the query pair splitting between its two callers (#2526) parse_query_text() and normalize_query_string() both walk a query string and both open with the same eight lines to cut one "key=value" span at its first '='. Give that a name and call it from both. divide() puts everything before the first delimiter in the left half and the rest in the right, so a span with no '=' lands entirely in key and leaves val empty. Both callers rely on that: parse_query_text() records a bare "flag" with an empty value, and normalize_query_string() emits it back without an '='. The helper's comment says so, since that is the part of divide()'s behaviour a reader has to know to follow either caller. --- httplib.h | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/httplib.h b/httplib.h index fc423a1e..7c0ed3f8 100644 --- a/httplib.h +++ b/httplib.h @@ -8291,6 +8291,19 @@ inline std::string params_to_query_str(const Params ¶ms) { return query; } +// Splits one "key=value" span of a query string at its first '='. A span with +// no '=' at all lands entirely in key, leaving val empty, which is how a bare +// "?flag" keeps its name. +inline void divide_query_pair(const char *b, const char *e, std::string &key, + std::string &val) { + divide(b, static_cast(e - b), '=', + [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data, + std::size_t rhs_size) { + key.assign(lhs_data, lhs_size); + val.assign(rhs_data, rhs_size); + }); +} + inline void parse_query_text(const char *data, std::size_t size, Params ¶ms) { std::set cache; @@ -8301,12 +8314,7 @@ inline void parse_query_text(const char *data, std::size_t size, std::string key; std::string val; - divide(b, static_cast(e - b), '=', - [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data, - std::size_t rhs_size) { - key.assign(lhs_data, lhs_size); - val.assign(rhs_data, rhs_size); - }); + divide_query_pair(b, e, key, val); if (!key.empty()) { params.emplace(decode_query_component(key), decode_query_component(val)); @@ -8331,12 +8339,7 @@ inline std::string normalize_query_string(const std::string &query) { [&](const char *b, const char *e) { std::string key; std::string val; - divide(b, static_cast(e - b), '=', - [&](const char *lhs_data, std::size_t lhs_size, - const char *rhs_data, std::size_t rhs_size) { - key.assign(lhs_data, lhs_size); - val.assign(rhs_data, rhs_size); - }); + divide_query_pair(b, e, key, val); if (!key.empty()) { auto dec_key = decode_query_component(key);