rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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

template<class T >
size_t c4::itoa ( substr  buf,
T  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 1120 of file charconv.hpp.

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

1121 {
1122  C4_STATIC_ASSERT(std::is_signed<T>::value);
1123  if(v >= T(0))
1124  {
1125  // write_dec() checks the buffer size, so no need to check here
1126  return write_dec(buf, v);
1127  }
1128  // when T is the min value (eg i8: -128), negating it
1129  // will overflow, so treat the min as a special case
1130  if C4_LIKELY(v != std::numeric_limits<T>::min())
1131  {
1132  v = (T)-v;
1133  unsigned digits = digits_dec(v);
1134  if C4_LIKELY(buf.len >= digits + 1u)
1135  {
1136  buf.str[0] = '-';
1137  write_dec_unchecked(buf.sub(1), v, digits);
1138  }
1139  return digits + 1u;
1140  }
1141  return detail::_itoadec2buf<T>(buf);
1142 }
void write_dec_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:614
auto digits_dec(T v) noexcept-> typename std::enable_if< sizeof(T)==1u, unsigned >::type
decimal digits for 8 bit integers
Definition: charconv.hpp:432
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:812
template<class T >
size_t c4::itoa ( substr  buf,
T  v,
T  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 1152 of file charconv.hpp.

1153 {
1154  C4_STATIC_ASSERT(std::is_signed<T>::value);
1155  C4_ASSERT(radix == 2 || radix == 8 || radix == 10 || radix == 16);
1156  C4_SUPPRESS_WARNING_GCC_PUSH
1157  #if (defined(__GNUC__) && (__GNUC__ >= 7))
1158  C4_SUPPRESS_WARNING_GCC("-Wstringop-overflow") // gcc has a false positive here
1159  #endif
1160  // when T is the min value (eg i8: -128), negating it
1161  // will overflow, so treat the min as a special case
1162  if C4_LIKELY(v != std::numeric_limits<T>::min())
1163  {
1164  unsigned pos = 0;
1165  if(v < 0)
1166  {
1167  v = (T)-v;
1168  if C4_LIKELY(buf.len > 0)
1169  buf.str[pos] = '-';
1170  ++pos;
1171  }
1172  unsigned digits = 0;
1173  switch(radix) // NOLINT(hicpp-multiway-paths-covered)
1174  {
1175  case T(10):
1176  digits = digits_dec(v);
1177  if C4_LIKELY(buf.len >= pos + digits)
1178  write_dec_unchecked(buf.sub(pos), v, digits);
1179  break;
1180  case T(16):
1181  digits = digits_hex(v);
1182  if C4_LIKELY(buf.len >= pos + 2u + digits)
1183  {
1184  buf.str[pos + 0] = '0';
1185  buf.str[pos + 1] = 'x';
1186  write_hex_unchecked(buf.sub(pos + 2), v, digits);
1187  }
1188  digits += 2u;
1189  break;
1190  case T(2):
1191  digits = digits_bin(v);
1192  if C4_LIKELY(buf.len >= pos + 2u + digits)
1193  {
1194  buf.str[pos + 0] = '0';
1195  buf.str[pos + 1] = 'b';
1196  write_bin_unchecked(buf.sub(pos + 2), v, digits);
1197  }
1198  digits += 2u;
1199  break;
1200  case T(8):
1201  digits = digits_oct(v);
1202  if C4_LIKELY(buf.len >= pos + 2u + digits)
1203  {
1204  buf.str[pos + 0] = '0';
1205  buf.str[pos + 1] = 'o';
1206  write_oct_unchecked(buf.sub(pos + 2), v, digits);
1207  }
1208  digits += 2u;
1209  break;
1210  }
1211  return pos + digits;
1212  }
1213  C4_SUPPRESS_WARNING_GCC_POP
1214  // when T is the min value (eg i8: -128), negating it
1215  // will overflow
1216  return detail::_itoa2buf<T>(buf, radix);
1217 }
void write_dec_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:614
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
Definition: charconv.hpp:538
void write_oct_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:663
auto digits_dec(T v) noexcept-> typename std::enable_if< sizeof(T)==1u, unsigned >::type
decimal digits for 8 bit integers
Definition: charconv.hpp:432
const char * str
Definition: node_type.cpp:70
unsigned digits_oct(T v_) noexcept
return the number of digits required to encode an octal number.
Definition: charconv.hpp:547
void write_hex_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:647
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
Definition: charconv.hpp:529
void write_bin_unchecked(substr buf, T v, unsigned digits_v) noexcept
Definition: charconv.hpp:679
template<class T >
size_t c4::itoa ( substr  buf,
T  v,
T  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 1229 of file charconv.hpp.

1230 {
1231  C4_STATIC_ASSERT(std::is_signed<T>::value);
1232  C4_ASSERT(radix == 2 || radix == 8 || radix == 10 || radix == 16);
1233  C4_SUPPRESS_WARNING_GCC_PUSH
1234  #if (defined(__GNUC__) && (__GNUC__ >= 7))
1235  C4_SUPPRESS_WARNING_GCC("-Wstringop-overflow") // gcc has a false positive here
1236  #endif
1237  // when T is the min value (eg i8: -128), negating it
1238  // will overflow, so treat the min as a special case
1239  if C4_LIKELY(v != std::numeric_limits<T>::min())
1240  {
1241  unsigned pos = 0;
1242  if(v < 0)
1243  {
1244  v = (T)-v;
1245  if C4_LIKELY(buf.len > 0)
1246  buf.str[pos] = '-';
1247  ++pos;
1248  }
1249  unsigned total_digits = 0;
1250  switch(radix) // NOLINT(hicpp-multiway-paths-covered)
1251  {
1252  case T(10):
1253  total_digits = digits_dec(v);
1254  total_digits = pos + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1255  if C4_LIKELY(buf.len >= total_digits)
1256  write_dec(buf.sub(pos), v, num_digits);
1257  break;
1258  case T(16):
1259  total_digits = digits_hex(v);
1260  total_digits = pos + 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1261  if C4_LIKELY(buf.len >= total_digits)
1262  {
1263  buf.str[pos + 0] = '0';
1264  buf.str[pos + 1] = 'x';
1265  write_hex(buf.sub(pos + 2), v, num_digits);
1266  }
1267  break;
1268  case T(2):
1269  total_digits = digits_bin(v);
1270  total_digits = pos + 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1271  if C4_LIKELY(buf.len >= total_digits)
1272  {
1273  buf.str[pos + 0] = '0';
1274  buf.str[pos + 1] = 'b';
1275  write_bin(buf.sub(pos + 2), v, num_digits);
1276  }
1277  break;
1278  case T(8):
1279  total_digits = digits_oct(v);
1280  total_digits = pos + 2u + (unsigned)(num_digits > total_digits ? num_digits : total_digits);
1281  if C4_LIKELY(buf.len >= total_digits)
1282  {
1283  buf.str[pos + 0] = '0';
1284  buf.str[pos + 1] = 'o';
1285  write_oct(buf.sub(pos + 2), v, num_digits);
1286  }
1287  break;
1288  }
1289  return total_digits;
1290  }
1291  C4_SUPPRESS_WARNING_GCC_POP
1292  // when T is the min value (eg i8: -128), negating it
1293  // will overflow
1294  return detail::_itoa2buf<T>(buf, radix, num_digits);
1295 }
unsigned digits_bin(T v) noexcept
return the number of digits required to encode a binary number.
Definition: charconv.hpp:538
auto digits_dec(T v) noexcept-> typename std::enable_if< sizeof(T)==1u, unsigned >::type
decimal digits for 8 bit integers
Definition: charconv.hpp:432
const char * str
Definition: node_type.cpp:70
unsigned digits_oct(T v_) noexcept
return the number of digits required to encode an octal number.
Definition: charconv.hpp:547
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:821
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: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:830
unsigned digits_hex(T v) noexcept
return the number of digits required to encode an hexadecimal number.
Definition: charconv.hpp:529
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:839