rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
from_chars: generalized chars to value

Read a value from the string, which must be trimmed to the value (ie, no leading/trailing whitespace). More...

Topics

 from_chars_first: generalized chars to value
 Read the first valid sequence of characters from the string, skipping leading whitespace, and convert it using from_chars: generalized chars to value .

Functions

bool c4::from_chars (csubstr buf, uint8_t *v) noexcept
bool c4::from_chars (csubstr buf, uint16_t *v) noexcept
bool c4::from_chars (csubstr buf, uint32_t *v) noexcept
bool c4::from_chars (csubstr buf, uint64_t *v) noexcept
bool c4::from_chars (csubstr buf, int8_t *v) noexcept
bool c4::from_chars (csubstr buf, int16_t *v) noexcept
bool c4::from_chars (csubstr buf, int32_t *v) noexcept
bool c4::from_chars (csubstr buf, int64_t *v) noexcept
bool c4::from_chars (csubstr buf, float *v) noexcept
bool c4::from_chars (csubstr buf, double *v) noexcept
template<class T>
auto c4::from_chars (csubstr buf, T *v) noexcept -> bool::type
template<class T>
bool c4::from_chars (csubstr buf, T **v) noexcept
bool c4::from_chars (csubstr buf, bool *v) noexcept
bool c4::from_chars (csubstr buf, char *v) noexcept
 extract a single character from a substring
bool c4::from_chars (csubstr buf, csubstr *v) noexcept
bool c4::from_chars (csubstr buf, substr *v) noexcept
template<class T>
bool c4::from_chars (csubstr s, fmt::overflow_checked_< T > wrapper)
 read an integer type, detecting overflow (returns false on overflow)
template<class T>
bool c4::from_chars (csubstr s, fmt::overflow_checked_< T > *wrapper)
 read an integer type, detecting overflow (returns false on overflow)
bool c4::from_chars (csubstr buf, fmt::raw_wrapper *r)
 read a variable in raw binary format, using memcpy
bool c4::from_chars (csubstr buf, fmt::raw_wrapper r)
 read a variable in raw binary format, using memcpy

Detailed Description

Read a value from the string, which must be trimmed to the value (ie, no leading/trailing whitespace).

return true if the conversion succeeded. There is no check for overflow; the value wraps around in a way similar to the standard C/C++ overflow behavior. For example, from_chars<int8_t>("128", &val) returns true and val will be set tot 0. See overflows: does a number string overflow a type and Check read for overflow for facilities enforcing no-overflow.

Dispatches to the most appropriate and efficient conversion function

See also
from_chars_first: generalized chars to value, atou, atoi, atof, atod

Function Documentation

◆ from_chars() [1/20]

bool c4::from_chars ( csubstr buf,
uint8_t * v )
inlinenoexcept

Definition at line 2382 of file charconv.hpp.

2382{ return atou(buf, v); }
bool atou(csubstr str, T *v) noexcept
Convert a trimmed string to an unsigned integral value.

◆ from_chars() [2/20]

bool c4::from_chars ( csubstr buf,
uint16_t * v )
inlinenoexcept

Definition at line 2383 of file charconv.hpp.

2383{ return atou(buf, v); }

◆ from_chars() [3/20]

bool c4::from_chars ( csubstr buf,
uint32_t * v )
inlinenoexcept

Definition at line 2384 of file charconv.hpp.

2384{ return atou(buf, v); }

◆ from_chars() [4/20]

bool c4::from_chars ( csubstr buf,
uint64_t * v )
inlinenoexcept

Definition at line 2385 of file charconv.hpp.

2385{ return atou(buf, v); }

◆ from_chars() [5/20]

bool c4::from_chars ( csubstr buf,
int8_t * v )
inlinenoexcept

Definition at line 2386 of file charconv.hpp.

2386{ return atoi(buf, v); }
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.

◆ from_chars() [6/20]

bool c4::from_chars ( csubstr buf,
int16_t * v )
inlinenoexcept

Definition at line 2387 of file charconv.hpp.

2387{ return atoi(buf, v); }

◆ from_chars() [7/20]

bool c4::from_chars ( csubstr buf,
int32_t * v )
inlinenoexcept

Definition at line 2388 of file charconv.hpp.

2388{ return atoi(buf, v); }

◆ from_chars() [8/20]

bool c4::from_chars ( csubstr buf,
int64_t * v )
inlinenoexcept

Definition at line 2389 of file charconv.hpp.

2389{ return atoi(buf, v); }

◆ from_chars() [9/20]

bool c4::from_chars ( csubstr buf,
float * v )
inlinenoexcept

Definition at line 2390 of file charconv.hpp.

2390{ return atof(buf, v); }
bool atof(csubstr str, float *v) noexcept
Convert a string to a single precision real number.

◆ from_chars() [10/20]

bool c4::from_chars ( csubstr buf,
double * v )
inlinenoexcept

Definition at line 2391 of file charconv.hpp.

2391{ return atod(buf, v); }
bool atod(csubstr str, double *v) noexcept
Convert a string to a double precision real number.

◆ from_chars() [11/20]

template<class T>
auto c4::from_chars ( csubstr buf,
T * v )->bool::type
inlinenoexcept

