rapidyaml  0.11.0
parse and emit YAML, and do it fast
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...

Modules

 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 More...
 
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) More...
 
template<class T >
bool c4::from_chars (csubstr s, fmt::overflow_checked_< T > *wrapper)
 read an integer type, detecting overflow (returns false on overflow) More...
 
bool c4::from_chars (csubstr buf, fmt::raw_wrapper *r)
 read a variable in raw binary format, using memcpy More...
 
bool c4::from_chars (csubstr buf, fmt::raw_wrapper r)
 read a variable in raw binary format, using memcpy More...
 
size_t c4::from_chars (csubstr buf, fmt::base64_wrapper *b)
 read a variable in base64 format More...
 

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

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

Definition at line 2366 of file charconv.hpp.

2366 { return atou(buf, v); }
bool atou(csubstr str, T *v) noexcept
Convert a trimmed string to an unsigned integral value.
Definition: charconv.hpp:1546

References c4::atou().

◆ from_chars() [2/21]

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

Definition at line 2367 of file charconv.hpp.

2367 { return atou(buf, v); }

References c4::atou().

◆ from_chars() [3/21]

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

Definition at line 2368 of file charconv.hpp.

2368 { return atou(buf, v); }

References c4::atou().

◆ from_chars() [4/21]

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

Definition at line 2369 of file charconv.hpp.

2369 { return atou(buf, v); }

References c4::atou().

◆ from_chars() [5/21]

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

Definition at line 2370 of file charconv.hpp.

2370 { return atoi(buf, v); }
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
Definition: charconv.hpp:1453

References c4::atoi().

◆ from_chars() [6/21]

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

Definition at line 2371 of file charconv.hpp.

2371 { return atoi(buf, v); }

References c4::atoi().

◆ from_chars() [7/21]

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

Definition at line 2372 of file charconv.hpp.

2372 { return atoi(buf, v); }

References c4::atoi().

◆ from_chars() [8/21]

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

Definition at line 2373 of file charconv.hpp.

2373 { return atoi(buf, v); }

References c4::atoi().

◆ from_chars() [9/21]

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

Definition at line 2374 of file charconv.hpp.

2374 { return atof(buf, v); }
bool atof(csubstr str, float *v) noexcept
Convert a string to a single precision real number.
Definition: charconv.hpp:2110

References c4::atof().

◆ from_chars() [10/21]

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

Definition at line 2375 of file charconv.hpp.

2375 { return atod(buf, v); }
bool atod(csubstr str, double *v) noexcept
Convert a string to a double precision real number.
Definition: charconv.hpp:2171

References c4::atod().

◆ from_chars() [11/21]

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

Definition at line 2377 of file charconv.hpp.

2377 { return atoi(buf, v); }

◆ from_chars() [12/21]

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

Definition at line 2380 of file charconv.hpp.

2380 { intptr_t tmp; bool ret = from_chars(buf, &tmp); if(ret) { *v = (T*)tmp; } return ret; }
bool from_chars(csubstr buf, substr *v) noexcept
Definition: charconv.hpp:2599

References c4::from_chars().

◆ from_chars() [13/21]

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

Definition at line 2449 of file charconv.hpp.

2450 {
2451  if(buf == '0')
2452  {
2453  *v = false; return true;
2454  }
2455  else if(buf == '1')
2456  {
2457  *v = true; return true;
2458  }
2459  else if(buf == "false")
2460  {
2461  *v = false; return true;
2462  }
2463  else if(buf == "true")
2464  {
2465  *v = true; return true;
2466  }
2467  else if(buf == "False")
2468  {
2469  *v = false; return true;
2470  }
2471  else if(buf == "True")
2472  {
2473  *v = true; return true;
2474  }
2475  else if(buf == "FALSE")
2476  {
2477  *v = false; return true;
2478  }
2479  else if(buf == "TRUE")
2480  {
2481  *v = true; return true;
2482  }
2483  // fallback to c-style int bools
2484  int val = 0;
2485  bool ret = from_chars(buf, &val);
2486  if(C4_LIKELY(ret))
2487  {
2488  *v = (val != 0);
2489  }
2490  return ret;
2491 }

References c4::from_chars().

◆ from_chars() [14/21]

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

2522 {
2523  if(buf.len != 1)
2524  return false;
2525  C4_XASSERT(buf.str);
2526  *v = buf.str[0];
2527  return true;
2528 }

◆ from_chars() [15/21]

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

Definition at line 2561 of file charconv.hpp.

2562 {
2563  *v = buf;
2564  return true;
2565 }

◆ from_chars() [16/21]

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

Definition at line 2599 of file charconv.hpp.

2600 {
2601  C4_ASSERT(!buf.overlaps(*v));
2602  // is the destination buffer wide enough?
2603  if(v->len >= buf.len)
2604  {
2605  // calling memcpy with null strings is undefined behavior
2606  // and will wreak havoc in calling code's branches.
2607  // see https://github.com/biojppm/rapidyaml/pull/264#issuecomment-1262133637
2608  if(buf.len)
2609  {
2610  C4_ASSERT(buf.str != nullptr);
2611  C4_ASSERT(v->str != nullptr);
2612  memcpy(v->str, buf.str, buf.len);
2613  }
2614  v->len = buf.len;
2615  return true;
2616  }
2617  return false;
2618 }

◆ from_chars() [17/21]

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 330 of file format.hpp.

331 {
332  if(C4_LIKELY(!overflows<T>(s)))
333  return atox(s, wrapper.val);
334  return false;
335 }
bool atox(csubstr s, uint8_t *v) noexcept
Definition: charconv.hpp:2291

References c4::atox(), and c4::fmt::overflow_checked_< T >::val.

◆ from_chars() [18/21]

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 339 of file format.hpp.

340 {
341  if(C4_LIKELY(!overflows<T>(s)))
342  return atox(s, wrapper->val);
343  return false;
344 }

References c4::atox(), and c4::fmt::overflow_checked_< T >::val.

◆ from_chars() [19/21]

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

read a variable in raw binary format, using memcpy

◆ from_chars() [20/21]

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

read a variable in raw binary format, using memcpy

Definition at line 473 of file format.hpp.

474 {
475  return from_chars(buf, &r);
476 }
bool from_chars(csubstr buf, fmt::raw_wrapper r)
read a variable in raw binary format, using memcpy
Definition: format.hpp:473

References c4::from_chars().

◆ from_chars() [21/21]

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

read a variable in base64 format

Definition at line 134 of file base64.hpp.

135 {
136  return base64_decode(buf, b->data);
137 }
size_t base64_decode(csubstr encoded, blob data)
decode the base64 encoding in the given buffer

References c4::base64_decode(), and c4::fmt::base64_wrapper_< CharOrConstChar >::data.