Remove ENABLE_SSSE3_ON_ATOM.
[glibc.git] / stdio-common / printf-parsemb.c
blobefd1eca3a2259856e19ba1616d1ce0e22258a441
1 /* Helper functions for parsing printf format strings.
2 Copyright (C) 1995-2000,2002-2004,2006,2009 Free Software Foundation, Inc.
3 This file is part of th GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/param.h>
25 #include <wchar.h>
26 #include <wctype.h>
28 #ifndef COMPILE_WPRINTF
29 # define CHAR_T char
30 # define UCHAR_T unsigned char
31 # define INT_T int
32 # define L_(Str) Str
33 # define ISDIGIT(Ch) isdigit (Ch)
34 # define HANDLE_REGISTERED_MODIFIER __handle_registered_modifier_mb
35 #else
36 # define CHAR_T wchar_t
37 # define UCHAR_T unsigned int
38 # define INT_T wint_t
39 # define L_(Str) L##Str
40 # define ISDIGIT(Ch) iswdigit (Ch)
41 # define HANDLE_REGISTERED_MODIFIER __handle_registered_modifier_wc
42 #endif
44 #include "printf-parse.h"
46 #define NDEBUG 1
47 #include <assert.h>
51 /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
52 with the parsed details. POSN is the number of arguments already
53 consumed. At most MAXTYPES - POSN types are filled in TYPES. Return
54 the number of args consumed by this spec; *MAX_REF_ARG is updated so it
55 remains the highest argument index used. */
56 size_t
57 attribute_hidden
58 #ifdef COMPILE_WPRINTF
59 __parse_one_specwc (const UCHAR_T *format, size_t posn,
60 struct printf_spec *spec, size_t *max_ref_arg)
61 #else
62 __parse_one_specmb (const UCHAR_T *format, size_t posn,
63 struct printf_spec *spec, size_t *max_ref_arg)
64 #endif
66 unsigned int n;
67 size_t nargs = 0;
69 /* Skip the '%'. */
70 ++format;
72 /* Clear information structure. */
73 spec->data_arg = -1;
74 spec->info.alt = 0;
75 spec->info.space = 0;
76 spec->info.left = 0;
77 spec->info.showsign = 0;
78 spec->info.group = 0;
79 spec->info.i18n = 0;
80 spec->info.extra = 0;
81 spec->info.pad = ' ';
82 spec->info.wide = sizeof (UCHAR_T) > 1;
84 /* Test for positional argument. */
85 if (ISDIGIT (*format))
87 const UCHAR_T *begin = format;
89 n = read_int (&format);
91 if (n > 0 && *format == L_('$'))
92 /* Is positional parameter. */
94 ++format; /* Skip the '$'. */
95 spec->data_arg = n - 1;
96 *max_ref_arg = MAX (*max_ref_arg, n);
98 else
99 /* Oops; that was actually the width and/or 0 padding flag.
100 Step back and read it again. */
101 format = begin;
104 /* Check for spec modifiers. */
107 switch (*format)
109 case L_(' '):
110 /* Output a space in place of a sign, when there is no sign. */
111 spec->info.space = 1;
112 continue;
113 case L_('+'):
114 /* Always output + or - for numbers. */
115 spec->info.showsign = 1;
116 continue;
117 case L_('-'):
118 /* Left-justify things. */
119 spec->info.left = 1;
120 continue;
121 case L_('#'):
122 /* Use the "alternate form":
123 Hex has 0x or 0X, FP always has a decimal point. */
124 spec->info.alt = 1;
125 continue;
126 case L_('0'):
127 /* Pad with 0s. */
128 spec->info.pad = '0';
129 continue;
130 case L_('\''):
131 /* Show grouping in numbers if the locale information
132 indicates any. */
133 spec->info.group = 1;
134 continue;
135 case L_('I'):
136 /* Use the internationalized form of the output. Currently
137 means to use the `outdigits' of the current locale. */
138 spec->info.i18n = 1;
139 continue;
140 default:
141 break;
143 break;
145 while (*++format);
147 if (spec->info.left)
148 spec->info.pad = ' ';
150 /* Get the field width. */
151 spec->width_arg = -1;
152 spec->info.width = 0;
153 if (*format == L_('*'))
155 /* The field width is given in an argument.
156 A negative field width indicates left justification. */
157 const UCHAR_T *begin = ++format;
159 if (ISDIGIT (*format))
161 /* The width argument might be found in a positional parameter. */
162 n = read_int (&format);
164 if (n > 0 && *format == L_('$'))
166 spec->width_arg = n - 1;
167 *max_ref_arg = MAX (*max_ref_arg, n);
168 ++format; /* Skip '$'. */
172 if (spec->width_arg < 0)
174 /* Not in a positional parameter. Consume one argument. */
175 spec->width_arg = posn++;
176 ++nargs;
177 format = begin; /* Step back and reread. */
180 else if (ISDIGIT (*format))
181 /* Constant width specification. */
182 spec->info.width = read_int (&format);
184 /* Get the precision. */
185 spec->prec_arg = -1;
186 /* -1 means none given; 0 means explicit 0. */
187 spec->info.prec = -1;
188 if (*format == L_('.'))
190 ++format;
191 if (*format == L_('*'))
193 /* The precision is given in an argument. */
194 const UCHAR_T *begin = ++format;
196 if (ISDIGIT (*format))
198 n = read_int (&format);
200 if (n > 0 && *format == L_('$'))
202 spec->prec_arg = n - 1;
203 *max_ref_arg = MAX (*max_ref_arg, n);
204 ++format;
208 if (spec->prec_arg < 0)
210 /* Not in a positional parameter. */
211 spec->prec_arg = posn++;
212 ++nargs;
213 format = begin;
216 else if (ISDIGIT (*format))
217 spec->info.prec = read_int (&format);
218 else
219 /* "%.?" is treated like "%.0?". */
220 spec->info.prec = 0;
223 /* Check for type modifiers. */
224 spec->info.is_long_double = 0;
225 spec->info.is_short = 0;
226 spec->info.is_long = 0;
227 spec->info.is_char = 0;
228 spec->info.user = 0;
230 if (__builtin_expect (__printf_modifier_table == NULL, 1)
231 || __printf_modifier_table[*format] == NULL
232 || HANDLE_REGISTERED_MODIFIER (&format, &spec->info) != 0)
233 switch (*format++)
235 case L_('h'):
236 /* ints are short ints or chars. */
237 if (*format != L_('h'))
238 spec->info.is_short = 1;
239 else
241 ++format;
242 spec->info.is_char = 1;
244 break;
245 case L_('l'):
246 /* ints are long ints. */
247 spec->info.is_long = 1;
248 if (*format != L_('l'))
249 break;
250 ++format;
251 /* FALLTHROUGH */
252 case L_('L'):
253 /* doubles are long doubles, and ints are long long ints. */
254 case L_('q'):
255 /* 4.4 uses this for long long. */
256 spec->info.is_long_double = 1;
257 break;
258 case L_('z'):
259 case L_('Z'):
260 /* ints are size_ts. */
261 assert (sizeof (size_t) <= sizeof (unsigned long long int));
262 #if LONG_MAX != LONG_LONG_MAX
263 spec->info.is_long_double = (sizeof (size_t)
264 > sizeof (unsigned long int));
265 #endif
266 spec->info.is_long = sizeof (size_t) > sizeof (unsigned int);
267 break;
268 case L_('t'):
269 assert (sizeof (ptrdiff_t) <= sizeof (long long int));
270 #if LONG_MAX != LONG_LONG_MAX
271 spec->info.is_long_double = (sizeof (ptrdiff_t) > sizeof (long int));
272 #endif
273 spec->info.is_long = sizeof (ptrdiff_t) > sizeof (int);
274 break;
275 case L_('j'):
276 assert (sizeof (uintmax_t) <= sizeof (unsigned long long int));
277 #if LONG_MAX != LONG_LONG_MAX
278 spec->info.is_long_double = (sizeof (uintmax_t)
279 > sizeof (unsigned long int));
280 #endif
281 spec->info.is_long = sizeof (uintmax_t) > sizeof (unsigned int);
282 break;
283 default:
284 /* Not a recognized modifier. Backup. */
285 --format;
286 break;
289 /* Get the format specification. */
290 spec->info.spec = (wchar_t) *format++;
291 spec->size = -1;
292 if (__builtin_expect (__printf_function_table == NULL, 1)
293 || spec->info.spec > UCHAR_MAX
294 || __printf_arginfo_table[spec->info.spec] == NULL
295 /* We don't try to get the types for all arguments if the format
296 uses more than one. The normal case is covered though. If
297 the call returns -1 we continue with the normal specifiers. */
298 || (spec->ndata_args = (*__printf_arginfo_table[spec->info.spec])
299 (&spec->info, 1, &spec->data_arg_type,
300 &spec->size)) < 0)
302 /* Find the data argument types of a built-in spec. */
303 spec->ndata_args = 1;
305 switch (spec->info.spec)
307 case L'i':
308 case L'd':
309 case L'u':
310 case L'o':
311 case L'X':
312 case L'x':
313 #if LONG_MAX != LONG_LONG_MAX
314 if (spec->info.is_long_double)
315 spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
316 else
317 #endif
318 if (spec->info.is_long)
319 spec->data_arg_type = PA_INT|PA_FLAG_LONG;
320 else if (spec->info.is_short)
321 spec->data_arg_type = PA_INT|PA_FLAG_SHORT;
322 else if (spec->info.is_char)
323 spec->data_arg_type = PA_CHAR;
324 else
325 spec->data_arg_type = PA_INT;
326 break;
327 case L'e':
328 case L'E':
329 case L'f':
330 case L'F':
331 case L'g':
332 case L'G':
333 case L'a':
334 case L'A':
335 if (spec->info.is_long_double)
336 spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
337 else
338 spec->data_arg_type = PA_DOUBLE;
339 break;
340 case L'c':
341 spec->data_arg_type = PA_CHAR;
342 break;
343 case L'C':
344 spec->data_arg_type = PA_WCHAR;
345 break;
346 case L's':
347 spec->data_arg_type = PA_STRING;
348 break;
349 case L'S':
350 spec->data_arg_type = PA_WSTRING;
351 break;
352 case L'p':
353 spec->data_arg_type = PA_POINTER;
354 break;
355 case L'n':
356 spec->data_arg_type = PA_INT|PA_FLAG_PTR;
357 break;
359 case L'm':
360 default:
361 /* An unknown spec will consume no args. */
362 spec->ndata_args = 0;
363 break;
367 if (spec->data_arg == -1 && spec->ndata_args > 0)
369 /* There are args consumed, but no positional spec. Use the
370 next sequential arg position. */
371 spec->data_arg = posn;
372 nargs += spec->ndata_args;
375 if (spec->info.spec == L'\0')
376 /* Format ended before this spec was complete. */
377 spec->end_of_fmt = spec->next_fmt = format - 1;
378 else
380 /* Find the next format spec. */
381 spec->end_of_fmt = format;
382 #ifdef COMPILE_WPRINTF
383 spec->next_fmt = __find_specwc (format);
384 #else
385 spec->next_fmt = __find_specmb (format);
386 #endif
389 return nargs;