-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexical_cast.cpp
More file actions
103 lines (87 loc) · 2.59 KB
/
Copy pathlexical_cast.cpp
File metadata and controls
103 lines (87 loc) · 2.59 KB
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
// Copyright (C) 2017, 2018, 2019, 2021, 2022 by Mark Melton
//
#include <fmt/printf.h>
#include "core/mp/traits/type.h"
#include "core/string/lexical_cast.h"
#include "core/string/from_chars.h"
#include "core/pp/map.h"
namespace core::str {
lexical_cast_error::lexical_cast_error(std::string_view input, std::string_view type)
: std::runtime_error(fmt::sprintf("cannot parse '%s' as '%s'", input, type))
{ }
namespace detail
{
bool lexical_cast_impl<bool>::parse(std::string_view input)
{
if (input == "0" or input == "f" or input == "false")
return false;
if (input == "1" or input == "t" or input == "true")
return true;
throw lexical_cast_error(input, "bool");
}
char lexical_cast_impl<char>::parse(std::string_view input)
{ return input[0]; }
std::string lexical_cast_impl<std::string>::parse(std::string_view input)
{ return std::string(input); }
const char* lexical_cast_impl<const char*>::parse(std::string_view input)
{ return input.begin(); }
template<class T>
T parse_integral(std::string_view input)
{
try
{
T value{0};
int base{10};
const char *start = input.begin();
if ((input.size() > 1) and (input[0] == '0') and
((input[1] == 'x') or input[1] == 'X')) {
start += 2;
base = 16;
}
auto r = std::from_chars(start, input.end(), value, base);
if (r.ptr != input.end())
throw lexical_cast_error(input, mp::type_traits<T>::name);
return value;
}
catch (std::invalid_argument const&)
{
throw lexical_cast_error(input, mp::type_traits<T>::name);
}
catch (std::out_of_range const&)
{
throw lexical_cast_error(input, mp::type_traits<T>::name);
}
}
#define CODE(T) \
T lexical_cast_impl<T>::parse(std::string_view input) \
{ return parse_integral<T>(input); }
CORE_PP_EVAL_MAP(CODE, std::int8_t, std::int16_t, std::int32_t, std::int64_t,
std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t);
#undef CODE
template<class T>
T parse_floating_point(std::string_view input)
{
try
{
T value = 0;
auto r = std::from_chars(input.begin(), input.end(), value);
if (r.ptr != input.end())
throw lexical_cast_error(input, mp::type_traits<T>::name);
return value;
}
catch (std::invalid_argument const&)
{
throw lexical_cast_error(input, mp::type_traits<T>::name);
}
catch (std::out_of_range const&)
{
throw lexical_cast_error(input, mp::type_traits<T>::name);
}
}
#define CODE(T) \
T lexical_cast_impl<T>::parse(std::string_view input) \
{ return parse_floating_point<T>(input); }
CORE_PP_EVAL_MAP(CODE, float, double, long double);
#undef CODE
}; // detail
}; // end ns core