1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HEADER_ABI_UTIL_H_
16 #define HEADER_ABI_UTIL_H_
17 
18 #include <map>
19 #include <regex>
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 
25 namespace header_checker {
26 namespace utils {
27 
28 
FindAndReplace(const std::string & candidate_str,const std::string & find_str,const std::string & replace_str)29 inline std::string FindAndReplace(const std::string &candidate_str,
30                                   const std::string &find_str,
31                                   const std::string &replace_str) {
32   // Find all matches of find_str in candidate_str and return a new string with
33   // all the matches replaced with replace_str
34   std::regex match_expr(find_str);
35   return std::regex_replace(candidate_str, match_expr, replace_str);
36 }
37 
38 template <typename T, typename K>
FindRemovedElements(const std::map<K,T> & old_elements_map,const std::map<K,T> & new_elements_map)39 std::vector<T> FindRemovedElements(
40     const std::map<K, T> &old_elements_map,
41     const std::map<K, T> &new_elements_map) {
42   std::vector<T> removed_elements;
43   for (auto &&map_element : old_elements_map) {
44     auto element_key = map_element.first;
45     auto new_element = new_elements_map.find(element_key);
46     if (new_element == new_elements_map.end()) {
47       removed_elements.emplace_back(map_element.second);
48     }
49   }
50   return removed_elements;
51 }
52 
53 template <typename K, typename T, typename Iterable, typename KeyGetter,
54           typename ValueGetter>
AddToMap(std::map<K,T> * dst,Iterable & src,KeyGetter get_key,ValueGetter get_value)55 inline void AddToMap(std::map<K, T> *dst, Iterable &src, KeyGetter get_key,
56                      ValueGetter get_value) {
57   for (auto &&element : src) {
58     dst->insert(std::make_pair(get_key(&element), get_value(&element)));
59   }
60 }
61 
62 template <typename K, typename Iterable, typename KeyGetter>
AddToSet(std::set<K> * dst,Iterable & src,KeyGetter get_key)63 inline void AddToSet(std::set<K> *dst, Iterable &src, KeyGetter get_key) {
64   for (auto &&element : src) {
65     dst->insert(get_key(element));
66   }
67 }
68 
69 template <typename K, typename T>
FindCommonElements(const std::map<K,T> & old_elements_map,const std::map<K,T> & new_elements_map)70 std::vector<std::pair<T, T>> FindCommonElements(
71     const std::map<K, T> &old_elements_map,
72     const std::map<K, T> &new_elements_map) {
73   std::vector<std::pair<T, T>> common_elements;
74   typename std::map<K, T>::const_iterator old_element =
75       old_elements_map.begin();
76   typename std::map<K, T>::const_iterator new_element =
77       new_elements_map.begin();
78   while (old_element != old_elements_map.end() &&
79          new_element != new_elements_map.end()) {
80     if (old_element->first == new_element->first) {
81       common_elements.emplace_back(std::make_pair(
82           old_element->second, new_element->second));
83       old_element++;
84       new_element++;
85       continue;
86     }
87     if (old_element->first < new_element->first) {
88       old_element++;
89     } else {
90       new_element++;
91     }
92   }
93   return common_elements;
94 }
95 
96 
97 }  // namespace utils
98 }  // namespace header_checker
99 
100 
101 #endif  // HEADER_ABI_UTIL_H_
102