Vince's CSV Parser
Loading...
Searching...
No Matches
parse_hex.hpp
Go to the documentation of this file.
1
5#pragma once
6#include <type_traits>
7#include <cmath>
8
9#include "common.hpp"
10
11namespace csv {
12 namespace internals {
13 template<typename T>
14 bool try_parse_hex(csv::string_view sv, T& parsedValue) {
15 static_assert(std::is_integral<T>::value,
16 "try_parse_hex only works with integral types (int, long, long long, etc.)");
17
18 size_t start = 0, end = 0;
19
20 // Trim out whitespace chars
21 for (; start < sv.size() && sv[start] == ' '; start++);
22 for (end = start; end < sv.size() && sv[end] != ' '; end++);
23
24 T value_ = 0;
25
26 size_t digits = (end - start);
27 size_t base16_exponent = digits - 1;
28
29 if (digits == 0) return false;
30
31 for (const auto& ch : sv.substr(start, digits)) {
32 int digit = 0;
33
34 switch (ch) {
35 case '0':
36 case '1':
37 case '2':
38 case '3':
39 case '4':
40 case '5':
41 case '6':
42 case '7':
43 case '8':
44 case '9':
45 digit = static_cast<int>(ch - '0');
46 break;
47 case 'a':
48 case 'A':
49 digit = 10;
50 break;
51 case 'b':
52 case 'B':
53 digit = 11;
54 break;
55 case 'c':
56 case 'C':
57 digit = 12;
58 break;
59 case 'd':
60 case 'D':
61 digit = 13;
62 break;
63 case 'e':
64 case 'E':
65 digit = 14;
66 break;
67 case 'f':
68 case 'F':
69 digit = 15;
70 break;
71 default:
72 return false;
73 }
74
75 value_ += digit * (T)pow(16, (double)base16_exponent);
77 }
78
80 return true;
81 }
82 }
83}
A standalone header file containing shared code.
CSV_CONST CONSTEXPR_17 OutArray arrayToDefault(T &&value)
Helper constexpr function to initialize an array with all the elements set to value.
The all encompassing namespace.
nonstd::string_view string_view
The string_view class used by this library.
Definition common.hpp:99