Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Utility.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Utility.hpp"
18
19#include <toolbox/contrib/fast_float/fast_float.h>
20
21#include <boost/spirit/home/qi/numeric/real.hpp>
22#include <boost/spirit/home/qi/parse.hpp>
23
24namespace toolbox {
25inline namespace util {
26using namespace std;
27
28bool stob(string_view sv, bool dfl) noexcept
29{
30 bool val{dfl};
31 switch (sv.size()) {
32 case 1:
33 switch (sv[0]) {
34 case '0':
35 case 'F':
36 case 'N':
37 case 'f':
38 case 'n':
39 val = false;
40 break;
41 case '1':
42 case 'T':
43 case 'Y':
44 case 't':
45 case 'y':
46 val = true;
47 break;
48 }
49 break;
50 case 2:
51 if ((sv[0] == 'N' || sv[0] == 'n') //
52 && (sv[1] == 'O' || sv[1] == 'o')) {
53 val = false;
54 } else if ((sv[0] == 'O' || sv[0] == 'o') //
55 && (sv[1] == 'N' || sv[1] == 'n')) {
56 val = true;
57 }
58 break;
59 case 3:
60 if ((sv[0] == 'O' || sv[0] == 'o') //
61 && (sv[1] == 'F' || sv[1] == 'f') //
62 && (sv[2] == 'F' || sv[2] == 'f')) {
63 val = false;
64 } else if ((sv[0] == 'Y' || sv[0] == 'y') //
65 && (sv[1] == 'E' || sv[1] == 'e') //
66 && (sv[2] == 'S' || sv[2] == 's')) {
67 val = true;
68 }
69 break;
70 case 4:
71 if ((sv[0] == 'T' || sv[0] == 't') //
72 && (sv[1] == 'R' || sv[1] == 'r') //
73 && (sv[2] == 'U' || sv[2] == 'u') //
74 && (sv[3] == 'E' || sv[3] == 'e')) {
75 val = true;
76 }
77 break;
78 case 5:
79 if ((sv[0] == 'F' || sv[0] == 'f') //
80 && (sv[1] == 'A' || sv[1] == 'a') //
81 && (sv[2] == 'L' || sv[2] == 'l') //
82 && (sv[3] == 'S' || sv[3] == 's') //
83 && (sv[4] == 'E' || sv[4] == 'e')) {
84 val = false;
85 }
86 break;
87 }
88 return val;
89}
90
91double stod(std::string_view sv, double dfl) noexcept
92{
93 using namespace boost::spirit;
94 double val{};
95 if (!qi::parse(sv.begin(), sv.end(), qi::double_, val)) {
96 return dfl;
97 }
98 return val;
99}
100
101double fast_stod(std::string_view sv, double dfl) noexcept
102{
103 double out{};
104 auto res = fast_float::from_chars(sv.data(), sv.data() + sv.size(), out,
105 fast_float::chars_format::fixed);
106 return res.ptr == nullptr ? dfl : out;
107}
108
109} // namespace util
110} // namespace toolbox
STL namespace.
bool stob(string_view sv, bool dfl) noexcept
Definition Utility.cpp:28
double fast_stod(std::string_view sv, double dfl) noexcept
Definition Utility.cpp:101
std::string_view sv
Definition Tokeniser.hpp:26
double stod(std::string_view sv, double dfl) noexcept
Definition Utility.cpp:91
constexpr auto bind() noexcept
Definition Slot.hpp:97