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) {
36     return "uint8_t";
37   }
38 
39   if (size <= 16) {
40     return "uint16_t";
41   }
42 
43   if (size <= 32) {
44     return "uint32_t";
45   }
46 
47   return "uint64_t";
48 }
49 
RoundSizeUp(int size)50 inline int RoundSizeUp(int size) {
51   if (size > 64) {
52     ERROR() << __func__ << ": Cannot use a type larger than 64 bits. (" << size << ")\n";
53   }
54 
55   if (size <= 8) {
56     return 8;
57   }
58   if (size <= 16) {
59     return 16;
60   }
61   if (size <= 32) {
62     return 32;
63   }
64   return 64;
65 }
66 
67 // Returns the max value that can be contained unsigned in a number of bits.
GetMaxValueForBits(int bits)68 inline uint64_t GetMaxValueForBits(int bits) {
69   if (bits > 64) {
70     ERROR() << __func__ << ": Cannot use a type larger than 64 bits. (" << bits << ")\n";
71   }
72 
73   // Set all the bits to 1, then shift off extras.
74   return ~(static_cast<uint64_t>(0)) >> (64 - bits);
75 }
76 
CamelCaseToUnderScore(std::string value)77 inline std::string CamelCaseToUnderScore(std::string value) {
78   if (value[0] < 'A' || value[0] > 'Z') {
79     ERROR() << value << " doesn't look like CamelCase";
80   }
81 
82   // Use static to avoid compiling the regex more than once.
83   static const std::regex camel_case_regex("[A-Z][a-z0-9]*");
84 
85   // Add an underscore to the end of each pattern match.
86   value = std::regex_replace(value, camel_case_regex, "$&_");
87 
88   // Remove the last underscore at the end of the string.
89   value.pop_back();
90 
91   // Convert all characters to lowercase.
92   std::transform(value.begin(), value.end(), value.begin(),
93                  [](unsigned char c) { return std::tolower(c); });
94 
95   return value;
96 }
97 
UnderscoreToCamelCase(std::string value)98 inline std::string UnderscoreToCamelCase(std::string value) {
99   if (value[0] < 'a' || value[0] > 'z') {
100     ERROR() << value << " invalid identifier";
101   }
102 
103   std::ostringstream camel_case;
104 
105   bool capitalize = true;
106   for (unsigned char c : value) {
107     if (c == '_') {
108       capitalize = true;
109     } else {
110       if (capitalize) {
111         c = std::toupper(c);
112         capitalize = false;
113       }
114       camel_case << c;
115     }
116   }
117 
118   return camel_case.str();
119 }
120 
ConstantCaseToCamelCase(std::string value)121 inline std::string ConstantCaseToCamelCase(std::string value) {
122   if (value[0] < 'A' || value[0] > 'Z') {
123     ERROR() << value << " doesn't look like CONSTANT_CASE";
124   }
125 
126   std::ostringstream camel_case;
127 
128   bool capitalize = true;
129   for (unsigned char c : value) {
130     if (c == '_') {
131       capitalize = true;
132     } else {
133       if (capitalize) {
134         c = std::toupper(c);
135         capitalize = false;
136       } else {
137         c = std::tolower(c);
138       }
139       camel_case << c;
140     }
141   }
142 
143   return camel_case.str();
144 }
145 
IsEnumCase(std::string value)146 inline bool IsEnumCase(std::string value) {
147   if (value[0] < 'A' || value[0] > 'Z') {
148     return false;
149   }
150 
151   // Use static to avoid compiling the regex more than once.
152   static const std::regex enum_regex("[A-Z][A-Z0-9_]*");
153 
154   return std::regex_match(value, enum_regex);
155 }
156 
StringJoin(const std::string & delimiter,const std::vector<std::string> & vec)157 inline std::string StringJoin(const std::string& delimiter, const std::vector<std::string>& vec) {
158   std::stringstream ss;
159   for (size_t i = 0; i < vec.size(); i++) {
160     ss << vec[i];
161     if (i != (vec.size() - 1)) {
162       ss << delimiter;
163     }
164   }
165   return ss.str();
166 }
167 
StringFindAndReplaceAll(std::string text,const std::string & old,const std::string & replacement)168 inline std::string StringFindAndReplaceAll(std::string text, const std::string& old,
169                                            const std::string& replacement) {
170   auto pos = text.find(old);
171   while (pos != std::string::npos) {
172     text.replace(pos, old.size(), replacement);
173     pos = text.find(old, pos + replacement.size());
174   }
175   return text;
176 }
177 
ToLowerCase(std::string value)178 inline std::string ToLowerCase(std::string value) {
179   if (value[0] < 'A' || value[0] > 'Z') {
180     ERROR() << value << " doesn't look like CONSTANT_CASE";
181   }
182 
183   std::ostringstream lower_case;
184 
185   for (unsigned char c : value) {
186     c = std::tolower(c);
187     lower_case << c;
188   }
189 
190   return lower_case.str();
191 }
192 
193 }  // namespace util
194