From 8d428361fbcfd6eb8d0ee9d4cf6d8dc7c6b6e22c Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 2 Aug 2026 19:31:22 -0400 Subject: [PATCH] Count entries with count() rather than equal_range plus distance (#2525) Seven accessors each spelled the same two lines: auto r = x.equal_range(key); return static_cast(std::distance(r.first, r.second)); The containers behind them all became detail::insertion_ordered_multimap along the way, so count() is available and says what these functions mean. The work is the same either way: equal_range() scans to the first match and distance() then walks the restricted iterator to the end, comparing keys at each step, which comes to one pass over the entries, and count() is one pass too. This is a readability change, not a faster one. Covers get_header_value_count, Request and Response get_trailer_value_count, Request::get_param_value_count, MultipartFormData get_field_count and get_file_count, and Result::get_request_header_value_count. The distance() call in get_param_values() stays, since it sizes a reserve() and needs the range anyway. --- httplib.h | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/httplib.h b/httplib.h index a2248828..fc423a1e 100644 --- a/httplib.h +++ b/httplib.h @@ -7600,8 +7600,7 @@ inline const char *get_header_value(const Headers &headers, inline size_t get_header_value_count(const Headers &headers, const std::string &key) { - auto r = headers.equal_range(key); - return static_cast(std::distance(r.first, r.second)); + return headers.count(key); } template @@ -10566,8 +10565,7 @@ inline std::string Request::get_trailer_value(const std::string &key, } inline size_t Request::get_trailer_value_count(const std::string &key) const { - auto r = trailers.equal_range(key); - return static_cast(std::distance(r.first, r.second)); + return trailers.count(key); } inline bool Request::has_param(const std::string &key) const { @@ -10591,8 +10589,7 @@ Request::get_param_values(const std::string &key) const { } inline size_t Request::get_param_value_count(const std::string &key) const { - auto r = params.equal_range(key); - return static_cast(std::distance(r.first, r.second)); + return params.count(key); } inline bool Request::is_multipart_form_data() const { @@ -10625,8 +10622,7 @@ inline bool MultipartFormData::has_field(const std::string &key) const { } inline size_t MultipartFormData::get_field_count(const std::string &key) const { - auto r = fields.equal_range(key); - return static_cast(std::distance(r.first, r.second)); + return fields.count(key); } inline FormData MultipartFormData::get_file(const std::string &key, @@ -10649,8 +10645,7 @@ inline bool MultipartFormData::has_file(const std::string &key) const { } inline size_t MultipartFormData::get_file_count(const std::string &key) const { - auto r = files.equal_range(key); - return static_cast(std::distance(r.first, r.second)); + return files.count(key); } // Multipart FormData writer implementation @@ -10729,8 +10724,7 @@ inline std::string Response::get_trailer_value(const std::string &key, } inline size_t Response::get_trailer_value_count(const std::string &key) const { - auto r = trailers.equal_range(key); - return static_cast(std::distance(r.first, r.second)); + return trailers.count(key); } inline void Response::set_redirect(const std::string &url, int stat) { @@ -10826,8 +10820,7 @@ inline std::string Result::get_request_header_value(const std::string &key, inline size_t Result::get_request_header_value_count(const std::string &key) const { - auto r = request_headers_.equal_range(key); - return static_cast(std::distance(r.first, r.second)); + return request_headers_.count(key); } // Stream implementation