]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/_i18n_number.h
iconv, localedef: avoid floating point rounding differences [BZ #24372]
[thirdparty/glibc.git] / stdio-common / _i18n_number.h
CommitLineData
04277e02 1/* Copyright (C) 2000-2019 Free Software Foundation, Inc.
4295702f 2 This file is part of the GNU C Library.
69c69fe1 3 Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
4295702f
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
4295702f
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
4295702f 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
4295702f 18
bea9b193 19#include <stdbool.h>
7be688b5
UD
20#include <wchar.h>
21#include <wctype.h>
cd00e12d 22#include <scratch_buffer.h>
7be688b5 23
69c69fe1 24#include "../locale/outdigits.h"
4295702f
UD
25#include "../locale/outdigitswc.h"
26
69c69fe1 27static CHAR_T *
3703468e 28_i18n_number_rewrite (CHAR_T *w, CHAR_T *rear_ptr, CHAR_T *end)
4295702f 29{
7be688b5 30#ifdef COMPILE_WPRINTF
7be688b5
UD
31# define decimal NULL
32# define thousands NULL
33#else
c1d0e639
JJ
34 char decimal[MB_LEN_MAX + 1];
35 char thousands[MB_LEN_MAX + 1];
7be688b5
UD
36#endif
37
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");
3bd6014e
UD
42 wint_t wdecimal = __towctrans (L'.', map);
43 wint_t wthousands = __towctrans (L',', map);
43e56b0e
UD
44
45#ifndef COMPILE_WPRINTF
a1ffb40e 46 if (__glibc_unlikely (map != NULL))
3bd6014e 47 {
7be688b5
UD
48 mbstate_t state;
49 memset (&state, '\0', sizeof (state));
50
c1d0e639
JJ
51 size_t n = __wcrtomb (decimal, wdecimal, &state);
52 if (n == (size_t) -1)
7be688b5 53 memcpy (decimal, ".", 2);
c1d0e639
JJ
54 else
55 decimal[n] = '\0';
7be688b5
UD
56
57 memset (&state, '\0', sizeof (state));
58
c1d0e639
JJ
59 n = __wcrtomb (thousands, wthousands, &state);
60 if (n == (size_t) -1)
7be688b5 61 memcpy (thousands, ",", 2);
c1d0e639
JJ
62 else
63 thousands[n] = '\0';
7be688b5 64 }
3bd6014e 65#endif
69c69fe1
UD
66
67 /* Copy existing string so that nothing gets overwritten. */
3703468e 68 CHAR_T *src;
cd00e12d
FW
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. */
74 return w;
75 src = buffer.data;
3703468e 76
7be688b5
UD
77 CHAR_T *s = (CHAR_T *) __mempcpy (src, w,
78 (rear_ptr - w) * sizeof (CHAR_T));
3703468e
UD
79
80 w = end;
69c69fe1
UD
81
82 /* Process all characters in the string. */
83 while (--s >= src)
84 {
85 if (*s >= '0' && *s <= '9')
86 {
87 if (sizeof (CHAR_T) == 1)
88 w = (CHAR_T *) outdigit_value ((char *) w, *s - '0');
89 else
90 *--w = (CHAR_T) outdigitwc_value (*s - '0');
91 }
7be688b5 92 else if (__builtin_expect (map == NULL, 1) || (*s != '.' && *s != ','))
69c69fe1 93 *--w = *s;
7be688b5
UD
94 else
95 {
96 if (sizeof (CHAR_T) == 1)
97 {
98 const char *outpunct = *s == '.' ? decimal : thousands;
99 size_t dlen = strlen (outpunct);
100
101 w -= dlen;
102 while (dlen-- > 0)
103 w[dlen] = outpunct[dlen];
104 }
105 else
106 *--w = *s == '.' ? (CHAR_T) wdecimal : (CHAR_T) wthousands;
107 }
69c69fe1
UD
108 }
109
cd00e12d 110 scratch_buffer_free (&buffer);
69c69fe1 111 return w;
4295702f 112}