rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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...
 
bool c4::from_chars (csubstr buf, fmt::base64_wrapper const &b)
 (1) read a variable in base64 format More...
 
bool c4::from_chars (csubstr buf, fmt::base64_wrapper *b)
 (2) read a variable in base64 format More...
 
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 More...
 
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 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

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

Definition at line 2383 of file charconv.hpp.

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

2383 { return atou(buf, v); }
bool atou(csubstr str, T *v) noexcept
Convert a trimmed string to an unsigned integral value.
Definition: charconv.hpp:1558
bool c4::from_chars ( csubstr  buf,
uint16_t *  v 
)
inlinenoexcept

Definition at line 2384 of file charconv.hpp.

2384 { return atou(buf, v); }
bool atou(csubstr str, T *v) noexcept
Convert a trimmed string to an unsigned integral value.
Definition: charconv.hpp:1558
bool c4::from_chars ( csubstr  buf,
uint32_t *  v 
)
inlinenoexcept

Definition at line 2385 of file charconv.hpp.

2385 { return atou(buf, v); }
bool atou(csubstr str, T *v) noexcept
Convert a trimmed string to an unsigned integral value.
Definition: charconv.hpp:1558
bool c4::from_chars ( csubstr  buf,
uint64_t *  v 
)
inlinenoexcept

Definition at line 2386 of file charconv.hpp.

2386 { return atou(buf, v); }
bool atou(csubstr str, T *v) noexcept
Convert a trimmed string to an unsigned integral value.
Definition: charconv.hpp:1558
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.
Definition: charconv.hpp:1466
bool c4::from_chars ( csubstr  buf,
int16_t *  v 
)
inlinenoexcept

Definition at line 2388 of file charconv.hpp.

2388 { return atoi(buf, v); }
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
Definition: charconv.hpp:1466
bool c4::from_chars ( csubstr  buf,
int32_t *  v 
)
inlinenoexcept

Definition at line 2389 of file charconv.hpp.

2389 { return atoi(buf, v); }
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
Definition: charconv.hpp:1466
bool c4::from_chars ( csubstr  buf,
int64_t *  v 
)
inlinenoexcept

Definition at line 2390 of file charconv.hpp.

2390 { return atoi(buf, v); }
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
Definition: charconv.hpp:1466
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.
Definition: charconv.hpp:2121
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.
Definition: charconv.hpp:2182
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); }
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
Definition: charconv.hpp:1466
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, substr *v) noexcept
Definition: charconv.hpp:2615
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 }
bool from_chars(csubstr buf, substr *v) noexcept
Definition: charconv.hpp:2615
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 }
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 }
bool c4::from_chars ( csubstr  buf,
substr *  v 
)
inlinenoexcept

Definition at line 2615 of file charconv.hpp.

Referenced by c4::atod(), and c4::atof().

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 }
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
Definition: charconv.hpp:2302
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 }
bool atox(csubstr s, uint8_t *v) noexcept
Definition: charconv.hpp:2302
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 }
const char * str
Definition: node_type.cpp:70
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 }
bool from_chars(csubstr buf, fmt::raw_wrapper r)
read a variable in raw binary format, using memcpy
Definition: format.hpp:465
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
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 }
bool 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

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 }
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
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 }
bool 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 ...