summaryrefslogtreecommitdiff
path: root/system/gd/packet/parser/util.h
blob: 342e1c5bd4efe8317ec03868667683a2dd204ad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <algorithm>
#include <iostream>
#include <regex>
#include <string>

#include "logging.h"

namespace util {

inline std::string GetTypeForSize(int size) {
  if (size > 64) {
    ERROR() << __func__ << ": Cannot use a type larger than 64 bits. (" << size << ")\n";
  }

  if (size <= 8) return "uint8_t";

  if (size <= 16) return "uint16_t";

  if (size <= 32) return "uint32_t";

  return "uint64_t";
}

inline int RoundSizeUp(int size) {
  if (size > 64) {
    ERROR() << __func__ << ": Cannot use a type larger than 64 bits. (" << size << ")\n";
  }

  if (size <= 8) return 8;
  if (size <= 16) return 16;
  if (size <= 32) return 32;
  return 64;
}

// Returns the max value that can be contained unsigned in a number of bits.
inline uint64_t GetMaxValueForBits(int bits) {
  if (bits > 64) {
    ERROR() << __func__ << ": Cannot use a type larger than 64 bits. (" << bits << ")\n";
  }

  // Set all the bits to 1, then shift off extras.
  return ~(static_cast<uint64_t>(0)) >> (64 - bits);
}

inline std::string CamelCaseToUnderScore(std::string value) {
  if (value[0] < 'A' || value[0] > 'Z') {
    ERROR() << value << " doesn't look like CamelCase";
  }

  // Use static to avoid compiling the regex more than once.
  static const std::regex camel_case_regex("[A-Z][a-z0-9]*");

  // Add an underscore to the end of each pattern match.
  value = std::regex_replace(value, camel_case_regex, "$&_");

  // Remove the last underscore at the end of the string.
  value.pop_back();

  // Convert all characters to lowercase.
  std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c) { return std::tolower(c); });

  return value;
}

inline std::string UnderscoreToCamelCase(std::string value) {
  if (value[0] < 'a' || value[0] > 'z') {
    ERROR() << value << " invalid identifier";
  }

  std::ostringstream camel_case;

  bool capitalize = true;
  for (unsigned char c : value) {
    if (c == '_') {
      capitalize = true;
    } else {
      if (capitalize) {
        c = std::toupper(c);
        capitalize = false;
      }
      camel_case << c;
    }
  }

  return camel_case.str();
}

inline std::string ConstantCaseToCamelCase(std::string value) {
  if (value[0] < 'A' || value[0] > 'Z') {
    ERROR() << value << " doesn't look like CONSTANT_CASE";
  }

  std::ostringstream camel_case;

  bool capitalize = true;
  for (unsigned char c : value) {
    if (c == '_') {
      capitalize = true;
    } else {
      if (capitalize) {
        c = std::toupper(c);
        capitalize = false;
      } else {
        c = std::tolower(c);
      }
      camel_case << c;
    }
  }

  return camel_case.str();
}

inline bool IsEnumCase(std::string value) {
  if (value[0] < 'A' || value[0] > 'Z') {
    return false;
  }

  // Use static to avoid compiling the regex more than once.
  static const std::regex enum_regex("[A-Z][A-Z0-9_]*");

  return std::regex_match(value, enum_regex);
}

inline std::string StringJoin(const std::string& delimiter, const std::vector<std::string>& vec) {
  std::stringstream ss;
  for (size_t i = 0; i < vec.size(); i++) {
    ss << vec[i];
    if (i != (vec.size() - 1)) {
      ss << delimiter;
    }
  }
  return ss.str();
}

inline std::string StringFindAndReplaceAll(std::string text, const std::string& old, const std::string& replacement) {
  auto pos = text.find(old);
  while (pos != std::string::npos) {
    text.replace(pos, old.size(), replacement);
    pos = text.find(old, pos + replacement.size());
  }
  return text;
}

inline std::string GetRustTypeForSize(int size) {
  if (size > 64) {
    ERROR() << __func__ << ": Cannot use a type larger than 64 bits. (" << size << ")\n";
  }

  if (size <= 8) return "u8";

  if (size <= 16) return "u16";

  if (size <= 32) return "u32";

  return "u64";
}

inline std::string ToLowerCase(std::string value) {
  if (value[0] < 'A' || value[0] > 'Z') {
    ERROR() << value << " doesn't look like CONSTANT_CASE";
  }

  std::ostringstream lower_case;

  for (unsigned char c : value) {
    c = std::tolower(c);
    lower_case << c;
  }

  return lower_case.str();
}

}  // namespace util