errno is already set.
Output string accumulator.
Allocate a small buffer that will hold a directive passed to sprintf or snprintf.
Invariants: result is either == resultbuf or == NULL or malloc-allocated. If length > 0, then result != NULL.
Ensures that allocated >= needed. Aborts through a jump to out_of_memory if needed is SIZE_MAX or otherwise too big.
This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we need that the format string contains only ASCII characters if FCHAR_T and DCHAR_T are not the same type.
Execute a single directive.
"A negative field width is taken as a '-' flag
followed by a positive field width."
Decide whether to handle the precision ourselves.
Decide whether to perform the padding ourselves.
Allocate a temporary buffer of sufficient size for calling sprintf.
Overflow, would lead to out of memory.
Out of memory.
Construct the format string for calling snprintf or sprintf.
The width specification is known to consist only of standard ASCII characters.
The precision specification is known to consist only of standard ASCII characters.
Construct the arguments for calling snprintf or sprintf.
Attempt to handle failure.
SNPRINTF or sprintf failed. Save and use the errno that it has set, if any.
Make room for the result.
Need at least count elements. But allocate proportionally.
Here count <= allocated - length.
Perform padding.
Here still count <= allocated - length.
Append the sprintf() result.
Add the final NUL.
Shrink the allocated memory if possible.
Note that we can produce a big string of a length > INT_MAX. POSIX says that snprintf() fails with errno = EOVERFLOW in this case, but that's only because snprintf() returns an 'int'. This function does not have this limitation.
1784 if (d.dir != d.direct_alloc_dir) \
1786 if (a.arg != a.direct_alloc_arg) \
1797 size_t buf_neededlength;
1811 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
1813 if (buf_neededlength < 4000 /
sizeof (
TCHAR_T))
1816 buf_malloced = NULL;
1821 size_t buf_memsize =
xtimes (buf_neededlength,
sizeof (
TCHAR_T));
1823 goto out_of_memory_1;
1826 goto out_of_memory_1;
1830 if (resultbuf != NULL)
1833 allocated = *lengthp;
1847 #define ENSURE_ALLOCATION(needed) \
1848 if ((needed) > allocated) \
1850 size_t memory_size; \
1853 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1854 if ((needed) > allocated) \
1855 allocated = (needed); \
1856 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1857 if (size_overflow_p (memory_size)) \
1858 goto out_of_memory; \
1859 if (result == resultbuf || result == NULL) \
1860 memory = (DCHAR_T *) malloc (memory_size); \
1862 memory = (DCHAR_T *) realloc (result, memory_size); \
1863 if (memory == NULL) \
1864 goto out_of_memory; \
1865 if (result == resultbuf && length > 0) \
1866 DCHAR_CPY (memory, result, length); \
1870 for (
cp = format, i = 0, dp = &d.dir[0]; ;
cp = dp->dir_end, i++, dp++)
1872 if (
cp != dp->dir_start)
1874 size_t n = dp->dir_start -
cp;
1875 size_t augmented_length =
xsum (length, n);
1884 length = augmented_length;
1889 result[length++] = (
unsigned char) *
cp++;
1897 if (dp->conversion ==
'%')
1899 size_t augmented_length;
1903 augmented_length =
xsum (length, 1);
1905 result[length] =
'%';
1906 length = augmented_length;
1913 if (dp->conversion ==
'n')
1915 switch (a.
arg[dp->arg_index].
type)
1929 #if HAVE_LONG_LONG_INT
1930 case TYPE_COUNT_LONGLONGINT_POINTER:
1931 *a.
arg[dp->arg_index].
a.a_count_longlongint_pointer = length;
1940 else if (dp->conversion ==
'U')
1943 int flags = dp->flags;
1951 if (dp->width_start != dp->width_end)
1953 if (dp->width_arg_index !=
ARG_NONE)
1959 arg = a.
arg[dp->width_arg_index].
a.
a_int;
1965 width = (
unsigned int) (-arg);
1972 const FCHAR_T *digitp = dp->width_start;
1975 width =
xsum (
xtimes (width, 10), *digitp++ -
'0');
1976 while (digitp != dp->width_end);
1983 if (dp->precision_start != dp->precision_end)
1985 if (dp->precision_arg_index !=
ARG_NONE)
1991 arg = a.
arg[dp->precision_arg_index].
a.
a_int;
2002 const FCHAR_T *digitp = dp->precision_start + 1;
2005 while (digitp != dp->precision_end)
2013 case TYPE_U8_STRING:
2015 const uint8_t *arg = a.
arg[dp->arg_index].
a.a_u8_string;
2026 int count = u8_strmblen (arg_end);
2031 if (!(result == resultbuf || result == NULL))
2033 if (buf_malloced != NULL)
2034 free (buf_malloced);
2051 int count = u8_strmblen (arg_end);
2056 if (!(result == resultbuf || result == NULL))
2058 if (buf_malloced != NULL)
2059 free (buf_malloced);
2071 arg_end = arg + u8_strlen (arg);
2076 if (has_width && width > characters
2079 size_t n = width - characters;
2085 # if DCHAR_IS_UINT8_T
2087 size_t n = arg_end - arg;
2094 DCHAR_T *converted = result + length;
2095 size_t converted_len = allocated - length;
2099 u8_conv_to_encoding (locale_charset (),
2100 iconveh_question_mark,
2101 arg, arg_end - arg, NULL,
2102 converted, &converted_len);
2106 U8_TO_DCHAR (arg, arg_end - arg,
2107 converted, &converted_len);
2109 if (converted == NULL)
2111 int saved_errno = errno;
2112 if (!(result == resultbuf || result == NULL))
2114 if (buf_malloced != NULL)
2115 free (buf_malloced);
2117 errno = saved_errno;
2120 if (converted != result + length)
2123 DCHAR_CPY (result + length, converted, converted_len);
2126 length += converted_len;
2130 if (has_width && width > characters
2133 size_t n = width - characters;
2141 case TYPE_U16_STRING:
2143 const uint16_t *arg = a.
arg[dp->arg_index].
a.a_u16_string;
2154 int count = u16_strmblen (arg_end);
2159 if (!(result == resultbuf || result == NULL))
2161 if (buf_malloced != NULL)
2162 free (buf_malloced);
2179 int count = u16_strmblen (arg_end);
2184 if (!(result == resultbuf || result == NULL))
2186 if (buf_malloced != NULL)
2187 free (buf_malloced);
2199 arg_end = arg + u16_strlen (arg);
2204 if (has_width && width > characters
2207 size_t n = width - characters;
2213 # if DCHAR_IS_UINT16_T
2215 size_t n = arg_end - arg;
2222 DCHAR_T *converted = result + length;
2223 size_t converted_len = allocated - length;
2227 u16_conv_to_encoding (locale_charset (),
2228 iconveh_question_mark,
2229 arg, arg_end - arg, NULL,
2230 converted, &converted_len);
2234 U16_TO_DCHAR (arg, arg_end - arg,
2235 converted, &converted_len);
2237 if (converted == NULL)
2239 int saved_errno = errno;
2240 if (!(result == resultbuf || result == NULL))
2242 if (buf_malloced != NULL)
2243 free (buf_malloced);
2245 errno = saved_errno;
2248 if (converted != result + length)
2251 DCHAR_CPY (result + length, converted, converted_len);
2254 length += converted_len;
2258 if (has_width && width > characters
2261 size_t n = width - characters;
2269 case TYPE_U32_STRING:
2271 const uint32_t *arg = a.
arg[dp->arg_index].
a.a_u32_string;
2282 int count = u32_strmblen (arg_end);
2287 if (!(result == resultbuf || result == NULL))
2289 if (buf_malloced != NULL)
2290 free (buf_malloced);
2307 int count = u32_strmblen (arg_end);
2312 if (!(result == resultbuf || result == NULL))
2314 if (buf_malloced != NULL)
2315 free (buf_malloced);
2327 arg_end = arg + u32_strlen (arg);
2332 if (has_width && width > characters
2335 size_t n = width - characters;
2341 # if DCHAR_IS_UINT32_T
2343 size_t n = arg_end - arg;
2350 DCHAR_T *converted = result + length;
2351 size_t converted_len = allocated - length;
2355 u32_conv_to_encoding (locale_charset (),
2356 iconveh_question_mark,
2357 arg, arg_end - arg, NULL,
2358 converted, &converted_len);
2362 U32_TO_DCHAR (arg, arg_end - arg,
2363 converted, &converted_len);
2365 if (converted == NULL)
2367 int saved_errno = errno;
2368 if (!(result == resultbuf || result == NULL))
2370 if (buf_malloced != NULL)
2371 free (buf_malloced);
2373 errno = saved_errno;
2376 if (converted != result + length)
2379 DCHAR_CPY (result + length, converted, converted_len);
2382 length += converted_len;
2386 if (has_width && width > characters
2389 size_t n = width - characters;
2402 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2403 else if (dp->conversion ==
's'
2404 #
if WIDE_CHAR_VERSION
2405 && a.
arg[dp->arg_index].
type != TYPE_WIDE_STRING
2407 && a.
arg[dp->arg_index].
type == TYPE_WIDE_STRING
2421 int flags = dp->flags;
2429 if (dp->width_start != dp->width_end)
2431 if (dp->width_arg_index !=
ARG_NONE)
2437 arg = a.
arg[dp->width_arg_index].
a.
a_int;
2443 width = (
unsigned int) (-arg);
2450 const FCHAR_T *digitp = dp->width_start;
2453 width =
xsum (
xtimes (width, 10), *digitp++ -
'0');
2454 while (digitp != dp->width_end);
2461 if (dp->precision_start != dp->precision_end)
2463 if (dp->precision_arg_index !=
ARG_NONE)
2469 arg = a.
arg[dp->precision_arg_index].
a.
a_int;
2480 const FCHAR_T *digitp = dp->precision_start + 1;
2483 while (digitp != dp->precision_end)
2489 # if WIDE_CHAR_VERSION
2493 const char *arg_end;
2510 count = mbrlen (arg_end, MB_CUR_MAX, &state);
2512 count = mblen (arg_end, MB_CUR_MAX);
2520 if (!(result == resultbuf || result == NULL))
2522 if (buf_malloced != NULL)
2523 free (buf_malloced);
2546 count = mbrlen (arg_end, MB_CUR_MAX, &state);
2548 count = mblen (arg_end, MB_CUR_MAX);
2556 if (!(result == resultbuf || result == NULL))
2558 if (buf_malloced != NULL)
2559 free (buf_malloced);
2571 arg_end = arg + strlen (arg);
2576 if (has_width && width > characters
2579 size_t n = width - characters;
2585 if (has_precision || has_width)
2594 for (remaining = characters; remaining > 0; remaining--)
2599 count = mbrtowc (&
wc, arg, arg_end - arg, &state);
2601 count = mbtowc (&
wc, arg, arg_end - arg);
2607 result[length++] =
wc;
2610 if (!(arg == arg_end))
2619 while (arg < arg_end)
2624 count = mbrtowc (&
wc, arg, arg_end - arg, &state);
2626 count = mbtowc (&
wc, arg, arg_end - arg);
2633 result[length++] =
wc;
2638 if (has_width && width > characters
2641 size_t n = width - characters;
2650 const wchar_t *arg = a.
arg[dp->arg_index].
a.a_wide_string;
2651 const wchar_t *arg_end;
2653 # if !DCHAR_IS_TCHAR
2666 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2680 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2681 count = wcrtomb (cbuf, *arg_end, &state);
2683 count = wctomb (cbuf, *arg_end);
2688 if (!(result == resultbuf || result == NULL))
2690 if (buf_malloced != NULL)
2691 free (buf_malloced);
2699 characters +=
count;
2711 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2725 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2726 count = wcrtomb (cbuf, *arg_end, &state);
2728 count = wctomb (cbuf, *arg_end);
2733 if (!(result == resultbuf || result == NULL))
2735 if (buf_malloced != NULL)
2736 free (buf_malloced);
2742 characters +=
count;
2749 arg_end = arg + local_wcslen (arg);
2755 # if !DCHAR_IS_TCHAR
2763 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2767 for (remaining = characters; remaining > 0; )
2774 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2775 count = wcrtomb (cbuf, *arg, &state);
2777 count = wctomb (cbuf, *arg);
2782 memcpy (tmpptr, cbuf,
count);
2787 if (!(arg == arg_end))
2793 DCHAR_CONV_FROM_ENCODING (locale_charset (),
2794 iconveh_question_mark,
2800 int saved_errno = errno;
2802 if (!(result == resultbuf || result == NULL))
2804 if (buf_malloced != NULL)
2805 free (buf_malloced);
2807 errno = saved_errno;
2815 # if ENABLE_UNISTDIO
2819 w = DCHAR_MBSNLEN (result + length, characters);
2830 if (has_width && width > w
2833 size_t n = width - w;
2840 if (has_precision || has_width)
2844 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2849 for (remaining = characters; remaining > 0; )
2856 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2857 count = wcrtomb (cbuf, *arg, &state);
2859 count = wctomb (cbuf, *arg);
2864 memcpy (result + length, cbuf,
count);
2869 if (!(arg == arg_end))
2874 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2878 while (arg < arg_end)
2885 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2886 count = wcrtomb (cbuf, *arg, &state);
2888 count = wctomb (cbuf, *arg);
2893 if (!(result == resultbuf || result == NULL))
2895 if (buf_malloced != NULL)
2896 free (buf_malloced);
2902 memcpy (result + length, cbuf,
count);
2909 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
2911 length += tmpdst_len;
2914 if (has_width && width > w
2917 size_t n = width - w;
2926 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2927 else if ((dp->conversion ==
'a' || dp->conversion ==
'A')
2928 #
if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2930 #
if NEED_PRINTF_DOUBLE
2933 #
if NEED_PRINTF_LONG_DOUBLE
2941 int flags = dp->flags;
2954 if (dp->width_start != dp->width_end)
2956 if (dp->width_arg_index !=
ARG_NONE)
2962 arg = a.
arg[dp->width_arg_index].
a.
a_int;
2968 width = (
unsigned int) (-arg);
2975 const FCHAR_T *digitp = dp->width_start;
2978 width =
xsum (
xtimes (width, 10), *digitp++ -
'0');
2979 while (digitp != dp->width_end);
2986 if (dp->precision_start != dp->precision_end)
2988 if (dp->precision_arg_index !=
ARG_NONE)
2994 arg = a.
arg[dp->precision_arg_index].
a.
a_int;
3005 const FCHAR_T *digitp = dp->precision_start + 1;
3008 while (digitp != dp->precision_end)
3017 (
unsigned int) ((LDBL_DIG + 1)
3023 (
unsigned int) ((DBL_DIG + 1)
3030 tmp_length =
xsum (tmp_length, 12);
3032 if (tmp_length < width)
3035 tmp_length =
xsum (tmp_length, 1);
3037 if (tmp_length <=
sizeof (tmpbuf) /
sizeof (
DCHAR_T))
3056 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
3061 if (dp->conversion ==
'A')
3063 *p++ =
'N'; *p++ =
'A'; *p++ =
'N';
3067 *p++ =
'n'; *p++ =
'a'; *p++ =
'n';
3073 DECL_LONG_DOUBLE_ROUNDING
3075 BEGIN_LONG_DOUBLE_ROUNDING ();
3090 if (arg > 0.0L && arg + arg == arg)
3092 if (dp->conversion ==
'A')
3094 *p++ =
'I'; *p++ =
'N'; *p++ =
'F';
3098 *p++ =
'i'; *p++ =
'n'; *p++ =
'f';
3104 long double mantissa;
3107 mantissa = printf_frexpl (arg, &exponent);
3115 &&
precision < (
unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
3118 long double tail = mantissa;
3123 int digit = (
int) tail;
3127 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
3142 *p++ = dp->conversion -
'A' +
'X';
3147 digit = (
int) mantissa;
3153 *p++ = decimal_point_char ();
3156 while (mantissa > 0.0L)
3159 digit = (
int) mantissa;
3164 : dp->conversion - 10);
3175 *p++ = dp->conversion -
'A' +
'P';
3176 # if WIDE_CHAR_VERSION
3178 static const wchar_t decimal_format[] =
3179 {
'%',
'+',
'd',
'\0' };
3180 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3187 sprintf ((
char *) p,
"%+d", exponent);
3195 sprintf (expbuf,
"%+d", exponent);
3196 for (ep = expbuf; (*p = *ep) !=
'\0'; ep++)
3202 END_LONG_DOUBLE_ROUNDING ();
3210 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
3215 if (dp->conversion ==
'A')
3217 *p++ =
'N'; *p++ =
'A'; *p++ =
'N';
3221 *p++ =
'n'; *p++ =
'a'; *p++ =
'n';
3241 if (arg > 0.0 && arg + arg == arg)
3243 if (dp->conversion ==
'A')
3245 *p++ =
'I'; *p++ =
'N'; *p++ =
'F';
3249 *p++ =
'i'; *p++ =
'n'; *p++ =
'f';
3258 mantissa = printf_frexp (arg, &exponent);
3266 &&
precision < (
unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
3269 double tail = mantissa;
3274 int digit = (
int) tail;
3278 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
3293 *p++ = dp->conversion -
'A' +
'X';
3298 digit = (
int) mantissa;
3304 *p++ = decimal_point_char ();
3307 while (mantissa > 0.0)
3310 digit = (
int) mantissa;
3315 : dp->conversion - 10);
3326 *p++ = dp->conversion -
'A' +
'P';
3327 # if WIDE_CHAR_VERSION
3329 static const wchar_t decimal_format[] =
3330 {
'%',
'+',
'd',
'\0' };
3331 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3338 sprintf ((
char *) p,
"%+d", exponent);
3346 sprintf (expbuf,
"%+d", exponent);
3347 for (ep = expbuf; (*p = *ep) !=
'\0'; ep++)
3359 if (has_width && p - tmp < width)
3361 size_t pad = width - (p - tmp);
3367 for (; pad > 0; pad--)
3370 else if ((flags &
FLAG_ZERO) && pad_ptr != NULL)
3377 for (; pad > 0; pad--)
3387 for (; pad > 0; pad--)
3395 size_t count = p - tmp;
3397 if (
count >= tmp_length)
3403 if (
count >= allocated - length)
3411 memcpy (result + length, tmp,
count *
sizeof (
DCHAR_T));
3418 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3419 else if ((dp->conversion ==
'f' || dp->conversion ==
'F'
3420 || dp->conversion ==
'e' || dp->conversion ==
'E'
3421 || dp->conversion ==
'g' || dp->conversion ==
'G'
3422 || dp->conversion ==
'a' || dp->conversion ==
'A')
3424 #
if NEED_PRINTF_DOUBLE
3426 # elif NEED_PRINTF_INFINITE_DOUBLE
3431 && is_infinite_or_zero (a.
arg[dp->arg_index].
a.
a_double))
3433 #
if NEED_PRINTF_LONG_DOUBLE
3435 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3445 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3448 int flags = dp->flags;
3461 if (dp->width_start != dp->width_end)
3463 if (dp->width_arg_index !=
ARG_NONE)
3469 arg = a.
arg[dp->width_arg_index].
a.
a_int;
3475 width = (
unsigned int) (-arg);
3482 const FCHAR_T *digitp = dp->width_start;
3485 width =
xsum (
xtimes (width, 10), *digitp++ -
'0');
3486 while (digitp != dp->width_end);
3493 if (dp->precision_start != dp->precision_end)
3495 if (dp->precision_arg_index !=
ARG_NONE)
3501 arg = a.
arg[dp->precision_arg_index].
a.
a_int;
3512 const FCHAR_T *digitp = dp->precision_start + 1;
3515 while (digitp != dp->precision_end)
3526 if (!(dp->conversion ==
'a' || dp->conversion ==
'A'))
3530 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3532 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3534 # elif NEED_PRINTF_LONG_DOUBLE
3535 tmp_length = LDBL_DIG + 1;
3536 # elif NEED_PRINTF_DOUBLE
3537 tmp_length = DBL_DIG + 1;
3543 # if NEED_PRINTF_LONG_DOUBLE
3544 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3547 if (dp->conversion ==
'f' || dp->conversion ==
'F')
3550 if (!(isnanl (arg) || arg + arg == arg))
3553 int exponent = floorlog10l (arg < 0 ? -arg : arg);
3554 if (exponent >= 0 && tmp_length < exponent +
precision)
3559 # if NEED_PRINTF_DOUBLE
3560 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3563 if (dp->conversion ==
'f' || dp->conversion ==
'F')
3566 if (!(isnand (arg) || arg + arg == arg))
3569 int exponent = floorlog10 (arg < 0 ? -arg : arg);
3570 if (exponent >= 0 && tmp_length < exponent +
precision)
3576 tmp_length =
xsum (tmp_length, 12);
3578 if (tmp_length < width)
3581 tmp_length =
xsum (tmp_length, 1);
3583 if (tmp_length <=
sizeof (tmpbuf) /
sizeof (
DCHAR_T))
3601 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3602 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3610 if (dp->conversion >=
'A' && dp->conversion <=
'Z')
3612 *p++ =
'N'; *p++ =
'A'; *p++ =
'N';
3616 *p++ =
'n'; *p++ =
'a'; *p++ =
'n';
3622 DECL_LONG_DOUBLE_ROUNDING
3624 BEGIN_LONG_DOUBLE_ROUNDING ();
3639 if (arg > 0.0L && arg + arg == arg)
3641 if (dp->conversion >=
'A' && dp->conversion <=
'Z')
3643 *p++ =
'I'; *p++ =
'N'; *p++ =
'F';
3647 *p++ =
'i'; *p++ =
'n'; *p++ =
'f';
3652 # if NEED_PRINTF_LONG_DOUBLE
3655 if (dp->conversion ==
'f' || dp->conversion ==
'F')
3661 scale10_round_decimal_long_double (arg,
precision);
3664 END_LONG_DOUBLE_ROUNDING ();
3667 ndigits = strlen (digits);
3673 *p++ = digits[ndigits];
3681 *p++ = decimal_point_char ();
3687 *p++ = digits[ndigits];
3693 else if (dp->conversion ==
'e' || dp->conversion ==
'E')
3703 *p++ = decimal_point_char ();
3715 exponent = floorlog10l (arg);
3720 scale10_round_decimal_long_double (arg,
3724 END_LONG_DOUBLE_ROUNDING ();
3727 ndigits = strlen (digits);
3755 scale10_round_decimal_long_double (arg,
3757 if (digits2 == NULL)
3760 END_LONG_DOUBLE_ROUNDING ();
3774 *p++ = digits[--ndigits];
3777 *p++ = decimal_point_char ();
3781 *p++ = digits[ndigits];
3788 *p++ = dp->conversion;
3789 # if WIDE_CHAR_VERSION
3791 static const wchar_t decimal_format[] =
3792 {
'%',
'+',
'.',
'2',
'd',
'\0' };
3793 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3800 sprintf ((
char *) p,
"%+.2d", exponent);
3808 sprintf (expbuf,
"%+.2d", exponent);
3809 for (ep = expbuf; (*p = *ep) !=
'\0'; ep++)
3814 else if (dp->conversion ==
'g' || dp->conversion ==
'G')
3832 if ((flags &
FLAG_ALT) || ndigits > nzeroes)
3834 *p++ = decimal_point_char ();
3835 while (ndigits > nzeroes)
3851 exponent = floorlog10l (arg);
3856 scale10_round_decimal_long_double (arg,
3860 END_LONG_DOUBLE_ROUNDING ();
3863 ndigits = strlen (digits);
3885 if (is_borderline (digits,
precision - 1))
3891 scale10_round_decimal_long_double (arg,
3893 if (digits2 == NULL)
3896 END_LONG_DOUBLE_ROUNDING ();
3914 while (nzeroes < ndigits
3915 && digits[nzeroes] ==
'0')
3928 size_t count = exponent + 1;
3931 *p++ = digits[--ndigits];
3932 if ((flags &
FLAG_ALT) || ndigits > nzeroes)
3934 *p++ = decimal_point_char ();
3935 while (ndigits > nzeroes)
3938 *p++ = digits[ndigits];
3944 size_t count = -exponent - 1;
3946 *p++ = decimal_point_char ();
3949 while (ndigits > nzeroes)
3952 *p++ = digits[ndigits];
3959 *p++ = digits[--ndigits];
3960 if ((flags &
FLAG_ALT) || ndigits > nzeroes)
3962 *p++ = decimal_point_char ();
3963 while (ndigits > nzeroes)
3966 *p++ = digits[ndigits];
3969 *p++ = dp->conversion -
'G' +
'E';
3970 # if WIDE_CHAR_VERSION
3972 static const wchar_t decimal_format[] =
3973 {
'%',
'+',
'.',
'2',
'd',
'\0' };
3974 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3981 sprintf ((
char *) p,
"%+.2d", exponent);
3989 sprintf (expbuf,
"%+.2d", exponent);
3990 for (ep = expbuf; (*p = *ep) !=
'\0'; ep++)
4008 if (dp->conversion ==
'f' || dp->conversion ==
'F')
4013 *p++ = decimal_point_char ();
4018 else if (dp->conversion ==
'e' || dp->conversion ==
'E')
4023 *p++ = decimal_point_char ();
4027 *p++ = dp->conversion;
4032 else if (dp->conversion ==
'g' || dp->conversion ==
'G')
4039 *p++ = decimal_point_char ();
4040 for (; ndigits > 0; --ndigits)
4044 else if (dp->conversion ==
'a' || dp->conversion ==
'A')
4047 *p++ = dp->conversion -
'A' +
'X';
4052 *p++ = decimal_point_char ();
4056 *p++ = dp->conversion -
'A' +
'P';
4065 END_LONG_DOUBLE_ROUNDING ();
4068 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4072 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4078 if (dp->conversion >=
'A' && dp->conversion <=
'Z')
4080 *p++ =
'N'; *p++ =
'A'; *p++ =
'N';
4084 *p++ =
'n'; *p++ =
'a'; *p++ =
'n';
4104 if (arg > 0.0 && arg + arg == arg)
4106 if (dp->conversion >=
'A' && dp->conversion <=
'Z')
4108 *p++ =
'I'; *p++ =
'N'; *p++ =
'F';
4112 *p++ =
'i'; *p++ =
'n'; *p++ =
'f';
4117 # if NEED_PRINTF_DOUBLE
4120 if (dp->conversion ==
'f' || dp->conversion ==
'F')
4126 scale10_round_decimal_double (arg,
precision);
4129 ndigits = strlen (digits);
4135 *p++ = digits[ndigits];
4143 *p++ = decimal_point_char ();
4149 *p++ = digits[ndigits];
4155 else if (dp->conversion ==
'e' || dp->conversion ==
'E')
4165 *p++ = decimal_point_char ();
4177 exponent = floorlog10 (arg);
4182 scale10_round_decimal_double (arg,
4186 ndigits = strlen (digits);
4214 scale10_round_decimal_double (arg,
4216 if (digits2 == NULL)
4232 *p++ = digits[--ndigits];
4235 *p++ = decimal_point_char ();
4239 *p++ = digits[ndigits];
4246 *p++ = dp->conversion;
4247 # if WIDE_CHAR_VERSION
4249 static const wchar_t decimal_format[] =
4252 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4253 {
'%',
'+',
'.',
'3',
'd',
'\0' };
4255 {
'%',
'+',
'.',
'2',
'd',
'\0' };
4257 SNPRINTF (p, 6 + 1, decimal_format, exponent);
4263 static const char decimal_format[] =
4266 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4273 sprintf ((
char *) p, decimal_format, exponent);
4281 sprintf (expbuf, decimal_format, exponent);
4282 for (ep = expbuf; (*p = *ep) !=
'\0'; ep++)
4288 else if (dp->conversion ==
'g' || dp->conversion ==
'G')
4306 if ((flags &
FLAG_ALT) || ndigits > nzeroes)
4308 *p++ = decimal_point_char ();
4309 while (ndigits > nzeroes)
4325 exponent = floorlog10 (arg);
4330 scale10_round_decimal_double (arg,
4334 ndigits = strlen (digits);
4356 if (is_borderline (digits,
precision - 1))
4362 scale10_round_decimal_double (arg,
4364 if (digits2 == NULL)
4384 while (nzeroes < ndigits
4385 && digits[nzeroes] ==
'0')
4398 size_t count = exponent + 1;
4401 *p++ = digits[--ndigits];
4402 if ((flags &
FLAG_ALT) || ndigits > nzeroes)
4404 *p++ = decimal_point_char ();
4405 while (ndigits > nzeroes)
4408 *p++ = digits[ndigits];
4414 size_t count = -exponent - 1;
4416 *p++ = decimal_point_char ();
4419 while (ndigits > nzeroes)
4422 *p++ = digits[ndigits];
4429 *p++ = digits[--ndigits];
4430 if ((flags &
FLAG_ALT) || ndigits > nzeroes)
4432 *p++ = decimal_point_char ();
4433 while (ndigits > nzeroes)
4436 *p++ = digits[ndigits];
4439 *p++ = dp->conversion -
'G' +
'E';
4440 # if WIDE_CHAR_VERSION
4442 static const wchar_t decimal_format[] =
4445 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4446 {
'%',
'+',
'.',
'3',
'd',
'\0' };
4448 {
'%',
'+',
'.',
'2',
'd',
'\0' };
4450 SNPRINTF (p, 6 + 1, decimal_format, exponent);
4456 static const char decimal_format[] =
4459 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4466 sprintf ((
char *) p, decimal_format, exponent);
4474 sprintf (expbuf, decimal_format, exponent);
4475 for (ep = expbuf; (*p = *ep) !=
'\0'; ep++)
4494 if (dp->conversion ==
'f' || dp->conversion ==
'F')
4499 *p++ = decimal_point_char ();
4504 else if (dp->conversion ==
'e' || dp->conversion ==
'E')
4509 *p++ = decimal_point_char ();
4513 *p++ = dp->conversion;
4517 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4523 else if (dp->conversion ==
'g' || dp->conversion ==
'G')
4530 *p++ = decimal_point_char ();
4531 for (; ndigits > 0; --ndigits)
4545 if (has_width && p - tmp < width)
4547 size_t pad = width - (p - tmp);
4553 for (; pad > 0; pad--)
4556 else if ((flags &
FLAG_ZERO) && pad_ptr != NULL)
4563 for (; pad > 0; pad--)
4573 for (; pad > 0; pad--)
4581 size_t count = p - tmp;
4583 if (
count >= tmp_length)
4589 if (
count >= allocated - length)
4597 memcpy (result + length, tmp,
count *
sizeof (
DCHAR_T));
4607 int flags = dp->flags;
4608 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4612 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4616 #if NEED_PRINTF_UNBOUNDED_PRECISION
4619 # define prec_ourselves 0
4621 #if NEED_PRINTF_FLAG_LEFTADJUST
4622 # define pad_ourselves 1
4623 #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4626 # define pad_ourselves 0
4629 unsigned int prefix_count;
4638 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4641 if (dp->width_start != dp->width_end)
4643 if (dp->width_arg_index !=
ARG_NONE)
4649 arg = a.
arg[dp->width_arg_index].
a.
a_int;
4655 width = (
unsigned int) (-arg);
4662 const FCHAR_T *digitp = dp->width_start;
4665 width =
xsum (
xtimes (width, 10), *digitp++ -
'0');
4666 while (digitp != dp->width_end);
4672 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4675 if (dp->precision_start != dp->precision_end)
4677 if (dp->precision_arg_index !=
ARG_NONE)
4683 arg = a.
arg[dp->precision_arg_index].
a.
a_int;
4694 const FCHAR_T *digitp = dp->precision_start + 1;
4697 while (digitp != dp->precision_end)
4705 #if NEED_PRINTF_UNBOUNDED_PRECISION
4706 switch (dp->conversion)
4708 case 'd':
case 'i':
case 'u':
4710 case 'x':
case 'X':
case 'p':
4720 #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4721 switch (dp->conversion)
4723 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4730 # if NEED_PRINTF_FLAG_ZERO
4731 case 'f':
case 'F':
case 'e':
case 'E':
case 'g':
case 'G':
4750 if (tmp_length <=
sizeof (tmpbuf) /
sizeof (
TCHAR_T))
4770 #if NEED_PRINTF_FLAG_GROUPING
4786 #if __GLIBC__ >= 2 && !defined __UCLIBC__
4787 if (flags & FLAG_LOCALIZED)
4794 if (dp->width_start != dp->width_end)
4796 size_t n = dp->width_end - dp->width_start;
4801 memcpy (fbp, dp->width_start, n * sizeof (
TCHAR_T));
4806 const FCHAR_T *mp = dp->width_start;
4808 *fbp++ = (
unsigned char) *mp++;
4815 if (dp->precision_start != dp->precision_end)
4817 size_t n = dp->precision_end - dp->precision_start;
4822 memcpy (fbp, dp->precision_start, n * sizeof (
TCHAR_T));
4827 const FCHAR_T *mp = dp->precision_start;
4829 *fbp++ = (
unsigned char) *mp++;
4837 #if HAVE_LONG_LONG_INT
4838 case TYPE_LONGLONGINT:
4839 case TYPE_ULONGLONGINT:
4840 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4853 case TYPE_WIDE_CHAR:
4856 case TYPE_WIDE_STRING:
4866 #if NEED_PRINTF_DIRECTIVE_F
4867 if (dp->conversion ==
'F')
4871 *fbp = dp->conversion;
4873 # if !(((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
4929 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4933 (2 + TCHARS_PER_DCHAR - 1)
4934 / TCHARS_PER_DCHAR));
4937 *(
TCHAR_T *) (result + length) =
'\0';
4948 size_t maxlen = allocated - length;
4951 if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
4952 maxlen = INT_MAX / TCHARS_PER_DCHAR;
4953 maxlen = maxlen * TCHARS_PER_DCHAR;
4954 # define SNPRINTF_BUF(arg) \
4955 switch (prefix_count) \
4958 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4963 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4965 prefixes[0], arg, &count); \
4968 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4970 prefixes[0], prefixes[1], arg, \
4977 # define SNPRINTF_BUF(arg) \
4978 switch (prefix_count) \
4981 count = sprintf (tmp, buf, arg); \
4984 count = sprintf (tmp, buf, prefixes[0], arg); \
4987 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
5006 unsigned int arg = a.
arg[dp->arg_index].
a.
a_uchar;
5030 unsigned int arg = a.
arg[dp->arg_index].
a.
a_uint;
5046 #if HAVE_LONG_LONG_INT
5047 case TYPE_LONGLONGINT:
5049 long long int arg = a.
arg[dp->arg_index].
a.a_longlongint;
5053 case TYPE_ULONGLONGINT:
5055 unsigned long long int arg = a.
arg[dp->arg_index].
a.a_ulonglongint;
5079 case TYPE_WIDE_CHAR:
5081 wint_t arg = a.
arg[dp->arg_index].
a.a_wide_char;
5093 case TYPE_WIDE_STRING:
5095 const wchar_t *arg = a.
arg[dp->arg_index].
a.a_wide_string;
5123 if (retcount >
count)
5142 # if !HAVE_SNPRINTF_RETVAL_C99
5155 dp->conversion,
type, flags,
5156 has_width ? width : 0,
5160 if (maxlen < tmp_length)
5164 size_t bigger_need =
5167 TCHARS_PER_DCHAR - 1)
5168 / TCHARS_PER_DCHAR);
5173 size_t bigger_need2 =
5175 if (bigger_need < bigger_need2)
5176 bigger_need = bigger_need2;
5193 int saved_errno = errno;
5195 if (!(result == resultbuf || result == NULL))
5197 if (buf_malloced != NULL)
5198 free (buf_malloced);
5203 : (dp->conversion ==
'c' || dp->conversion ==
's'
5215 if ((
unsigned int)
count + 1 >= maxlen)
5220 if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
5235 ((
unsigned int)
count + 2
5236 + TCHARS_PER_DCHAR - 1)
5237 / TCHARS_PER_DCHAR),
5246 #if NEED_PRINTF_UNBOUNDED_PRECISION
5252 (
TCHAR_T *) (result + length);
5256 size_t prefix_count;
5262 && (*prec_ptr ==
'-' || *prec_ptr ==
'+'
5263 || *prec_ptr ==
' '))
5268 && prec_ptr[0] ==
'0'
5269 && (prec_ptr[1] ==
'x' || prec_ptr[1] ==
'X'))
5272 move =
count - prefix_count;
5282 (
count + insert + TCHARS_PER_DCHAR - 1)
5283 / TCHARS_PER_DCHAR);
5284 length += (
count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5286 length -= (
count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5287 prec_ptr = (
TCHAR_T *) (result + length);
5290 prec_end = prec_ptr +
count;
5291 prec_ptr += prefix_count;
5293 while (prec_end > prec_ptr)
5296 prec_end[insert] = prec_end[0];
5302 while (prec_end > prec_ptr);
5310 if (
count >= tmp_length)
5318 if (dp->conversion ==
'c' || dp->conversion ==
's')
5329 tmpsrc = (
TCHAR_T *) (result + length);
5334 DCHAR_CONV_FROM_ENCODING (locale_charset (),
5335 iconveh_question_mark,
5341 int saved_errno = errno;
5342 if (!(result == resultbuf || result == NULL))
5344 if (buf_malloced != NULL)
5345 free (buf_malloced);
5347 errno = saved_errno;
5351 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
5371 if (result == resultbuf)
5373 tmpsrc = (
TCHAR_T *) (result + length);
5383 tmpsrc = (
TCHAR_T *) (result + length);
5389 tmpdst = result + length;
5393 for (n =
count; n > 0; n--)
5394 *--tmpdst = (
unsigned char) *--tmpsrc;
5399 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
5401 if (
count > allocated - length)
5415 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5419 # if ENABLE_UNISTDIO
5423 w = DCHAR_MBSNLEN (result + length,
count);
5431 size_t pad = width - w;
5434 if (
xsum (
count, pad) > allocated - length)
5453 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
5454 DCHAR_T *
const rp = result + length;
5461 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5462 if (dp->conversion ==
'c'
5463 || dp->conversion ==
's')
5469 pad_ptr = (*rp ==
'-' ? rp + 1 : rp);
5471 if ((*pad_ptr >=
'A' && *pad_ptr <=
'Z')
5472 || (*pad_ptr >=
'a' && *pad_ptr <=
'z'))
5484 for (; pad > 0; pad--)
5487 else if ((flags &
FLAG_ZERO) && pad_ptr != NULL)
5494 for (; pad > 0; pad--)
5504 for (; pad > 0; pad--)
5514 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
5518 memcpy (result + length, tmp,
count *
sizeof (
DCHAR_T));
5525 #if NEED_PRINTF_DIRECTIVE_F
5526 if (dp->conversion ==
'F')
5529 DCHAR_T *rp = result + length;
5531 for (rc =
count; rc > 0; rc--, rp++)
5532 if (*rp >=
'a' && *rp <=
'z')
5533 *rp = *rp -
'a' +
'A';
5541 #undef pad_ourselves
5542 #undef prec_ourselves
5549 result[length] =
'\0';
5551 if (result != resultbuf && length + 1 < allocated)
5556 memory = (
DCHAR_T *) realloc (result, (length + 1) *
sizeof (
DCHAR_T));
5561 if (buf_malloced != NULL)
5562 free (buf_malloced);
5573 if (!(result == resultbuf || result == NULL))
5575 if (buf_malloced != NULL)
5576 free (buf_malloced);
5583 if (!(result == resultbuf || result == NULL))
5585 if (buf_malloced != NULL)
5586 free (buf_malloced);
void * alloca(size_t)
Memory allocation on the stack.
static int wc
FI: Provisional management of warnings.
void * memset(void *str, int c, size_t len)
memset.c – set an area of memory to a given value Copyright (C) 1991, 2003, 2009-2011 Free Software F...
@ TYPE_COUNT_SCHAR_POINTER
@ TYPE_COUNT_SHORT_POINTER
@ TYPE_COUNT_LONGINT_POINTER
#define PRINTF_FETCHARGS
Decomposed printf argument list.
#define FLAG_GROUP
Parse printf format string.
#define ARG_NONE
arg_index value indicating that no argument is consumed.
Pvecteur cp
pointeur sur l'egalite ou l'inegalite courante
signed char * a_count_schar_pointer
long int * a_count_longint_pointer
unsigned long int a_ulongint
short * a_count_short_pointer
int * a_count_int_pointer
#define ENSURE_ALLOCATION(needed)
#define SNPRINTF_BUF(arg)
#define MAX_ROOM_NEEDED
Use a different function name, to make it possible that the 'wchar_t' parametrization and the 'char' ...
#define IF_LINT(Code)
Here we need to call the native snprintf, not rpl_snprintf.
#define verify(R)
Verify requirement R at compile-time, as a declaration without a trailing ';'.
#define wint_t
The definitions of _GL_FUNCDECL_RPL etc.
XSIZE_INLINE size_t xsum4(size_t size1, size_t size2, size_t size3, size_t size4)
Sum of four sizes, with overflow check.
XSIZE_INLINE size_t xmax(size_t size1, size_t size2)
Maximum of two sizes, with overflow check.
XSIZE_INLINE size_t xsum3(size_t size1, size_t size2, size_t size3)
Sum of three sizes, with overflow check.