rapidyaml 0.15.2
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
bool c4::from_chars (csubstr buf, fmt::base64_wrapper const &b)
 (1) read a variable in base64 format
bool c4::from_chars (csubstr buf, fmt::base64_wrapper *b)
 (2) read a variable in base64 format
template<class T>
bool c4::from_chars (csubstr buf, fmt::base64_container_wrapper< T > const &b)
 read a container in base64 format, resizing it as needed to accomodate the result
template<class T>
bool c4::from_chars (csubstr buf, fmt::base64_container_wrapper< T > const *b)
 read a container in base64 format, resizing it as needed to accomodate the result

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/24]

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

Definition at line 2383 of file charconv.hpp.

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

Referenced by from_chars(), from_chars(), from_chars(), from_chars_first(), from_chars_first(), from_chars_first(), c4::yml::from_chars_float(), c4::yml::from_chars_integral(), c4::yml::read(), c4::yml::read(), c4::yml::scalar_deserialize(), and uncatsep().

◆ from_chars() [2/24]

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

Definition at line 2384 of file charconv.hpp.

2384{ return atou(buf, v); }

◆ from_chars() [3/24]

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

Definition at line 2385 of file charconv.hpp.

2385{ return atou(buf, v); }

◆ from_chars() [4/24]

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

Definition at line 2386 of file charconv.hpp.

2386{ return atou(buf, v); }

◆ from_chars() [5/24]

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

Definition at line 2387 of file charconv.hpp.

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

◆ from_chars() [6/24]

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

Definition at line 2388 of file charconv.hpp.

2388{ return atoi(buf, v); }

◆ from_chars() [7/24]

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

Definition at line 2389 of file charconv.hpp.

2389{ return atoi(buf, v); }

◆ from_chars() [8/24]

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

Definition at line 2390 of file charconv.hpp.

2390{ return atoi(buf, v); }

◆ from_chars() [9/24]

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

Definition at line 2391 of file charconv.hpp.

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

◆ from_chars() [10/24]

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

Definition at line 2392 of file charconv.hpp.

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

◆ from_chars() [11/24]

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

Definition at line 2394 of file charconv.hpp.

2394{ return atoi(buf, v); }

◆ from_chars() [12/24]

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

Definition at line 2397 of file charconv.hpp.

2397{ 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/24]

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

Definition at line 2466 of file charconv.hpp.

2467{
2468 if(buf.len == 1)
2469 {
2470 if(buf.str[0] == '0')
2471 {
2472 *v = false; return true;
2473 }
2474 else if(buf.str[0] == '1')
2475 {
2476 *v = true; return true;
2477 }
2478 }
2479 else if(buf.len == 4)
2480 {
2481 if(((buf.str[0] == 't') && (0 == memcmp(buf.str + 1, "rue", 3)))
2482 ||
2483 ((buf.str[0] == 'T') && (0 == memcmp(buf.str + 1, "rue", 3) ||
2484 0 == memcmp(buf.str + 1, "RUE", 3))))
2485 {
2486 *v = true; return true;
2487 }
2488 }
2489 else if(buf.len == 5)
2490 {
2491 if(((buf.str[0] == 'f') && (0 == memcmp(buf.str + 1, "alse", 4)))
2492 ||
2493 ((buf.str[0] == 'F') && (0 == memcmp(buf.str + 1, "alse", 4) ||
2494 0 == memcmp(buf.str + 1, "ALSE", 4))))
2495 {
2496 *v = false; return true;
2497 }
2498 }
2499 // fallback to c-style int bools
2500 int val = 0;
2501 bool ret = from_chars(buf, &val);
2502 if C4_LIKELY(ret)
2503 {
2504 *v = (val != 0);
2505 }
2506 return ret;
2507}
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/24]

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 2537 of file charconv.hpp.

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

◆ from_chars() [15/24]

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

Definition at line 2577 of file charconv.hpp.

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

◆ from_chars() [16/24]

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

Definition at line 2615 of file charconv.hpp.

