rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
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.
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.
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).

Detailed Description

Function Documentation

◆ itoa() [1/3]

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

1122{
1123 C4_STATIC_ASSERT(std::is_signed<T>::value);
1124 if(v >= T(0))
1125 {
1126 // write_dec() checks the buffer size, so no need to check here
1127 return write_dec(buf, v);
1128 }
1129 // when T is the min value (eg i8: -128), negating it
1130 // will overflow, so treat the min as a special case
1131 if(C4_LIKELY(v != std::numeric_limits<T>::min()))
1132 {
1133 v = (T)-v;
1134 unsigned digits = digits_dec(v);
1135 if(C4_LIKELY(buf.len >= digits + 1u))
1136 {
1137 buf.str[0] = '-';
1138 write_dec_unchecked(buf.sub(1), v, digits);
1139 }
1140 return digits + 1u;
1141 }
1142 return detail::_itoadec2buf<T>(buf);
1143}
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:615
size_t write_dec(substr buf, T v) noexcept
write an integer to a string in decimal format.
Definition charconv.hpp:715
size_t len
the length of the substring
Definition substr.hpp:218
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:503
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216

◆ itoa() [2/3]

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

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

◆ itoa() [3/3]

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

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