rapidyaml  0.8.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 1108 of file charconv.hpp.

1109 {
1110  C4_STATIC_ASSERT(std::is_signed<T>::value);
1111  if(v >= T(0))
1112  {
1113  // write_dec() checks the buffer size, so no need to check here
1114  return write_dec(buf, v);
1115  }
1116  // when T is the min value (eg i8: -128), negating it
1117  // will overflow, so treat the min as a special case
1118  if(C4_LIKELY(v != std::numeric_limits<T>::min()))
1119  {
1120  v = -v;
1121  unsigned digits = digits_dec(v);
1122  if(C4_LIKELY(buf.len >= digits + 1u))
1123  {
1124  buf.str[0] = '-';
1125  write_dec_unchecked(buf.sub(1), v, digits);
1126  }
1127  return digits + 1u;
1128  }
1129  return detail::_itoadec2buf<T>(buf);
1130 }
auto digits_dec(T v) noexcept -> typename std::enable_if< sizeof(T)==1u, unsigned >::type
decimal digits for 8 bit integers
Definition: charconv.hpp:434
void write_dec_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:602
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:800

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

Referenced by c4::to_chars(), and c4::xtoa().

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

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

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

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

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().

Referenced by c4::to_chars(), and c4::xtoa().