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

Function process_suffixed_number

src/numfmt.c:1336–1388  ·  view source on GitHub ↗

Converts the TEXT number string to the requested representation, and handles automatic suffix addition. */

Source from the content-addressed store, hash-verified

1334/* Converts the TEXT number string to the requested representation,
1335 and handles automatic suffix addition. */
1336static bool
1337process_suffixed_number (char *text, long double *result,
1338 size_t *precision, long int field)
1339{
1340 char saved_suffix = '\0';
1341
1342 if (suffix)
1343 {
1344 if (mbs_endswith (text, suffix))
1345 {
1346 saved_suffix = *(text + strlen (text) - strlen (suffix));
1347 *(text + strlen (text) - strlen (suffix)) = '\0';
1348 devmsg ("trimming suffix %s\n", quote (suffix));
1349 }
1350 else
1351 devmsg ("no valid suffix found\n");
1352 }
1353
1354 /* Skip blanks - always. */
1355 char *p = skip_str_matching (text, newline_or_blank, true);
1356
1357 /* setup auto-padding. */
1358 if (auto_padding)
1359 {
1360 padding_width = text < p || 1 < field
1361 ? mbswidth (text,
1362 MBSW_REJECT_INVALID | MBSW_REJECT_UNPRINTABLE)
1363 : 0;
1364 if (padding_width < 0)
1365 padding_width = strlen (text);
1366 devmsg ("setting Auto-Padding to %jd characters\n", padding_width);
1367 }
1368
1369 long double val = 0;
1370 enum simple_strtod_error e = parse_human_number (p, &val, precision);
1371 if (e == SSE_OK_PRECISION_LOSS && debug)
1372 error (0, 0, _("large input value %s: possible precision loss"),
1373 quote (p));
1374
1375 if (from_unit_size != 1 || to_unit_size != 1)
1376 val = (val * from_unit_size) / to_unit_size;
1377
1378 *result = val;
1379
1380 if (e == SSE_OK || e == SSE_OK_PRECISION_LOSS)
1381 return true;
1382 else
1383 {
1384 if (saved_suffix)
1385 *(text + strlen (text)) = saved_suffix;
1386 return false;
1387 }
1388}
1389
1390/* Search for multi-byte character C in multi-byte string S.
1391 Return a pointer to the character, or NULL if not found. */

Callers 1

process_fieldFunction · 0.85

Calls 2

skip_str_matchingFunction · 0.85
parse_human_numberFunction · 0.85

Tested by

no test coverage detected