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
IsEnumCase(std::string value)106 inline bool IsEnumCase(std::string value) {
107 if (value[0] < 'A' || value[0] > 'Z') {
108 return false;
109 }
110
111 // Use static to avoid compiling the regex more than once.
112 static const std::regex enum_regex("[A-Z][A-Z0-9_]*");
113
114 return std::regex_match(value, enum_regex);
115 }
116
StringJoin(const std::string & delimiter,const std::vector<std::string> & vec)117 inline std::string StringJoin(const std::string& delimiter, const std::vector<std::string>& vec) {
118 std::stringstream ss;
119 for (size_t i = 0; i < vec.size(); i++) {
120 ss << vec[i];
121 if (i != (vec.size() - 1)) {
122 ss << delimiter;
123 }
124 }
125 return ss.str();
126 }
127
StringFindAndReplaceAll(std::string text,const std::string & old,const std::string & replacement)128 inline std::string StringFindAndReplaceAll(std::string text, const std::string& old, const std::string& replacement) {
129 auto pos = text.find(old);
130 while (pos != std::string::npos) {
131 text.replace(pos, old.size(), replacement);
132 pos = text.find(old, pos + replacement.size());
133 }
134 return text;
135 }
136
137 } // namespace util
138