rapidyaml  0.11.0
parse and emit YAML, and do it fast
itoa: signed to chars

Functions

template<class T >
size_t c4::itoa (substr buf, T v) noexcept
 convert an integral signed decimal to a string. More...
 
template<class T >
size_t c4::itoa (substr buf, T v, T radix) noexcept
 convert an integral signed integer to a string, using a specific radix. More...
 
template<class T >
size_t c4::itoa (substr buf, T v, T radix, size_t num_digits) noexcept
 same as c4::itoa(), but pad with zeroes on the left such that the resulting string is num_digits wide, not accounting for radix prefix (0x,0o,0b). More...
 

Detailed Description

Function Documentation

◆ itoa() [1/3]

template<class T >
size_t c4::itoa ( substr  buf,
v 
)
inlinenoexcept

convert an integral signed decimal to a string.

Note
the resulting string is NOT zero-terminated.
it is ok to call this with an empty or too-small buffer; no writes will occur, and the needed size will be returned
Returns
the number of characters required for the buffer.

Definition at line 1107 of file charconv.hpp.

1108 {
1109  C4_STATIC_ASSERT(std::is_signed<T>::value);
1110  if(v >= T(0))
1111  {
1112  // write_dec() checks the buffer size, so no need to check here
1113  return write_dec(buf, v);
1114  }
1115  // when T is the min value (eg i8: -128), negating it
1116  // will overflow, so treat the min as a special case
1117  if(C4_LIKELY(v != std::numeric_limits<T>::min()))
1118  {
1119  v = (T)-v;
1120  unsigned digits = digits_dec(v);
1121  if(C4_LIKELY(buf.len >= digits + 1u))
1122  {
1123  buf.str[0] = '-';
1124  write_dec_unchecked(buf.sub(1), v, digits);
1125  }
1126  return digits + 1u;
1127  }
1128  return detail::_itoadec2buf<T>(buf);
1129 }
auto digits_dec(T v) noexcept -> typename std::enable_if< sizeof(T)==1u, unsigned >::type
decimal digits for 8 bit integers
Definition: charconv.hpp:433
void write_dec_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:601
size_t write_dec(substr buf, T val, size_t num_digits) noexcept
same as c4::write_dec(), but pad with zeroes on the left such that the resulting string is num_digits...
Definition: charconv.hpp:799

References c4::digits_dec(), c4::write_dec(), and c4::write_dec_unchecked().

◆ itoa() [2/3]

template<class T >
size_t c4::itoa ( substr  buf,
v,
radix 
)
inlinenoexcept

convert an integral signed integer to a string, using a specific radix.

The radix must be 2, 8, 10 or 16.

Note
the resulting string is NOT zero-terminated.
it is ok to call this with an empty or too-small buffer; no writes will occur, and the needed size will be returned
Returns
the number of characters required for the buffer.

Definition at line 1139 of file charconv.hpp.

1140 {
1141  C4_STATIC_ASSERT(std::is_signed<T>::value);
1142  C4_ASSERT(radix == 2 || radix == 8 || radix == 10 || radix == 16);
1143  C4_SUPPRESS_WARNING_GCC_PUSH
1144  #if (defined(__GNUC__) && (__GNUC__ >= 7))
1145  C4_SUPPRESS_WARNING_GCC("-Wstringop-overflow") // gcc has a false positive here
1146  #endif
1147  // when T is the min value (eg i8: -128), negating it
1148  // will overflow, so treat the min as a special case
1149  if(C4_LIKELY(v != std::numeric_limits<T>::min()))
1150  {
1151  unsigned pos = 0;
1152  if(v < 0)
1153  {
1154  v = (T)-v;
1155  if(C4_LIKELY(buf.len > 0))
1156  buf.str[pos] = '-';
1157  ++pos;
1158  }
1159  unsigned digits = 0;
1160  switch(radix) // NOLINT(hicpp-multiway-paths-covered)
1161  {
1162  case T(10):
1163  digits = digits_dec(v);
1164  if(C4_LIKELY(buf.len >= pos + digits))
1165  write_dec_unchecked(buf.sub(pos), v, digits);
1166  break;
1167  case T(16):
1168  digits = digits_hex(v);
1169  if(C4_LIKELY(buf.len >= pos + 2u + digits))
1170  {
1171  buf.str[pos + 0] = '0';
1172  buf.str[pos + 1] = 'x';
1173  write_hex_unchecked(buf.sub(pos + 2), v, digits);
1174  }
1175  digits += 2u;
1176  break;
1177  case T(2):
1178  digits = digits_bin(v);
1179  if(C4_LIKELY(buf.len >= pos + 2u + digits))
1180  {
1181  buf.str[pos + 0] = '0';
1182  buf.str[pos + 1] = 'b';
1183  write_bin_unchecked(buf.sub(pos + 2), v, digits);
1184  }
1185  digits += 2u;
1186  break;
1187  case T(8):
1188  digits = digits_oct(v);
1189  if(C4_LIKELY(buf.len >= pos + 2u + digits))
1190  {
1191  buf.str[pos + 0] = '0';
1192  buf.str[pos + 1] = 'o';
1193  write_oct_unchecked(buf.sub(pos + 2), v, digits);
1194  }
1195  digits += 2u;
1196  break;
1197  }
1198  return pos + digits;
1199  }
1200  C4_SUPPRESS_WARNING_GCC_POP
1201  // when T is the min value (eg i8: -128), negating it
1202  // will overflow
1203  return detail::_itoa2buf<T>(buf, radix);
1204 }
unsigned digits_oct(T v_) noexcept
return the number of digits required to encode an octal number.
Definition: charconv.hpp:534
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
Definition: charconv.hpp:516
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
Definition: charconv.hpp:525
void write_bin_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:666
void write_hex_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:634
void write_oct_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:650

