1 /* Copyright (C) 2000-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
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, see
17 <http://www.gnu.org/licenses/>. */
23 #include "../locale/outdigits.h"
24 #include "../locale/outdigitswc.h"
27 _i18n_number_rewrite (CHAR_T
*w
, CHAR_T
*rear_ptr
, CHAR_T
*end
)
29 #ifdef COMPILE_WPRINTF
31 # define thousands NULL
33 char decimal
[MB_LEN_MAX
+ 1];
34 char thousands
[MB_LEN_MAX
+ 1];
37 /* "to_outpunct" is a map from ASCII decimal point and thousands-sep
38 to their equivalent in locale. This is defined for locales which
39 use extra decimal point and thousands-sep. */
40 wctrans_t map
= __wctrans ("to_outpunct");
41 wint_t wdecimal
= __towctrans (L
'.', map
);
42 wint_t wthousands
= __towctrans (L
',', map
);
44 #ifndef COMPILE_WPRINTF
45 if (__glibc_unlikely (map
!= NULL
))
48 memset (&state
, '\0', sizeof (state
));
50 size_t n
= __wcrtomb (decimal
, wdecimal
, &state
);
52 memcpy (decimal
, ".", 2);
56 memset (&state
, '\0', sizeof (state
));
58 n
= __wcrtomb (thousands
, wthousands
, &state
);
60 memcpy (thousands
, ",", 2);
66 /* Copy existing string so that nothing gets overwritten. */
68 bool use_alloca
= __libc_use_alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
69 if (__builtin_expect (use_alloca
, true))
70 src
= (CHAR_T
*) alloca ((rear_ptr
- w
) * sizeof (CHAR_T
));
73 src
= (CHAR_T
*) malloc ((rear_ptr
- w
) * sizeof (CHAR_T
));
75 /* If we cannot allocate the memory don't rewrite the string.
76 It is better than nothing. */
80 CHAR_T
*s
= (CHAR_T
*) __mempcpy (src
, w
,
81 (rear_ptr
- w
) * sizeof (CHAR_T
));
85 /* Process all characters in the string. */
88 if (*s
>= '0' && *s
<= '9')
90 if (sizeof (CHAR_T
) == 1)
91 w
= (CHAR_T
*) outdigit_value ((char *) w
, *s
- '0');
93 *--w
= (CHAR_T
) outdigitwc_value (*s
- '0');
95 else if (__builtin_expect (map
== NULL
, 1) || (*s
!= '.' && *s
!= ','))
99 if (sizeof (CHAR_T
) == 1)
101 const char *outpunct
= *s
== '.' ? decimal
: thousands
;
102 size_t dlen
= strlen (outpunct
);
106 w
[dlen
] = outpunct
[dlen
];
109 *--w
= *s
== '.' ? (CHAR_T
) wdecimal
: (CHAR_T
) wthousands
;