rapidyaml  0.7.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 1109 of file charconv.hpp.

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

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

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

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

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

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