2616{
2617 C4_ASSERT(!buf.overlaps(*v));
2618 // is the destination buffer wide enough?
2619 if(v->len >= buf.len)
2620 {
2621 // calling memcpy with zero len is undefined behavior
2622 // and will wreak havoc in calling code's branches.
2623 // see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-1262133637
2624 if(buf.len)
2625 {
2626 C4_ASSERT(buf.str != nullptr);
2627 C4_ASSERT(v->str != nullptr);
2628 memcpy(v->str, buf.str, buf.len);
2629 }
2630 v->len = buf.len;
2631 return true;
2632 }
2633 return false;
2634}
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:493

◆ from_chars() [17/24]

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/24]

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/24]

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

read a variable in raw binary format, using memcpy

Definition at line 39 of file format.cpp.

40{
41 C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wcast-qual")
42 void * vptr = (void*)buf.str;
43 C4_SUPPRESS_WARNING_GCC_POP
44 size_t space = buf.len;
45 char * ptr = (char*) std::align(r->alignment, r->len, vptr, space);
46 C4_CHECK(ptr != nullptr);
47 C4_CHECK(ptr >= buf.begin() && ptr <= buf.end());
48 C4_SUPPRESS_WARNING_GCC_PUSH
49 #if defined(__GNUC__) && __GNUC__ > 9
50 C4_SUPPRESS_WARNING_GCC("-Wanalyzer-null-argument")
51 #endif
52 memcpy(r->buf, ptr, r->len);
53 C4_SUPPRESS_WARNING_GCC_POP
54 return true;
55}

◆ from_chars() [20/24]

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}

◆ from_chars() [21/24]

bool c4::from_chars ( csubstr buf,
fmt::base64_wrapper const & b )
inline

(1) read a variable in base64 format

Definition at line 219 of file format_base64.hpp.

220{
221 size_t reqsize = 0;
222 bool ok = base64_decode(buf.str, buf.len, b.data.buf, b.data.len, &reqsize);
223 if(b.required_size)
224 *b.required_size = reqsize;
225 return ok;
226}
bool base64_decode(char const *encoded, size_t encoded_sz, void *data, size_t data_sz, size_t *data_sz_required)
decode the base64 encoding in the given buffer.
Definition base64.cpp:412

◆ from_chars() [22/24]

bool c4::from_chars ( csubstr buf,
fmt::base64_wrapper * b )
inline

(2) read a variable in base64 format

Definition at line 229 of file format_base64.hpp.

230{
231 return from_chars(buf, *b);
232}

◆ from_chars() [23/24]

template<class T>
bool c4::from_chars ( csubstr buf,
fmt::base64_container_wrapper< T > const & b )

read a container in base64 format, resizing it as needed to accomodate the result

Definition at line 261 of file format_base64.hpp.

262{
263 enum : size_t { elm_sz = sizeof(typename fmt::base64_container_wrapper<T>::value_type) }; // NOLINT
264 blob data = b.data();
265 size_t required_size = 0;
266 bool ok = base64_decode(buf.str, buf.len, data.buf, data.len, &required_size);
267 if(b.required_size)
268 *b.required_size = required_size;
269 if(!required_size)
270 return ok;
271 else if(!ok && ((required_size < data.len) || (required_size % elm_sz)))
272 return false;
273 size_t num_elms = required_size / elm_sz;
274 b.container->resize(num_elms);
275 if(required_size > data.len)
276 {
277 data = b.data();
278 ok = base64_decode(buf.str, buf.len, data.buf, data.len, &required_size);
279 if(b.required_size)
280 *b.required_size = required_size;
281 }
282 return ok;
283}
detail::base64_container_wrapper_< Container, byte > base64_container_wrapper
a tag type to mark a payload to be encoded as base64

◆ from_chars() [24/24]

template<class T>
bool c4::from_chars ( csubstr buf,
fmt::base64_container_wrapper< T > const * b )

read a container in base64 format, resizing it as needed to accomodate the result

Definition at line 288 of file format_base64.hpp.

289{
290 return from_chars(buf, *b);
291}