From a945acc5d5f27490c9bc48d8f6ed46dc26782bb4 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Sun, 7 Apr 2024 09:09:07 -0400 Subject: [PATCH 1/2] Remove some obsolete and/or rarely used STL utilities --- src/cli/utils.cpp | 13 ++++-- src/lib/utils/http_util/http_util.cpp | 6 +-- src/lib/utils/stl_util.h | 59 --------------------------- src/lib/x509/asn1_alt_name.cpp | 7 +++- src/lib/x509/x509_dn.cpp | 4 +- src/tests/tests.cpp | 16 +++++++- 6 files changed, 34 insertions(+), 71 deletions(-) diff --git a/src/cli/utils.cpp b/src/cli/utils.cpp index 2761b0dd065..6fa42fd6fb6 100644 --- a/src/cli/utils.cpp +++ b/src/cli/utils.cpp @@ -66,12 +66,19 @@ class Print_Help final : public Command { oss << "Available commands:\n\n"; for(const auto& commands : grouped_commands) { - std::string desc = commands.first; - if(desc.empty()) { + const std::string group = commands.first; + if(group.empty()) { + // ??? continue; } - oss << Botan::search_map(groups_description, desc, desc) << ":\n"; + auto descr = groups_description.find(group); + if(descr != groups_description.end()) { + oss << descr->second; + } else { + oss << group; + } + oss << ":\n"; for(const auto& cmd : commands.second) { oss << " " << std::setw(16) << std::left << cmd->cmd_name() << " " << cmd->description() << "\n"; } diff --git a/src/lib/utils/http_util/http_util.cpp b/src/lib/utils/http_util/http_util.cpp index 4c2dd35bdbf..c88c3894ff4 100644 --- a/src/lib/utils/http_util/http_util.cpp +++ b/src/lib/utils/http_util/http_util.cpp @@ -214,9 +214,9 @@ Response http_sync(const http_exch_fn& http_transact, resp_body.insert(resp_body.end(), buf.data(), &buf[got]); } - const std::string header_size = search_map(headers, std::string("Content-Length")); - - if(!header_size.empty()) { + auto cl_hdr = headers.find("Content-Length"); + if(cl_hdr != headers.end()) { + const std::string header_size = cl_hdr->second; if(resp_body.size() != to_u32bit(header_size)) { throw HTTP_Error(fmt("Content-Length disagreement, header says {} got {}", header_size, resp_body.size())); } diff --git a/src/lib/utils/stl_util.h b/src/lib/utils/stl_util.h index 26f624370d3..2629350f21e 100644 --- a/src/lib/utils/stl_util.h +++ b/src/lib/utils/stl_util.h @@ -11,9 +11,7 @@ #include #include -#include #include -#include #include #include #include @@ -54,63 +52,6 @@ RetT reduce(const std::vector& keys, RetT acc, ReducerT reducer) return acc; } -/** -* Return the keys of a map as a std::set -*/ -template -std::set map_keys_as_set(const std::map& kv) { - std::set s; - for(auto&& i : kv) { - s.insert(i.first); - } - return s; -} - -/** -* Return the keys of a multimap as a std::set -*/ -template -std::set map_keys_as_set(const std::multimap& kv) { - std::set s; - for(auto&& i : kv) { - s.insert(i.first); - } - return s; -} - -/* -* Searching through a std::map -* @param mapping the map to search -* @param key is what to look for -* @param null_result is the value to return if key is not in mapping -* @return mapping[key] or null_result -*/ -template -inline V search_map(const std::map& mapping, const K& key, const V& null_result = V()) { - auto i = mapping.find(key); - if(i == mapping.end()) { - return null_result; - } - return i->second; -} - -template -inline R search_map(const std::map& mapping, const K& key, const R& null_result, const R& found_result) { - auto i = mapping.find(key); - if(i == mapping.end()) { - return null_result; - } - return found_result; -} - -/* -* Insert a key/value pair into a multimap -*/ -template -void multimap_insert(std::multimap& multimap, const K& key, const V& value) { - multimap.insert(std::make_pair(key, value)); -} - /** * Existence check for values */ diff --git a/src/lib/x509/asn1_alt_name.cpp b/src/lib/x509/asn1_alt_name.cpp index 08bace9e3af..3ea26a05b2d 100644 --- a/src/lib/x509/asn1_alt_name.cpp +++ b/src/lib/x509/asn1_alt_name.cpp @@ -56,7 +56,8 @@ void AlternativeName::add_othername(const OID& oid, std::string_view value, ASN1 if(value.empty()) { return; } - multimap_insert(m_othernames, oid, ASN1_String(value, type)); + ASN1_String str(value, type); + m_othernames.insert(std::make_pair(oid, str)); } /* @@ -70,7 +71,9 @@ std::multimap AlternativeName::contents() const { } for(const auto& othername : m_othernames) { - multimap_insert(names, othername.first.to_formatted_string(), othername.second.value()); + std::string typ = othername.first.to_formatted_string(); + std::string value = othername.second.value(); + names.insert(std::make_pair(typ, value)); } return names; diff --git a/src/lib/x509/x509_dn.cpp b/src/lib/x509/x509_dn.cpp index bf2a5357829..d4ad09858c1 100644 --- a/src/lib/x509/x509_dn.cpp +++ b/src/lib/x509/x509_dn.cpp @@ -113,7 +113,7 @@ std::multimap X509_DN::get_attributes() const { std::multimap retval; for(auto& i : m_rdn) { - multimap_insert(retval, i.first, i.second.value()); + retval.insert(std::make_pair(i.first, i.second.value())); } return retval; } @@ -125,7 +125,7 @@ std::multimap X509_DN::contents() const { std::multimap retval; for(auto& i : m_rdn) { - multimap_insert(retval, i.first.to_formatted_string(), i.second.value()); + retval.insert(std::make_pair(i.first.to_formatted_string(), i.second.value())); } return retval; } diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index c6ce80b7884..683fb2c235c 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -519,9 +519,21 @@ class Test_Registry { return nullptr; } - std::set registered_tests() const { return Botan::map_keys_as_set(m_tests); } + std::set registered_tests() const { + std::set s; + for(auto&& i : m_tests) { + s.insert(i.first); + } + return s; + } - std::set registered_test_categories() const { return Botan::map_keys_as_set(m_categories); } + std::set registered_test_categories() const { + std::set s; + for(auto&& i : m_categories) { + s.insert(i.first); + } + return s; + } std::vector filter_registered_tests(const std::vector& requested, const std::set& to_be_skipped) { From 479128852dd4b9f9745bf869cd1ceed18fff848a Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Tue, 9 Apr 2024 07:57:49 -0400 Subject: [PATCH 2/2] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Take advantage of emplace Co-authored-by: René Meusel --- src/lib/x509/asn1_alt_name.cpp | 7 ++----- src/lib/x509/x509_dn.cpp | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/lib/x509/asn1_alt_name.cpp b/src/lib/x509/asn1_alt_name.cpp index 3ea26a05b2d..8fff9145c21 100644 --- a/src/lib/x509/asn1_alt_name.cpp +++ b/src/lib/x509/asn1_alt_name.cpp @@ -56,8 +56,7 @@ void AlternativeName::add_othername(const OID& oid, std::string_view value, ASN1 if(value.empty()) { return; } - ASN1_String str(value, type); - m_othernames.insert(std::make_pair(oid, str)); + m_othernames.emplace(oid, ASN1_String{value, type}); } /* @@ -71,9 +70,7 @@ std::multimap AlternativeName::contents() const { } for(const auto& othername : m_othernames) { - std::string typ = othername.first.to_formatted_string(); - std::string value = othername.second.value(); - names.insert(std::make_pair(typ, value)); + names.emplace(othername.first.to_formatted_string(), othername.second.value()); } return names; diff --git a/src/lib/x509/x509_dn.cpp b/src/lib/x509/x509_dn.cpp index d4ad09858c1..1f7a9e21c47 100644 --- a/src/lib/x509/x509_dn.cpp +++ b/src/lib/x509/x509_dn.cpp @@ -113,7 +113,7 @@ std::multimap X509_DN::get_attributes() const { std::multimap retval; for(auto& i : m_rdn) { - retval.insert(std::make_pair(i.first, i.second.value())); + retval.emplace(i.first, i.second.value()); } return retval; } @@ -125,7 +125,7 @@ std::multimap X509_DN::contents() const { std::multimap retval; for(auto& i : m_rdn) { - retval.insert(std::make_pair(i.first.to_formatted_string(), i.second.value())); + retval.emplace(i.first.to_formatted_string(), i.second.value()); } return retval; }