1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <algorithm>
20 #include <cstdint>
21 #include <iostream>
22 #include <regex>
23 #include <string>
24 #include <vector>
25 
26 #include "logging.h"
27 
28 namespace util {
29 
GetTypeForSize(int size)30 inline std::string GetTypeForSize(int size) {
31   if (size > 64) {
32     ERROR() << __func__ << ": Cannot use a type larger than 64 bits. (" << size << ")\n";
33   }
34 
35   if (size <= 8) return "uint8_t";
36 
37   if (size <= 16) return "uint16_t";
38 
39   if (size <= 32) return "uint32_t";
40 
41   return "uint64_t";
42 }
43 
RoundSizeUp(int size)44 inline int RoundSizeUp(int size) {
45   if (size > 64) {
46     ERROR() << __func__ << ": Cannot use a type larger than 64 bits. (" << size << ")\n";
47   }
48 
49   if (size <= 8) return 8;
50   if (size <= 16) return 16;
51   if (size <= 32) return 32;
52   return 64;
53 }
54 
55 // Returns the max value that can be contained unsigned in a number of bits.
GetMaxValueForBits(int bits)56 inline uint64_t GetMaxValueForBits(int bits) {
57   if (bits > 64) {
58     ERROR() << __func__ << ": Cannot use a type larger than 64 bits. (" << bits << ")\n";
59   }
60 
61   // Set all the bits to 1, then shift off extras.
62   return ~(static_cast<uint64_t>(0)) >> (64 - bits);
63 }
64 
CamelCaseToUnderScore(std::string value)65 inline std::string CamelCaseToUnderScore(std::string value) {
66   if (value[0] < 'A' || value[0] > 'Z') {
67     ERROR() << value << " doesn't look like CamelCase";
68   }
69 
70   // Use static to avoid compiling the regex more than once.
71   static const std::regex camel_case_regex("[A-Z][a-z0-9]*");
72 
73   // Add an underscore to the end of each pattern match.
74   value = std::regex_replace(value, camel_case_regex, "$&_");
75 
76   // Remove the last underscore at the end of the string.
77   value.pop_back();
78 
79   // Convert all characters to lowercase.
80   std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c) { return std::tolower(c); });
81 
82   return value;
83 }
84 
UnderscoreToCamelCase(std::string value)85 inline std::string UnderscoreToCamelCase(std::string value) {
86   if (value[0] < 'a' || value[0] > 'z') {
87     ERROR() << value << " invalid identifier";
88   }
89 
90   std::ostringstream camel_case;
91 
92   bool capitalize = true;
93   for (unsigned char c : value) {
94     if (c == '_') {
95       capitalize = true;
96     } else {
97       if (capitalize) {
98         c = std::toupper(c);
99         capitalize = false;
100       }
101       camel_case << c;
102     }
103   }
104 
105   return camel_case.str();
106 }
107 
ConstantCaseToCamelCase(std::string value)108 inline std::string ConstantCaseToCamelCase(std::string value) {
109   if (value[0] < 'A' || value[0] > 'Z') {
110     ERROR() << value << " doesn't look like CONSTANT_CASE";
111   }
112 
113   std::ostringstream camel_case;
114 
115   bool capitalize = true;
116   for (unsigned char c : value) {
117     if (c == '_') {
118       capitalize = true;
119     } else {
120       if (capitalize) {
121         c = std::toupper(c);
122         capitalize = false;
123       } else {
124         c = std::tolower(c);
125       }
126       camel_case << c;
127     }
128   }
129 
130   return camel_case.str();
131 }
132 
IsEnumCase(std::string value)133 inline bool IsEnumCase(std::string value) {
134   if (value[0] < 'A' || value[0] > 'Z') {
135     return false;
136   }
137 
138   // Use static to avoid compiling the regex more than once.
139   static const std::regex enum_regex("[A-Z][A-Z0-9_]*");
140 
141   return std::regex_match(value, enum_regex);
142 }
143 
StringJoin(const std::string & delimiter,const std::vector<std::string> & vec)144 inline std::string StringJoin(const std::string& delimiter, const std::vector<std::string>& vec) {
145   std::stringstream ss;
146   for (size_t i = 0; i < vec.size(); i++) {
147     ss << vec[i];
148     if (i != (vec.size() - 1)) {
149       ss << delimiter;
150     }
151   }
152   return ss.str();
153 }
154 
StringFindAndReplaceAll(std::string text,const std::string & old,const std::string & replacement)155 inline std::string StringFindAndReplaceAll(std::string text, const std::string& old, const std::string& replacement) {
156   auto pos = text.find(old);
157   while (pos != std::string::npos) {
158     text.replace(pos, old.size(), replacement);
159     pos = text.find(old, pos + replacement.size());
160   }
161   return text;
162 }
163 
ToLowerCase(std::string value)164 inline std::string ToLowerCase(std::string value) {
165   if (value[0] < 'A' || value[0] > 'Z') {
166     ERROR() << value << " doesn't look like CONSTANT_CASE";
167   }
168 
169   std::ostringstream lower_case;
170 
171   for (unsigned char c : value) {
172     c = std::tolower(c);
173     lower_case << c;
174   }
175 
176   return lower_case.str();
177 }
178 
179 }  // namespace util
180