| 253 | } |
| 254 | |
| 255 | tl::expected<Bytes, std::string> |
| 256 | parse_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 | |
| 297 | tl::expected<double, std::string> |
| 298 | parse_double(const std::string& value) |
no test coverage detected