1 /* Copyright (C) 2000-2020 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 <https://www.gnu.org/licenses/>. */
22 #include <scratch_buffer.h>
24 #include "../locale/outdigits.h"
25 #include "../locale/outdigitswc.h"
28 _i18n_number_rewrite (CHAR_T
*w
, CHAR_T
*rear_ptr
, CHAR_T
*end
)
30 #ifdef COMPILE_WPRINTF
32 # define thousands NULL
34 char decimal
[MB_LEN_MAX
+ 1];
35 char thousands
[MB_LEN_MAX
+ 1];
38 /* "to_outpunct" is a map from ASCII decimal point and thousands-sep
39 to their equivalent in locale. This is defined for locales which
40 use extra decimal point and thousands-sep. */
41 wctrans_t map
= __wctrans ("to_outpunct");
42 wint_t wdecimal
= __towctrans (L
'.', map
);
43 wint_t wthousands
= __towctrans (L
',', map
);
45 #ifndef COMPILE_WPRINTF
46 if (__glibc_unlikely (map
!= NULL
))
49 memset (&state
, '\0', sizeof (state
));
51 size_t n
= __wcrtomb (decimal
, wdecimal
, &state
);
53 memcpy (decimal
, ".", 2);
57 memset (&state
, '\0', sizeof (state
));
59 n
= __wcrtomb (thousands
, wthousands
, &state
);
61 memcpy (thousands
, ",", 2);
67 /* Copy existing string so that nothing gets overwritten. */
69 struct scratch_buffer buffer
;
70 scratch_buffer_init (&buffer
);
71 if (!scratch_buffer_set_array_size (&buffer
, rear_ptr
- w
, sizeof (CHAR_T
)))
72 /* If we cannot allocate the memory don't rewrite the string.
73 It is better than nothing. */
77 CHAR_T
*s
= (CHAR_T
*) __mempcpy (src
, w
,
78 (rear_ptr
- w
) * sizeof (CHAR_T
));
82 /* Process all characters in the string. */
85 if (*s
>= '0' && *s
<= '9')
87 if (sizeof (CHAR_T
) == 1)
88 w
= (CHAR_T
*) outdigit_value ((char *) w
, *s
- '0');
90 *--w
= (CHAR_T
) outdigitwc_value (*s
- '0');
92 else if (__builtin_expect (map
== NULL
, 1) || (*s
!= '.' && *s
!= ','))
96 if (sizeof (CHAR_T
) == 1)
98 const char *outpunct
= *s
== '.' ? decimal
: thousands
;
99 size_t dlen
= strlen (outpunct
);
103 w
[dlen
] = outpunct
[dlen
];
106 *--w
= *s
== '.' ? (CHAR_T
) wdecimal
: (CHAR_T
) wthousands
;
110 scratch_buffer_free (&buffer
);