33#include <NDEVR/BaseValues.h>
34#include <NDEVR/Angle.h>
61 0.0000000000000000001,
67 template<
class t_type>
71 template<>
constexpr uint04 MaxDigits<sint01>() {
return 3; }
82 template<
class t_type>
83 static constexpr t_type npow10(
uint02 exp);
84 template<
class t_type>
85 static constexpr t_type pow10(
uint02 exp);
101 constexpr fltp04 table[] = {
102 1e0f, 1e1f, 1e2f, 1e3f, 1e4f, 1e5f, 1e6f, 1e7f, 1e8f, 1e9f,
103 1e10f, 1e11f, 1e12f, 1e13f, 1e14f, 1e15f, 1e16f, 1e17f, 1e18f, 1e19f,
104 1e20f, 1e21f, 1e22f, 1e23f, 1e24f, 1e25f, 1e26f, 1e27f, 1e28f, 1e29f,
105 1e30f, 1e31f, 1e32f, 1e33f, 1e34f, 1e35f, 1e36f, 1e37f, 1e38f
107 return (exp < 39) ? table[exp] : INFINITY;
112 constexpr fltp04 table[] = {
113 1e-0f, 1e-1f, 1e-2f, 1e-3f, 1e-4f, 1e-5f, 1e-6f, 1e-7f, 1e-8f, 1e-9f,
114 1e-10f, 1e-11f, 1e-12f, 1e-13f, 1e-14f, 1e-15f, 1e-16f, 1e-17f, 1e-18f, 1e-19f,
115 1e-20f, 1e-21f, 1e-22f, 1e-23f, 1e-24f, 1e-25f, 1e-26f, 1e-27f, 1e-28f, 1e-29f,
116 1e-30f, 1e-31f, 1e-32f, 1e-33f, 1e-34f, 1e-35f, 1e-36f, 1e-37f, 1e-38f
118 return (exp < 39) ? table[exp] : 0.0f;
121 constexpr fltp08 pow10(uint16_t exp)
124 constexpr fltp08 table[] = {
125 1e0, 1e8, 1e16, 1e24, 1e32, 1e40, 1e48, 1e56,
126 1e64, 1e72, 1e80, 1e88, 1e96, 1e104, 1e112, 1e120,
127 1e128, 1e136, 1e144, 1e152, 1e160, 1e168, 1e176, 1e184,
128 1e192, 1e200, 1e208, 1e216, 1e224, 1e232, 1e240, 1e248,
129 1e256, 1e264, 1e272, 1e280, 1e288, 1e296, 1e304
135 const uint16_t block = exp / 8;
136 const uint16_t rem = exp % 8;
139 constexpr fltp08 pow10_small[] = {
140 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7
143 return table[block] * pow10_small[rem];
146 constexpr fltp08 npow10(uint16_t exp)
149 constexpr fltp08 table[] = {
150 1e0, 1e-8, 1e-16, 1e-24, 1e-32, 1e-40, 1e-48, 1e-56,
151 1e-64, 1e-72, 1e-80, 1e-88, 1e-96, 1e-104, 1e-112, 1e-120,
152 1e-128, 1e-136, 1e-144, 1e-152, 1e-160, 1e-168, 1e-176, 1e-184,
153 1e-192, 1e-200, 1e-208, 1e-216, 1e-224, 1e-232, 1e-240, 1e-248,
154 1e-256, 1e-264, 1e-272, 1e-280, 1e-288, 1e-296, 1e-304
160 const uint16_t block = exp / 8;
161 const uint16_t rem = exp % 8;
164 constexpr fltp08 pow10_inv[] = {
165 1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7
168 return table[block] * pow10_inv[rem];
174 template<
class t_type>
175 static constexpr t_type parse(
const char* in,
const char** out =
nullptr, t_type Invalid_Value = Constant<t_type>::Invalid,
bool check_overflow =
true)
178 return Constant<t_type>::Invalid;
179 return _parse(in, out, Invalid_Value, check_overflow);
181 template<
class t_type>
182 static constexpr t_type parseUInt(
const char* in,
const char** out, t_type Invalid_Value,
bool check_overflow)
184 if (*in <
'0' || *in >
'9')
187 return Invalid_Value;
190 for (
uint04 i = 0; i < MaxDigits<t_type>() - 1; i++)
192 if (*in <
'0' || *in >
'9')
194 check_overflow =
false;
202 if (*in >=
'0' && *in <=
'9')
204 t_type new_value = (value * 10) + (*in -
'0');
206 if (new_value < value)
208 value = Invalid_Value;
213 if (*in >
'0' && *in <
'9')
214 value = Invalid_Value;
223 template <
class t_
int_type,
typename t_type>
224 static constexpr t_type parseFloat(
const char* c,
const char** out_s, t_type Invalid_Value,
bool check_overflow)
230 return Invalid_Value;
232 bool inv = (*c ==
'-');
233 if (inv || *c ==
'+')
235 t_int_type value = 0;
237 value = parse<t_int_type>(c, &c, Constant<t_int_type>::Invalid, check_overflow);
242 return Invalid_Value;
248 const char* c_old = c;
249 value = parseUInt<t_int_type>(c, &c, Constant<t_int_type>::Invalid,
false);
256 while (*c >=
'0' && *c <=
'9')
260 if (*c ==
'e' || *c ==
'E')
263 bool inv_e = (*c ==
'-');
264 if (inv_e || *c ==
'+')
266 uint02 val = parseUInt<uint02>(c, &c, Constant<uint02>::Invalid, check_overflow);
268 float_value *= npow10<t_type>(val);
270 float_value *= pow10<t_type>(val);
275 float_value = -float_value;
282 static constexpr inline uint04 parseHex(
const char* in,
const char** out =
nullptr)
287 if (*in >=
'0' && *in <=
'9')
288 value = (value << 4u) + (*in -
'0');
289 else if (*in >=
'A' && *in <=
'F')
290 value = (value << 4u) + (*in -
'A') + 10;
291 else if (*in >=
'a' && *in <=
'f')
292 value = (value << 4u) + (*in -
'a') + 10;
305 template<
class t_
char_type>
306 static constexpr inline uint04 HexDigitToDecimal(t_char_type in)
308 uint04 out = Constant<uint04>::Invalid;
309 if (in >=
'0' && in <=
'9')
311 else if (in >=
'a' && in <=
'f')
312 out = 10u + in -
'a';
313 else if (in >=
'A' && in <=
'F')
314 out = 10u + in -
'A';
321 template<
class t_
char_type>
322 static constexpr inline uint01 HexOctetToDecimal(
const t_char_type* in)
327 static constexpr inline uint01 _parse(
const char* in,
const char** out,
uint01 Invalid_Value,
bool check_overflow)
329 if (*in ==
'+') in++;
330 return parseUInt(in, out, Invalid_Value, check_overflow);
332 static constexpr inline uint02 _parse(
const char* in,
const char** out,
uint02 Invalid_Value,
bool check_overflow)
334 if (*in ==
'+') in++;
335 return parseUInt(in, out, Invalid_Value, check_overflow);
337 static constexpr inline uint04 _parse(
const char* in,
const char** out,
uint04 Invalid_Value,
bool check_overflow)
339 if (*in ==
'+') in++;
340 return parseUInt(in, out, Invalid_Value, check_overflow);
342 static constexpr inline uint08 _parse(
const char* in,
const char** out,
uint08 Invalid_Value,
bool check_overflow)
344 if (*in ==
'+') in++;
345 return parseUInt(in, out, Invalid_Value, check_overflow);
347 static constexpr inline sint01 _parse(
const char* in,
const char** out,
sint01 Invalid_Value,
bool check_overflow)
349 bool inv = (*in ==
'-');
350 if (inv || *in ==
'+')
352 sint01 value = parseUInt<sint01>(in, out, Constant<sint01>::Invalid, check_overflow);
354 return Invalid_Value;
355 return inv ? -value : value;
357 static constexpr inline sint02 _parse(
const char* in,
const char** out,
sint02 Invalid_Value,
bool check_overflow)
359 bool inv = (*in ==
'-');
360 if (inv || *in ==
'+')
362 sint02 value = parseUInt<sint02>(in, out, Constant<sint02>::Invalid, check_overflow);
364 return Invalid_Value;
365 return inv ? -value : value;
367 static constexpr inline sint04 _parse(
const char* in,
const char** out,
sint04 Invalid_Value,
bool check_overflow)
369 bool inv = (*in ==
'-');
370 if (inv || *in ==
'+')
372 sint04 value = parseUInt<sint04>(in, out, Constant<sint04>::Invalid, check_overflow);
374 return Invalid_Value;
375 return inv ? -value : value;
377 static constexpr inline sint08 _parse(
const char* in,
const char** out,
sint08 Invalid_Value,
bool check_overflow)
379 bool inv = (*in ==
'-');
380 if (inv || *in ==
'+')
382 sint08 value = parseUInt<sint08>(in, out, Constant<sint08>::Invalid, check_overflow);
384 return Invalid_Value;
389 static constexpr inline fltp04 _parse(
const char* in,
const char** out,
fltp04 Invalid_Value,
bool check_overflow)
391 return parseFloat<uint04, fltp04>(in, out, Invalid_Value, check_overflow);
393 static constexpr inline fltp08 _parse(
const char* in,
const char** out,
fltp08 Invalid_Value,
bool check_overflow)
395 return parseFloat<uint08, fltp08>(in, out, Invalid_Value, check_overflow);
397 static constexpr inline Angle<sint04> _parse(
const char* in,
const char** out, Angle<sint04> Invalid_Value,
bool check_overflow)
399 return Angle<sint04>(NumberParser::_parse(in, out, Invalid_Value.internal<
false>(), check_overflow));
401 static constexpr inline Angle<fltp08> _parse(
const char* in,
const char** out, Angle<fltp08> Invalid_Value,
bool check_overflow)
403 return Angle<fltp08>(NumberParser::_parse(in, out, Invalid_Value.internal<
false>(), check_overflow));
405 static constexpr inline Angle<fltp04> _parse(
const char* in,
const char** out, Angle<fltp04> Invalid_Value,
bool check_overflow)
407 return Angle<fltp04>(NumberParser::_parse(in, out, Invalid_Value.internal<
false>(), check_overflow));
412 static constexpr inline uint04 parseOctal(
const char* in,
const char** out =
nullptr)
417 if (*in <
'0' || *in >
'7')
419 value = (value << 3) + (*in -
'0');
426 static_assert(NumberParser::parse<fltp04>(
"0.1234") == 0.1234f,
"Bad parsing");
427 static_assert(NumberParser::parse<sint04>(
"43043") == 43043,
"Bad parsing");
428 static_assert(NumberParser::parse<fltp08>(
"0.123456337891233") == 0.123456337891233,
"Bad parsing");
429 static_assert(NumberParser::parse<fltp08>(
"2e54") == 2e54,
"Bad parsing");
430 static_assert(NumberParser::parse<fltp08>(
"21e-222") == 21e-222,
"Bad parsing");
431 static_assert(NumberParser::parse<fltp08>(
"21222bds") == 21222.0,
"Bad parsing");
432 static_assert(NumberParser::parse<uint01>(
"255") == 255U,
"Bad parsing");
433 static_assert(NumberParser::parse<uint01>(
"-126") == Constant<uint01>::Invalid,
"Bad parsing");
434 static_assert(NumberParser::parse<sint01>(
"-128") == -128,
"Bad parsing");
435 static_assert(NumberParser::parse<sint01>(
"-130") == -128,
"Bad parsing");
The primary namespace for the NDEVR SDK.
constexpr const fltp08 fast_atof_table[20]
Data for quick text to number lookup.
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
static constexpr bool IsValid(const Angle< t_type > &value)
Checks whether the given Angle holds a valid value.
uint16_t uint02
-Defines an alias representing a 2 byte, unsigned integer -Can represent exact integer values 0 throu...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
int16_t sint02
-Defines an alias representing a 2 byte, signed integer.
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
int8_t sint01
-Defines an alias representing a 1 byte, signed integer.
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
constexpr uint04 MaxDigits()
For a given number type, returns the maximum number of digits the number type can represent.
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.