MCPcopy Create free account
hub / github.com/coreutils/coreutils / prepare_padded_number

Function prepare_padded_number

src/numfmt.c:1238–1314  ·  view source on GitHub ↗

Print the given VAL, using the requested representation. The number is printed to STDOUT, with padding and alignment. */

Source from the content-addressed store, hash-verified

1236/* Print the given VAL, using the requested representation.
1237 The number is printed to STDOUT, with padding and alignment. */
1238static bool
1239prepare_padded_number (const long double val, size_t precision,
1240 intmax_t *padding)
1241{
1242 /* Generate Output. */
1243 size_t precision_used = user_precision == -1 ? precision : user_precision;
1244
1245 /* Can't reliably print too-large values without auto-scaling. */
1246 int x;
1247 expld (val, 10, &x);
1248
1249 if (scale_to == scale_none
1250 && x + precision_used > MAX_UNSCALED_DIGITS)
1251 {
1252 if (inval_style != inval_ignore)
1253 {
1254 if (precision_used)
1255 error (conv_exit_code, 0,
1256 _("value/precision too large to be printed: '%Lg/%zu'"
1257 " (consider using --to)"), val, precision_used);
1258 else
1259 error (conv_exit_code, 0,
1260 _("value too large to be printed: '%Lg'"
1261 " (consider using --to)"), val);
1262 }
1263 return false;
1264 }
1265
1266 if (x > MAX_ACCEPTABLE_DIGITS - 1)
1267 {
1268 if (inval_style != inval_ignore)
1269 error (conv_exit_code, 0, _("value too large to be printed: '%Lg'"
1270 " (cannot handle values > 999Q)"), val);
1271 return false;
1272 }
1273
1274 while (true)
1275 {
1276 int numlen = double_to_human (val, precision_used,
1277 padding_buffer, padding_buffer_size,
1278 scale_to, grouping, round_style);
1279 ptrdiff_t growth;
1280 if (numlen < 0 || ckd_sub (&growth, numlen, padding_buffer_size - 1))
1281 error (EXIT_FAILURE, 0,
1282 _("failed to prepare value '%Lf' for printing"), val);
1283 if (growth <= 0)
1284 break;
1285 padding_buffer = xpalloc (padding_buffer, &padding_buffer_size,
1286 growth, -1, 1);
1287 }
1288
1289 devmsg ("formatting output:\n value: %Lf\n humanized: %s\n",
1290 val, quote (padding_buffer));
1291
1292 intmax_t pad = 0;
1293 if (padding_width)
1294 {
1295 int buf_width = mbswidth (padding_buffer,

Callers 1

process_fieldFunction · 0.85

Calls 2

expldFunction · 0.85
double_to_humanFunction · 0.85

Tested by

no test coverage detected