neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
string_numeric.hpp
Go to the documentation of this file.
1// string_numeric.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <string>
40#include <string_view>
41#include <boost/spirit/include/qi_parse.hpp>
42#include <boost/spirit/include/qi_numeric.hpp>
44
45namespace neolib
46{
47 template <typename CharT, typename Traits>
48 inline int32_t string_to_int32(const std::basic_string_view<CharT, Traits>& aStringView)
49 {
50 namespace qi = boost::spirit::qi;
51 int32_t result = 0;
52 qi::parse(aStringView.begin(), aStringView.end(), qi::int_, result);
53 return result;
54 }
55
56 template <typename CharT, typename Traits, typename Alloc>
57 inline int32_t string_to_int32(const std::basic_string<CharT, Traits, Alloc>& aString, int aBase = 10)
58 {
59 if (aBase == 10) // todo: does spirit provide other bases?
60 string_to_int32(static_cast<std::basic_string_view<CharT, Traits>>(aString));
61 return strtol(aString.c_str(), nullptr, aBase);
62 }
63
64 template <typename CharT, typename Traits>
65 inline int64_t string_to_int64(const std::basic_string_view<CharT, Traits>& aStringView)
66 {
67 namespace qi = boost::spirit::qi;
68 int64_t result = 0ll;
69 qi::parse(aStringView.begin(), aStringView.end(), qi::long_long, result);
70 return result;
71 }
72
73 template <typename CharT, typename Traits, typename Alloc>
74 inline int64_t string_to_int64(const std::basic_string<CharT, Traits, Alloc>& aString, int aBase = 10)
75 {
76 if (aBase == 10) // todo: does spirit provide other bases?
77 return string_to_int64(static_cast<std::basic_string_view<CharT, Traits>>(aString));
78 return strtoll(aString.c_str(), nullptr, aBase);
79 }
80
81 template <typename CharT, typename Traits>
82 inline uint32_t string_to_uint32(const std::basic_string_view<CharT, Traits>& aStringView)
83 {
84 namespace qi = boost::spirit::qi;
85 uint32_t result = 0u;
86 qi::parse(aStringView.begin(), aStringView.end(), qi::uint_, result);
87 return result;
88 }
89
90 template <typename CharT, typename Traits, typename Alloc>
91 inline uint32_t string_to_uint32(const std::basic_string<CharT, Traits, Alloc>& aString, int aBase = 10)
92 {
93 if (aBase == 10)
94 return string_to_uint32(static_cast<std::basic_string_view<CharT, Traits>>(aString));
95 return strtoul(aString.c_str(), 0, aBase);
96 }
97
98 template <typename CharT, typename Traits>
99 inline uint64_t string_to_uint64(const std::basic_string_view<CharT, Traits>& aStringView)
100 {
101 namespace qi = boost::spirit::qi;
102 uint64_t result = 0ull;
103 qi::parse(aStringView.begin(), aStringView.end(), qi::ulong_long, result);
104 return result;
105 }
106
107 template <typename CharT, typename Traits, typename Alloc>
108 inline uint64_t string_to_uint64(const std::basic_string<CharT, Traits, Alloc>& aString, int aBase = 10)
109 {
110 if (aBase == 10)
111 return string_to_uint64(static_cast<std::basic_string_view<CharT, Traits>>(aString));
112 return strtoull(aString.c_str(), 0, aBase);
113 }
114
116
117 struct string_to_number_failure : std::logic_error { string_to_number_failure() : std::logic_error("neolib::string_to_number_failure") {} };
118
119 template <typename CharT, typename Traits>
120 inline number_t string_to_number(const std::basic_string_view<CharT, Traits>& aStringView)
121 {
122 namespace qi = boost::spirit::qi;
123 {
124 int32_t result = 0;
125 if (qi::parse(aStringView.begin(), aStringView.end(), qi::int_, result))
126 return result;
127 }
128 {
129 uint32_t result = 0u;
130 if (qi::parse(aStringView.begin(), aStringView.end(), qi::uint_, result))
131 return result;
132 }
133 {
134 int64_t result = 0ll;
135 if (qi::parse(aStringView.begin(), aStringView.end(), qi::long_long, result))
136 return result;
137 }
138 {
139 uint64_t result = 0ull;
140 if (qi::parse(aStringView.begin(), aStringView.end(), qi::ulong_long, result))
141 return result;
142 }
143 {
144 double result = 0.0;
145 if (qi::parse(aStringView.begin(), aStringView.end(), qi::double_, result))
146 return result;
147 }
149 }
150
151 template <typename CharT, typename Traits>
152 inline double string_to_double(const std::basic_string_view<CharT, Traits>& aStringView)
153 {
154 namespace qi = boost::spirit::qi;
155 double result = 0.0;
156 qi::parse(aStringView.begin(), aStringView.end(), qi::double_, result);
157 return result;
158 }
159
160 template <typename CharT, typename Traits, typename Alloc>
161 inline double string_to_double(const std::basic_string<CharT, Traits, Alloc>& aString)
162 {
163 return string_to_double(static_cast<std::basic_string_view<CharT, Traits>>(aString));
164 }
165
166 template <typename CharT, typename Traits = std::char_traits<CharT>, typename Alloc = std::allocator<CharT>>
167 inline std::basic_string<CharT, Traits, Alloc> int32_to_string(int32_t aint32, int aBase = 10, std::size_t aWidth = 0, CharT aFill = '0')
168 {
169 std::basic_stringstream<CharT, Traits, Alloc> string;
170 if (aBase == 16)
171 string << std::hex << std::uppercase;
172 if (aWidth != 0)
173 {
174 string.width(aWidth);
175 string.fill(aFill);
176 }
177 string << aint32;
178 return string.str();
179 }
180
181 template <typename CharT, typename Traits = std::char_traits<CharT>, typename Alloc = std::allocator<CharT>>
182 inline std::basic_string<CharT, Traits, Alloc> int64_to_string(int64_t aint32, int aBase = 10, std::size_t aWidth = 0, CharT aFill = '0')
183 {
184 std::basic_stringstream<CharT, Traits, Alloc> string;
185 switch (aBase)
186 {
187 case 8:
188 string << std::oct;
189 break;
190 case 16:
191 string << std::hex << std::uppercase;
192 break;
193 }
194 if (aWidth != 0)
195 {
196 string.width(aWidth);
197 string.fill(aFill);
198 }
199 string << aint32;
200 return string.str();
201 }
202
203 template <typename CharT, typename Traits = std::char_traits<CharT>, typename Alloc = std::allocator<CharT>>
204 inline std::basic_string<CharT, Traits, Alloc> uint32_to_string(uint32_t aint32, int aBase = 10, std::size_t aWidth = 0, CharT aFill = '0')
205 {
206 std::basic_stringstream<CharT, Traits, Alloc> string;
207 if (aBase == 16)
208 string << std::hex << std::uppercase;
209 if (aWidth != 0)
210 {
211 string.width(aWidth);
212 string.fill(aFill);
213 }
214 string << aint32;
215 return string.str();
216 }
217
218 template <typename CharT, typename Traits = std::char_traits<CharT>, typename Alloc = std::allocator<CharT>>
219 inline std::basic_string<CharT, Traits, Alloc> uint64_to_string(uint64_t aint32, int aBase = 10, std::size_t aWidth = 0, CharT aFill = '0')
220 {
221 std::basic_stringstream<CharT, Traits, Alloc> string;
222 if (aBase == 16)
223 string << std::hex << std::uppercase;
224 if (aWidth != 0)
225 {
226 string.width(aWidth);
227 string.fill(aFill);
228 }
229 string << aint32;
230 return string.str();
231 }
232
233 template <typename CharT, typename Traits = std::char_traits<CharT>, typename Alloc = std::allocator<CharT>>
234 inline std::basic_string<CharT, Traits, Alloc> double_to_string(double aDouble, std::size_t aPrecision = 0, bool aFixed = true, std::size_t aWidth = 0, CharT aFill = '0')
235 {
236 std::basic_stringstream<CharT, Traits, Alloc> string;
237 if (aPrecision != 0)
238 string.precision(aPrecision);
239 if (aFixed)
240 string << std::fixed;
241 else
242 string << std::scientific;
243 if (aWidth != 0)
244 {
245 string.width(aWidth);
246 string.fill(aFill);
247 }
248 string << aDouble;
249 return string.str();
250 }
251
252}
int32_t string_to_int32(const std::basic_string_view< CharT, Traits > &aStringView)
uint64_t string_to_uint64(const std::basic_string_view< CharT, Traits > &aStringView)
number_t string_to_number(const std::basic_string_view< CharT, Traits > &aStringView)
std::basic_string< CharT, Traits, Alloc > uint32_to_string(uint32_t aint32, int aBase=10, std::size_t aWidth=0, CharT aFill='0')
variant< double, int32_t, uint32_t, int64_t, uint64_t > number_t
int64_t string_to_int64(const std::basic_string_view< CharT, Traits > &aStringView)
std::basic_string< CharT, Traits, Alloc > double_to_string(double aDouble, std::size_t aPrecision=0, bool aFixed=true, std::size_t aWidth=0, CharT aFill='0')
uint32_t string_to_uint32(const std::basic_string_view< CharT, Traits > &aStringView)
std::basic_string< CharT, Traits, Alloc > int32_to_string(int32_t aint32, int aBase=10, std::size_t aWidth=0, CharT aFill='0')
std::basic_string< CharT, Traits, Alloc > uint64_to_string(uint64_t aint32, int aBase=10, std::size_t aWidth=0, CharT aFill='0')
std::basic_string< CharT, Traits, Alloc > int64_to_string(int64_t aint32, int aBase=10, std::size_t aWidth=0, CharT aFill='0')
double string_to_double(const std::basic_string_view< CharT, Traits > &aStringView)