Definition at line 2393 of file charconv.hpp.

2393{ return atoi(buf, v); }

◆ from_chars() [12/20]

template<class T>
bool c4::from_chars ( csubstr buf,
T ** v )
inlinenoexcept

Definition at line 2396 of file charconv.hpp.

2396{ intptr_t tmp; bool ret = from_chars(buf, &tmp); if(ret) { *v = (T*)tmp; } return ret; }
bool from_chars(csubstr buf, uint8_t *v) noexcept

◆ from_chars() [13/20]

bool c4::from_chars ( csubstr buf,
bool * v )
inlinenoexcept

Definition at line 2465 of file charconv.hpp.

2466{
2467 if(buf.len == 1)
2468 {
2469 if(buf.str[0] == '0')
2470 {
2471 *v = false; return true;
2472 }
2473 else if(buf.str[0] == '1')
2474 {
2475 *v = true; return true;
2476 }
2477 }
2478 else if(buf.len == 4)
2479 {
2480 if(((buf.str[0] == 't') && (0 == memcmp(buf.str + 1, "rue", 3)))
2481 ||
2482 ((buf.str[0] == 'T') && (0 == memcmp(buf.str + 1, "rue", 3) ||
2483 0 == memcmp(buf.str + 1, "RUE", 3))))
2484 {
2485 *v = true; return true;
2486 }
2487 }
2488 else if(buf.len == 5)
2489 {
2490 if(((buf.str[0] == 'f') && (0 == memcmp(buf.str + 1, "alse", 4)))
2491 ||
2492 ((buf.str[0] == 'F') && (0 == memcmp(buf.str + 1, "alse", 4) ||
2493 0 == memcmp(buf.str + 1, "ALSE", 4))))
2494 {
2495 *v = false; return true;
2496 }
2497 }
2498 // fallback to c-style int bools
2499 int val = 0;
2500 bool ret = from_chars(buf, &val);
2501 if(C4_LIKELY(ret))
2502 {
2503 *v = (val != 0);
2504 }
2505 return ret;
2506}
size_t len
the length of the substring
Definition substr.hpp:218
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

◆ from_chars() [14/20]

bool c4::from_chars ( csubstr buf,
char * v )
inlinenoexcept

extract a single character from a substring

Note
to extract a string instead and not just a single character, use the csubstr overload

Definition at line 2536 of file charconv.hpp.

2537{
2538 if(buf.len != 1)
2539 return false;
2540 C4_XASSERT(buf.str);
2541 *v = buf.str[0];
2542 return true;
2543}

◆ from_chars() [15/20]

bool c4::from_chars ( csubstr buf,
csubstr * v )
inlinenoexcept

Definition at line 2576 of file charconv.hpp.

2577{
2578 *v = buf;
2579 return true;
2580}

◆ from_chars() [16/20]

bool c4::from_chars ( csubstr buf,
substr * v )
inlinenoexcept

Definition at line 2614 of file charconv.hpp.

2615{
2616 C4_ASSERT(!buf.overlaps(*v));
2617 // is the destination buffer wide enough?
2618 if(v->len >= buf.len)
2619 {
2620 // calling memcpy with zero len is undefined behavior
2621 // and will wreak havoc in calling code's branches.
2622 // see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-1262133637
2623 if(buf.len)
2624 {
2625 C4_ASSERT(buf.str != nullptr);
2626 C4_ASSERT(v->str != nullptr);
2627 memcpy(v->str, buf.str, buf.len);
2628 }
2629 v->len = buf.len;
2630 return true;
2631 }
2632 return false;
2633}
bool overlaps(ro_substr const that) const noexcept
true if there is overlap of at least one element between that and *this
Definition substr.hpp:494

◆ from_chars() [17/20]

template<class T>
bool c4::from_chars ( csubstr s,
fmt::overflow_checked_< T > wrapper )
inline

read an integer type, detecting overflow (returns false on overflow)

Definition at line 322 of file format.hpp.

323{
324 if(C4_LIKELY(!overflows<T>(s)))
325 return atox(s, wrapper.val);
326 return false;
327}
bool atox(csubstr s, uint8_t *v) noexcept
auto overflows(csubstr str) noexcept -> typename std::enable_if< std::is_unsigned< T >::value, bool >::type
Test if the following string would overflow when converted to associated integral types; this functio...

◆ from_chars() [18/20]

template<class T>
bool c4::from_chars ( csubstr s,
fmt::overflow_checked_< T > * wrapper )
inline

read an integer type, detecting overflow (returns false on overflow)

Definition at line 331 of file format.hpp.

332{
333 if(C4_LIKELY(!overflows<T>(s)))
334 return atox(s, wrapper->val);
335 return false;
336}

◆ from_chars() [19/20]

bool c4::from_chars ( csubstr buf,
fmt::raw_wrapper * r )

read a variable in raw binary format, using memcpy

◆ from_chars() [20/20]

bool c4::from_chars ( csubstr buf,
fmt::raw_wrapper r )
inline

read a variable in raw binary format, using memcpy

Definition at line 465 of file format.hpp.

466{
467 return from_chars(buf, &r);
468}