MCPcopy Create free account
hub / github.com/ccache/ccache / parse_base16

Function parse_base16

src/ccache/util/string.cpp:255–295  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

253}
254
255tl::expected<Bytes, std::string>
256parse_base16(std::string_view hex_string)
257{
258 if (hex_string.size() % 2 != 0) {
259 return tl::unexpected(
260 FMT("invalid hex string (odd length): \"{}\"", hex_string));
261 }
262
263 const auto from_hex_digit = [](char ch) -> std::optional<uint8_t> {
264 if (ch >= '0' && ch <= '9') {
265 return ch - '0';
266 } else if (ch >= 'a' && ch <= 'f') {
267 return ch - 'a' + 10;
268 } else if (ch >= 'A' && ch <= 'F') {
269 return ch - 'A' + 10;
270 } else {
271 return std::nullopt;
272 }
273 };
274
275 Bytes result;
276 result.reserve(hex_string.size() / 2);
277
278 for (size_t i = 0; i < hex_string.size(); i += 2) {
279 auto high = from_hex_digit(hex_string[i]);
280 auto low = from_hex_digit(hex_string[i + 1]);
281
282 if (!high) {
283 return tl::unexpected(
284 FMT("invalid hex character at position {}: \"{}\"", i, hex_string));
285 }
286 if (!low) {
287 return tl::unexpected(
288 FMT("invalid hex character at position {}: \"{}\"", i + 1, hex_string));
289 }
290
291 result.push_back(static_cast<uint8_t>((*high << 4) | *low));
292 }
293
294 return result;
295}
296
297tl::expected<double, std::string>
298parse_double(const std::string& value)

Callers 4

cmd_getFunction · 0.85
cmd_putFunction · 0.85
cmd_removeFunction · 0.85

Calls 3

reserveMethod · 0.80
sizeMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected