2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
7 * Copyright (c) 2011 The FreeBSD Foundation
9 * Portions of this software were developed by David Chisnall
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
49 #include "xlocale_private.h"
52 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
53 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
54 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
55 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
56 #define SUPPRESS_CURR_SYMBOL 0x10 /* suppress the currency from output */
57 #define LEFT_JUSTIFY 0x20 /* left justify */
58 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
59 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
62 #define PRINT(CH) do { \
63 if (dst >= s + maxsize) \
68 #define PRINTS(STR) do { \
70 while (*tmps != '\0') \
74 #define GET_NUMBER(VAR, LOC) do { \
76 while (isdigit_l((unsigned char)*fmt, LOC)) { \
77 if (VAR > INT_MAX / 10) \
87 #define GRPCPY(howmany) do { \
91 *--bufend = *(avalue + avalue_size + padded); \
96 bufend -= thousands_sep_size; \
97 memcpy(bufend, thousands_sep, thousands_sep_size); \
101 static void __setup_vars(int, char *, char *, char *, char **, struct lconv *);
102 static int __calc_left_pad(int, char *, struct lconv *);
103 static char *__format_grouped_double(double, int *, int, int, int,
104 struct lconv *, locale_t);
107 vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
108 const char * __restrict format, va_list ap)
110 char *dst; /* output destination pointer */
111 const char *fmt; /* current format poistion pointer */
112 struct lconv *lc; /* pointer to lconv structure */
113 char *asciivalue; /* formatted double pointer */
115 int flags; /* formatting options */
116 int pad_char; /* padding character */
117 int pad_size; /* pad size */
118 int width; /* field width */
119 int left_prec; /* left precision */
120 int right_prec; /* right precision */
121 double value; /* just value */
122 char space_char = ' '; /* space after currency */
124 char cs_precedes, /* values gathered from struct lconv */
130 char *tmpptr; /* temporary vars */
134 lc = localeconv_l(loc);
138 currency_symbol = NULL;
141 /* pass nonformating characters AS IS */
147 /* "%%" mean just '%' */
148 if (*(fmt + 1) == '%') {
155 /* set up initial values */
156 flags = (NEED_GROUPING|LOCALE_POSN);
157 pad_char = ' '; /* padding character is "space" */
158 pad_size = 0; /* no padding initially */
159 left_prec = -1; /* no left precision specified */
160 right_prec = -1; /* no right precision specified */
161 width = -1; /* no width specified */
162 value = 0; /* we have no value to print now */
167 case '=': /* fill character */
169 if (pad_char == '\0')
172 case '^': /* not group currency */
173 flags &= ~(NEED_GROUPING);
175 case '+': /* use locale defined signs */
176 if (flags & SIGN_POSN_USED)
178 flags |= (SIGN_POSN_USED|LOCALE_POSN);
180 case '(': /* enclose negatives with () */
181 if (flags & SIGN_POSN_USED)
183 flags |= (SIGN_POSN_USED|PARENTH_POSN);
185 case '!': /* suppress currency symbol */
186 flags |= SUPPRESS_CURR_SYMBOL;
188 case '-': /* alignment (left) */
189 flags |= LEFT_JUSTIFY;
198 if (isdigit_l((unsigned char)*fmt, loc)) {
199 GET_NUMBER(width, loc);
200 /* Do we have enough space to put number with
203 if ((unsigned int)width >= maxsize - (dst - s))
209 if (!isdigit_l((unsigned char)*++fmt, loc))
211 GET_NUMBER(left_prec, loc);
212 if ((unsigned int)left_prec >= maxsize - (dst - s))
216 /* Right precision */
218 if (!isdigit_l((unsigned char)*++fmt, loc))
220 GET_NUMBER(right_prec, loc);
221 if ((unsigned int)right_prec >= maxsize - (dst - s) -
226 /* Conversion Characters */
228 case 'i': /* use international currency format */
229 flags |= USE_INTL_CURRENCY;
231 case 'n': /* use national currency format */
232 flags &= ~(USE_INTL_CURRENCY);
234 default: /* required character is missing or
239 if (currency_symbol != NULL)
240 free(currency_symbol);
241 if (flags & USE_INTL_CURRENCY) {
242 currency_symbol = strdup(lc->int_curr_symbol);
243 if (currency_symbol != NULL &&
244 strlen(currency_symbol) > 3) {
245 space_char = currency_symbol[3];
246 currency_symbol[3] = '\0';
249 currency_symbol = strdup(lc->currency_symbol);
251 if (currency_symbol == NULL)
252 goto end_error; /* ENOMEM. */
255 value = va_arg(ap, double);
259 flags |= IS_NEGATIVE;
263 /* fill left_prec with amount of padding chars */
264 if (left_prec >= 0) {
265 pad_size = __calc_left_pad((flags ^ IS_NEGATIVE),
266 currency_symbol, lc) -
267 __calc_left_pad(flags, currency_symbol, lc);
272 if (asciivalue != NULL)
274 asciivalue = __format_grouped_double(value, &flags,
275 left_prec, right_prec, pad_char, lc, loc);
276 if (asciivalue == NULL)
277 goto end_error; /* errno already set */
278 /* to ENOMEM by malloc() */
280 /* set some variables for later use */
281 __setup_vars(flags, &cs_precedes, &sep_by_space,
282 &sign_posn, &signstr, lc);
285 * Description of some LC_MONETARY's values:
287 * p_cs_precedes & n_cs_precedes
289 * = 1 - $currency_symbol precedes the value
290 * for a monetary quantity with a non-negative value
291 * = 0 - symbol succeeds the value
293 * p_sep_by_space & n_sep_by_space
295 * = 0 - no space separates $currency_symbol
296 * from the value for a monetary quantity with a
298 * = 1 - space separates the symbol from the value
299 * = 2 - space separates the symbol and the sign string,
300 * if adjacent; otherwise, a space separates
301 * the sign string from the value
303 * p_sign_posn & n_sign_posn
305 * = 0 - parentheses enclose the quantity and the
307 * = 1 - the sign string precedes the quantity and the
309 * = 2 - the sign string succeeds the quantity and the
311 * = 3 - the sign string precedes the $currency_symbol
312 * = 4 - the sign string succeeds the $currency_symbol
318 while (pad_size-- > 0)
321 if (sign_posn == 0 && (flags & IS_NEGATIVE))
324 if (cs_precedes == 1) {
325 if (sign_posn == 1 || sign_posn == 3) {
327 if (sep_by_space == 2)
331 if (!(flags & SUPPRESS_CURR_SYMBOL)) {
332 PRINTS(currency_symbol);
334 if (sign_posn == 4) {
335 if (sep_by_space == 2)
338 if (sep_by_space == 1)
340 } else if (sep_by_space == 1)
343 } else if (sign_posn == 1) {
345 if (sep_by_space == 2)
351 if (cs_precedes == 0) {
352 if (sign_posn == 3) {
353 if (sep_by_space == 1)
358 if (!(flags & SUPPRESS_CURR_SYMBOL)) {
359 if ((sign_posn == 3 && sep_by_space == 2)
360 || (sep_by_space == 1
366 PRINTS(currency_symbol);
367 if (sign_posn == 4) {
368 if (sep_by_space == 2)
375 if (sign_posn == 2) {
376 if (sep_by_space == 2)
381 if (sign_posn == 0) {
382 if (flags & IS_NEGATIVE)
384 else if (left_prec >= 0)
388 if (dst - tmpptr < width) {
389 if (flags & LEFT_JUSTIFY) {
390 while (dst - tmpptr < width)
393 pad_size = dst - tmpptr;
394 memmove(tmpptr + width - pad_size, tmpptr,
396 memset(tmpptr, ' ', width - pad_size);
397 dst += width - pad_size;
404 free(currency_symbol);
405 return (dst - s - 1); /* return size of put data except trailing '\0' */
416 if (asciivalue != NULL)
418 if (currency_symbol != NULL)
419 free(currency_symbol);
425 __setup_vars(int flags, char *cs_precedes, char *sep_by_space,
426 char *sign_posn, char **signstr, struct lconv *lc)
428 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
429 *cs_precedes = lc->int_n_cs_precedes;
430 *sep_by_space = lc->int_n_sep_by_space;
431 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
432 *signstr = (lc->negative_sign[0] == '\0') ? "-"
434 } else if (flags & USE_INTL_CURRENCY) {
435 *cs_precedes = lc->int_p_cs_precedes;
436 *sep_by_space = lc->int_p_sep_by_space;
437 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
438 *signstr = lc->positive_sign;
439 } else if (flags & IS_NEGATIVE) {
440 *cs_precedes = lc->n_cs_precedes;
441 *sep_by_space = lc->n_sep_by_space;
442 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
443 *signstr = (lc->negative_sign[0] == '\0') ? "-"
446 *cs_precedes = lc->p_cs_precedes;
447 *sep_by_space = lc->p_sep_by_space;
448 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
449 *signstr = lc->positive_sign;
452 /* Set default values for unspecified information. */
453 if (*cs_precedes != 0)
455 if (*sep_by_space == CHAR_MAX)
457 if (*sign_posn == CHAR_MAX)
462 __calc_left_pad(int flags, char *cur_symb, struct lconv *lc)
464 char cs_precedes, sep_by_space, sign_posn, *signstr;
467 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn,
470 if (cs_precedes != 0) {
471 left_chars += strlen(cur_symb);
472 if (sep_by_space != 0)
478 if (flags & IS_NEGATIVE)
482 left_chars += strlen(signstr);
486 if (cs_precedes != 0)
487 left_chars += strlen(signstr);
493 get_groups(int size, char *grouping)
497 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */
500 while (size > (int)*grouping) {
502 size -= (int)*grouping++;
503 /* no more grouping ? */
504 if (*grouping == CHAR_MAX)
506 /* rest grouping with same value ? */
507 if (*grouping == 0) {
508 chars += (size - 1) / *(grouping - 1);
515 /* convert double to locale-encoded string */
517 __format_grouped_double(double value, int *flags,
518 int left_prec, int right_prec, int pad_char, struct lconv *lc, locale_t loc)
531 const char *decimal_point;
532 const char *thousands_sep;
533 size_t decimal_point_size;
534 size_t thousands_sep_size;
538 grouping = lc->mon_grouping;
539 decimal_point = lc->mon_decimal_point;
540 if (*decimal_point == '\0')
541 decimal_point = lc->decimal_point;
542 thousands_sep = lc->mon_thousands_sep;
543 if (*thousands_sep == '\0')
544 thousands_sep = lc->thousands_sep;
546 decimal_point_size = strlen(decimal_point);
547 thousands_sep_size = strlen(thousands_sep);
549 /* fill left_prec with default value */
553 /* fill right_prec with default value */
554 if (right_prec == -1) {
555 if (*flags & USE_INTL_CURRENCY)
556 right_prec = lc->int_frac_digits;
558 right_prec = lc->frac_digits;
560 if (right_prec == CHAR_MAX) /* POSIX locale ? */
564 if (*flags & NEED_GROUPING)
565 left_prec += get_groups(left_prec, grouping);
567 /* convert to string */
568 avalue_size = asprintf_l(&avalue, loc, "%*.*f",
569 left_prec + right_prec + 1, right_prec, value);
573 /* make sure that we've enough space for result string */
574 bufsize = avalue_size * (1 + thousands_sep_size) + decimal_point_size +
576 rslt = calloc(1, bufsize);
581 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */
583 /* skip spaces at beginning */
585 while (avalue[padded] == ' ') {
590 if (right_prec > 0) {
591 bufend -= right_prec;
592 memcpy(bufend, avalue + avalue_size + padded - right_prec,
594 bufend -= decimal_point_size;
595 memcpy(bufend, decimal_point, decimal_point_size);
596 avalue_size -= (right_prec + 1);
599 if ((*flags & NEED_GROUPING) &&
600 thousands_sep_size > 0 &&
601 *grouping != CHAR_MAX &&
603 while (avalue_size > (int)*grouping) {
608 /* no more grouping ? */
609 if (*grouping == CHAR_MAX)
612 /* rest grouping with same value ? */
613 if (*grouping == 0) {
615 while (avalue_size > *grouping) {
621 if (avalue_size != 0)
625 bufend -= avalue_size;
626 memcpy(bufend, avalue + padded, avalue_size);
627 /* decrease assumed $decimal_point */
629 padded -= decimal_point_size;
632 /* do padding with pad_char */
635 memset(bufend, pad_char, padded);
638 bufsize = rslt + bufsize - bufend;
639 memmove(rslt, bufend, bufsize);
645 strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
651 va_start(ap, format);
652 ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap);
659 strfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
660 const char * __restrict format, ...)
665 va_start(ap, format);
666 ret = vstrfmon_l(s, maxsize, loc, format, ap);