/src/botan/build/include/botan/internal/stl_util.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * STL Utility Functions |
3 | | * (C) 1999-2007 Jack Lloyd |
4 | | * (C) 2015 Simon Warta (Kullo GmbH) |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #ifndef BOTAN_STL_UTIL_H_ |
10 | | #define BOTAN_STL_UTIL_H_ |
11 | | |
12 | | #include <vector> |
13 | | #include <string> |
14 | | #include <map> |
15 | | #include <set> |
16 | | #include <botan/secmem.h> |
17 | | |
18 | | namespace Botan { |
19 | | |
20 | | inline std::vector<uint8_t> to_byte_vector(const std::string& s) |
21 | 0 | { |
22 | 0 | return std::vector<uint8_t>(s.cbegin(), s.cend()); |
23 | 0 | } |
24 | | |
25 | | inline std::string to_string(const secure_vector<uint8_t> &bytes) |
26 | 0 | { |
27 | 0 | return std::string(bytes.cbegin(), bytes.cend()); |
28 | 0 | } |
29 | | |
30 | | /** |
31 | | * Return the keys of a map as a std::set |
32 | | */ |
33 | | template<typename K, typename V> |
34 | | std::set<K> map_keys_as_set(const std::map<K, V>& kv) |
35 | | { |
36 | | std::set<K> s; |
37 | | for(auto&& i : kv) |
38 | | { |
39 | | s.insert(i.first); |
40 | | } |
41 | | return s; |
42 | | } |
43 | | |
44 | | /* |
45 | | * Searching through a std::map |
46 | | * @param mapping the map to search |
47 | | * @param key is what to look for |
48 | | * @param null_result is the value to return if key is not in mapping |
49 | | * @return mapping[key] or null_result |
50 | | */ |
51 | | template<typename K, typename V> |
52 | | inline V search_map(const std::map<K, V>& mapping, |
53 | | const K& key, |
54 | | const V& null_result = V()) |
55 | 0 | { |
56 | 0 | auto i = mapping.find(key); |
57 | 0 | if(i == mapping.end()) |
58 | 0 | return null_result; |
59 | 0 | return i->second; |
60 | 0 | } |
61 | | |
62 | | template<typename K, typename V, typename R> |
63 | | inline R search_map(const std::map<K, V>& mapping, const K& key, |
64 | | const R& null_result, const R& found_result) |
65 | | { |
66 | | auto i = mapping.find(key); |
67 | | if(i == mapping.end()) |
68 | | return null_result; |
69 | | return found_result; |
70 | | } |
71 | | |
72 | | /* |
73 | | * Insert a key/value pair into a multimap |
74 | | */ |
75 | | template<typename K, typename V> |
76 | | void multimap_insert(std::multimap<K, V>& multimap, |
77 | | const K& key, const V& value) |
78 | 258k | { |
79 | 258k | multimap.insert(std::make_pair(key, value)); |
80 | 258k | } void Botan::multimap_insert<Botan::OID, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::multimap<Botan::OID, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<Botan::OID>, std::__1::allocator<std::__1::pair<Botan::OID const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >&, Botan::OID const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 78 | 160k | { | 79 | 160k | multimap.insert(std::make_pair(key, value)); | 80 | 160k | } |
void Botan::multimap_insert<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::multimap<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 78 | 97.1k | { | 79 | 97.1k | multimap.insert(std::make_pair(key, value)); | 80 | 97.1k | } |
void Botan::multimap_insert<Botan::OID, Botan::ASN1_String>(std::__1::multimap<Botan::OID, Botan::ASN1_String, std::__1::less<Botan::OID>, std::__1::allocator<std::__1::pair<Botan::OID const, Botan::ASN1_String> > >&, Botan::OID const&, Botan::ASN1_String const&) Line | Count | Source | 78 | 1.06k | { | 79 | 1.06k | multimap.insert(std::make_pair(key, value)); | 80 | 1.06k | } |
|
81 | | |
82 | | /** |
83 | | * Existence check for values |
84 | | */ |
85 | | template<typename T> |
86 | | bool value_exists(const std::vector<T>& vec, |
87 | | const T& val) |
88 | 2.96M | { |
89 | 9.07M | for(size_t i = 0; i != vec.size(); ++i) |
90 | 6.33M | if(vec[i] == val) |
91 | 228k | return true; |
92 | 2.96M | return false; |
93 | 2.96M | } bool Botan::value_exists<unsigned short>(std::__1::vector<unsigned short, std::__1::allocator<unsigned short> > const&, unsigned short const&) Line | Count | Source | 88 | 2.48M | { | 89 | 7.53M | for(size_t i = 0; i != vec.size(); ++i) | 90 | 5.06M | if(vec[i] == val) | 91 | 23.4k | return true; | 92 | 2.48M | return false; | 93 | 2.48M | } |
bool Botan::value_exists<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 88 | 163k | { | 89 | 362k | for(size_t i = 0; i != vec.size(); ++i) | 90 | 323k | if(vec[i] == val) | 91 | 124k | return true; | 92 | 163k | return false; | 93 | 163k | } |
bool Botan::value_exists<Botan::TLS::Group_Params>(std::__1::vector<Botan::TLS::Group_Params, std::__1::allocator<Botan::TLS::Group_Params> > const&, Botan::TLS::Group_Params const&) Line | Count | Source | 88 | 285k | { | 89 | 1.14M | for(size_t i = 0; i != vec.size(); ++i) | 90 | 910k | if(vec[i] == val) | 91 | 50.6k | return true; | 92 | 285k | return false; | 93 | 285k | } |
bool Botan::value_exists<Botan::TLS::Protocol_Version>(std::__1::vector<Botan::TLS::Protocol_Version, std::__1::allocator<Botan::TLS::Protocol_Version> > const&, Botan::TLS::Protocol_Version const&) Line | Count | Source | 88 | 9 | { | 89 | 305 | for(size_t i = 0; i != vec.size(); ++i) | 90 | 304 | if(vec[i] == val) | 91 | 8 | return true; | 92 | 9 | return false; | 93 | 9 | } |
bool Botan::value_exists<unsigned char>(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, unsigned char const&) Line | Count | Source | 88 | 29.8k | { | 89 | 30.6k | for(size_t i = 0; i != vec.size(); ++i) | 90 | 30.5k | if(vec[i] == val) | 91 | 29.7k | return true; | 92 | 29.8k | return false; | 93 | 29.8k | } |
|
94 | | |
95 | | template<typename T, typename Pred> |
96 | | void map_remove_if(Pred pred, T& assoc) |
97 | 914 | { |
98 | 914 | auto i = assoc.begin(); |
99 | 2.74k | while(i != assoc.end()) |
100 | 1.82k | { |
101 | 1.82k | if(pred(i->first)) |
102 | 914 | assoc.erase(i++); |
103 | 914 | else |
104 | 914 | i++; |
105 | 1.82k | } |
106 | 914 | } |
107 | | |
108 | | } |
109 | | |
110 | | #endif |