mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-01 05:02:29 +07:00
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<size_t>(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.
This commit is contained in:
@@ -7600,8 +7600,7 @@ inline const char *get_header_value(const Headers &headers,
|
|||||||
|
|
||||||
inline size_t get_header_value_count(const Headers &headers,
|
inline size_t get_header_value_count(const Headers &headers,
|
||||||
const std::string &key) {
|
const std::string &key) {
|
||||||
auto r = headers.equal_range(key);
|
return headers.count(key);
|
||||||
return static_cast<size_t>(std::distance(r.first, r.second));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Map>
|
template <typename Map>
|
||||||
@@ -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 {
|
inline size_t Request::get_trailer_value_count(const std::string &key) const {
|
||||||
auto r = trailers.equal_range(key);
|
return trailers.count(key);
|
||||||
return static_cast<size_t>(std::distance(r.first, r.second));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Request::has_param(const std::string &key) const {
|
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 {
|
inline size_t Request::get_param_value_count(const std::string &key) const {
|
||||||
auto r = params.equal_range(key);
|
return params.count(key);
|
||||||
return static_cast<size_t>(std::distance(r.first, r.second));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Request::is_multipart_form_data() const {
|
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 {
|
inline size_t MultipartFormData::get_field_count(const std::string &key) const {
|
||||||
auto r = fields.equal_range(key);
|
return fields.count(key);
|
||||||
return static_cast<size_t>(std::distance(r.first, r.second));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline FormData MultipartFormData::get_file(const std::string &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 {
|
inline size_t MultipartFormData::get_file_count(const std::string &key) const {
|
||||||
auto r = files.equal_range(key);
|
return files.count(key);
|
||||||
return static_cast<size_t>(std::distance(r.first, r.second));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multipart FormData writer implementation
|
// 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 {
|
inline size_t Response::get_trailer_value_count(const std::string &key) const {
|
||||||
auto r = trailers.equal_range(key);
|
return trailers.count(key);
|
||||||
return static_cast<size_t>(std::distance(r.first, r.second));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Response::set_redirect(const std::string &url, int stat) {
|
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
|
inline size_t
|
||||||
Result::get_request_header_value_count(const std::string &key) const {
|
Result::get_request_header_value_count(const std::string &key) const {
|
||||||
auto r = request_headers_.equal_range(key);
|
return request_headers_.count(key);
|
||||||
return static_cast<size_t>(std::distance(r.first, r.second));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stream implementation
|
// Stream implementation
|
||||||
|
|||||||
Reference in New Issue
Block a user