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 
29 std::string RealPath(const std::string &path);
30 
31 std::set<std::string> CollectAllExportedHeaders(
32     const std::vector<std::string> &exported_header_dirs);
33 
FindAndReplace(const std::string & candidate_str,const std::string & find_str,const std::string & replace_str)34 inline std::string FindAndReplace(const std::string &candidate_str,
35                                   const std::string &find_str,
36                                   const std::string &replace_str) {
37   // Find all matches of find_str in candidate_str and return a new string with
38   // all the matches replaced with replace_str
39   std::regex match_expr(find_str);
40   return std::regex_replace(candidate_str, match_expr, replace_str);
41 }
42 
43 template <typename T, typename K>
FindRemovedElements(const std::map<K,T> & old_elements_map,const std::map<K,T> & new_elements_map)44 std::vector<T> FindRemovedElements(
45     const std::map<K, T> &old_elements_map,
46     const std::map<K, T> &new_elements_map) {
47   std::vector<T> removed_elements;
48   for (auto &&map_element : old_elements_map) {
49     auto element_key = map_element.first;
50     auto new_element = new_elements_map.find(element_key);
51     if (new_element == new_elements_map.end()) {
52       removed_elements.emplace_back(map_element.second);
53     }
54   }
55   return removed_elements;
56 }
57 
58 template <typename K, typename T, typename Iterable, typename KeyGetter,
59           typename ValueGetter>
AddToMap(std::map<K,T> * dst,Iterable & src,KeyGetter get_key,ValueGetter get_value)60 inline void AddToMap(std::map<K, T> *dst, Iterable &src, KeyGetter get_key,
61                      ValueGetter get_value) {
62   for (auto &&element : src) {
63     dst->insert(std::make_pair(get_key(&element), get_value(&element)));
64   }
65 }
66 
67 template <typename K, typename Iterable, typename KeyGetter>
AddToSet(std::set<K> * dst,Iterable & src,KeyGetter get_key)68 inline void AddToSet(std::set<K> *dst, Iterable &src, KeyGetter get_key) {
69   for (auto &&element : src) {
70     dst->insert(get_key(element));
71   }
72 }
73 
74 template <typename K, typename T>
FindCommonElements(const std::map<K,T> & old_elements_map,const std::map<K,T> & new_elements_map)75 std::vector<std::pair<T, T>> FindCommonElements(
76     const std::map<K, T> &old_elements_map,
77     const std::map<K, T> &new_elements_map) {
78   std::vector<std::pair<T, T>> common_elements;
79   typename std::map<K, T>::const_iterator old_element =
80       old_elements_map.begin();
81   typename std::map<K, T>::const_iterator new_element =
82       new_elements_map.begin();
83   while (old_element != old_elements_map.end() &&
84          new_element != new_elements_map.end()) {
85     if (old_element->first == new_element->first) {
86       common_elements.emplace_back(std::make_pair(
87           old_element->second, new_element->second));
88       old_element++;
89       new_element++;
90       continue;
91     }
92     if (old_element->first < new_element->first) {
93       old_element++;
94     } else {
95       new_element++;
96     }
97   }
98   return common_elements;
99 }
100 
101 
102 }  // namespace utils
103 }  // namespace header_checker
104 
105 
106 #endif  // HEADER_ABI_UTIL_H_
107