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