References c4::digits_bin(), c4::digits_dec(), c4::digits_hex(), c4::digits_oct(), c4::write_bin_unchecked(), c4::write_dec_unchecked(), c4::write_hex_unchecked(), and c4::write_oct_unchecked().

◆ itoa() [3/3]

template<class T >
size_t c4::itoa ( substr  buf,
v,
radix,
size_t  num_digits 
)
inlinenoexcept

same as c4::itoa(), but pad with zeroes on the left such that the resulting string is num_digits wide, not accounting for radix prefix (0x,0o,0b).

The radix must be 2, 8, 10 or 16.

Note
the resulting string is NOT zero-terminated.
it is ok to call this with an empty or too-small buffer; no writes will occur, and the needed size will be returned
Returns
the number of characters required for the buffer.

Definition at line 1216 of file charconv.hpp.

1217 {
1218  C4_STATIC_ASSERT(std::is_signed<T>::value);
1219  C4_ASSERT(radix == 2 || radix == 8 || radix == 10 || radix == 16);
1220  C4_SUPPRESS_WARNING_GCC_PUSH
1221  #if (defined(__GNUC__) && (__GNUC__ >= 7))
1222  C4_SUPPRESS_WARNING_GCC("-Wstringop-overflow") // gcc has a false positive here
1223  #endif
1224  // when T is the min value (eg i8: -128), negating it
1225  // will overflow, so treat the min as a special case
1226  if(C4_LIKELY(v != std::numeric_limits<T>::min()))
1227  {
1228  unsigned pos = 0;
1229  if(v < 0)
1230  {
1231  v = (T)-v;
1232  if(C4_LIKELY(buf.len > 0))
1233  buf.str[pos] = '-';
1234  ++pos;
1235  }
1236  unsigned total_digits = 0;
1237  switch(radix) // NOLINT(hicpp-multiway-paths-covered)
1238  {
1239  case T(10):
1240  total_digits = digits_dec(v);
1241  total_digits = pos + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1242  if(C4_LIKELY(buf.len >= total_digits))
1243  write_dec(buf.sub(pos), v, num_digits);
1244  break;
1245  case T(16):
1246  total_digits = digits_hex(v);
1247  total_digits = pos + 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1248  if(C4_LIKELY(buf.len >= total_digits))
1249  {
1250  buf.str[pos + 0] = '0';
1251  buf.str[pos + 1] = 'x';
1252  write_hex(buf.sub(pos + 2), v, num_digits);
1253  }
1254  break;
1255  case T(2):
1256  total_digits = digits_bin(v);
1257  total_digits = pos + 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1258  if(C4_LIKELY(buf.len >= total_digits))
1259  {
1260  buf.str[pos + 0] = '0';
1261  buf.str[pos + 1] = 'b';
1262  write_bin(buf.sub(pos + 2), v, num_digits);
1263  }
1264  break;
1265  case T(8):
1266  total_digits = digits_oct(v);
1267  total_digits = pos + 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1268  if(C4_LIKELY(buf.len >= total_digits))
1269  {
1270  buf.str[pos + 0] = '0';
1271  buf.str[pos + 1] = 'o';
1272  write_oct(buf.sub(pos + 2), v, num_digits);
1273  }
1274  break;
1275  }
1276  return total_digits;
1277  }
1278  C4_SUPPRESS_WARNING_GCC_POP
1279  // when T is the min value (eg i8: -128), negating it
1280  // will overflow
1281  return detail::_itoa2buf<T>(buf, radix, num_digits);
1282 }
size_t write_hex(substr buf, T val, size_t num_digits) noexcept
same as c4::write_hex(), but pad with zeroes on the left such that the resulting string is num_digits...
Definition: charconv.hpp:808
size_t write_bin(substr buf, T val, size_t num_digits) noexcept
same as c4::write_bin(), but pad with zeroes on the left such that the resulting string is num_digits...
Definition: charconv.hpp:817
size_t write_oct(substr buf, T val, size_t num_digits) noexcept
same as c4::write_oct(), but pad with zeroes on the left such that the resulting string is num_digits...
Definition: charconv.hpp:826

References c4::digits_bin(), c4::digits_dec(), c4::digits_hex(), c4::digits_oct(), c4::write_bin(), c4::write_dec(), c4::write_hex(), and c4::write_oct().