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

Function parse_format_string

src/numfmt.c:1097–1201  ·  view source on GitHub ↗

Given 'fmt' (a printf(3) compatible format string), extracts the following: 1. padding (e.g. %20f) 2. alignment (e.g. %-20f) 3. grouping (e.g. %'f) Only a limited subset of printf(3) syntax is supported. TODO: support %e %g etc. rather than just %f NOTES: 1. This function sets the global variables: padding_width, grouping, format_str_prefix, format_str

Source from the content-addressed store, hash-verified

1095 format_str_prefix, format_str_suffix
1096 2. The function aborts on any errors. */
1097static void
1098parse_format_string (char const *fmt)
1099{
1100 size_t i;
1101 size_t prefix_len = 0;
1102 size_t suffix_pos;
1103 char *endptr = NULL;
1104 bool zero_padding = false;
1105
1106 for (i = 0; !(fmt[i] == '%' && fmt[i + 1] != '%'); i += (fmt[i] == '%') + 1)
1107 {
1108 if (!fmt[i])
1109 error (EXIT_FAILURE, 0,
1110 _("format %s has no %% directive"), quote (fmt));
1111 prefix_len++;
1112 }
1113
1114 i++;
1115 while (true)
1116 {
1117 size_t skip = strspn (fmt + i, " ");
1118 i += skip;
1119 if (fmt[i] == '\'')
1120 {
1121 grouping = 1;
1122 i++;
1123 }
1124 else if (fmt[i] == '0')
1125 {
1126 zero_padding = true;
1127 i++;
1128 }
1129 else if (! skip)
1130 break;
1131 }
1132
1133 intmax_t pad = strtoimax (fmt + i, &endptr, 10);
1134
1135 if (pad != 0)
1136 {
1137 if (debug && padding_width && !(zero_padding && pad > 0))
1138 error (0, 0, _("--format padding overriding --padding"));
1139
1140 /* Set padding width and alignment. On overflow, set widths to
1141 large values that cause later code to avoid undefined behavior
1142 and fail at a reasonable point. */
1143 if (pad < 0)
1144 padding_width = pad;
1145 else
1146 {
1147 if (zero_padding)
1148 zero_padding_width = MIN (pad, INT_MAX);
1149 else
1150 padding_width = pad;
1151 }
1152 }
1153 i = endptr - fmt;
1154

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected