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 <boost/spirit/home/qi/numeric/real.hpp>
20#include <boost/spirit/home/qi/parse.hpp>
21
22namespace toolbox {
23inline namespace util {
24using namespace std;
25
26bool stob(string_view sv, bool dfl) noexcept
27{
28 bool val{dfl};
29 switch (sv.size()) {
30 case 1:
31 switch (sv[0]) {
32 case '0':
33 case 'F':
34 case 'N':
35 case 'f':
36 case 'n':
37 val = false;
38 break;
39 case '1':
40 case 'T':
41 case 'Y':
42 case 't':
43 case 'y':
44 val = true;
45 break;
46 }
47 break;
48 case 2:
49 if ((sv[0] == 'N' || sv[0] == 'n') //
50 && (sv[1] == 'O' || sv[1] == 'o')) {
51 val = false;
52 } else if ((sv[0] == 'O' || sv[0] == 'o') //
53 && (sv[1] == 'N' || sv[1] == 'n')) {
54 val = true;
55 }
56 break;
57 case 3:
58 if ((sv[0] == 'O' || sv[0] == 'o') //
59 && (sv[1] == 'F' || sv[1] == 'f') //
60 && (sv[2] == 'F' || sv[2] == 'f')) {
61 val = false;
62 } else if ((sv[0] == 'Y' || sv[0] == 'y') //
63 && (sv[1] == 'E' || sv[1] == 'e') //
64 && (sv[2] == 'S' || sv[2] == 's')) {
65 val = true;
66 }
67 break;
68 case 4:
69 if ((sv[0] == 'T' || sv[0] == 't') //
70 && (sv[1] == 'R' || sv[1] == 'r') //
71 && (sv[2] == 'U' || sv[2] == 'u') //
72 && (sv[3] == 'E' || sv[3] == 'e')) {
73 val = true;
74 }
75 break;
76 case 5:
77 if ((sv[0] == 'F' || sv[0] == 'f') //
78 && (sv[1] == 'A' || sv[1] == 'a') //
79 && (sv[2] == 'L' || sv[2] == 'l') //
80 && (sv[3] == 'S' || sv[3] == 's') //
81 && (sv[4] == 'E' || sv[4] == 'e')) {
82 val = false;
83 }
84 break;
85 }
86 return val;
87}
88
89double stod(std::string_view sv, double dfl) noexcept
90{
91 using namespace boost::spirit;
92 double val{};
93 if (!qi::parse(sv.begin(), sv.end(), qi::double_, val)) {
94 return dfl;
95 }
96 return val;
97}
98
99} // namespace util
100} // namespace toolbox
STL namespace.
bool stob(string_view sv, bool dfl) noexcept
Definition Utility.cpp:26
std::string_view sv
Definition Tokeniser.hpp:26
double stod(std::string_view sv, double dfl) noexcept
Definition Utility.cpp:89
constexpr auto bind() noexcept
Definition Slot.hpp:92