]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/_i18n_number.h
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / stdio-common / _i18n_number.h
1 /* Copyright (C) 2000, 2004, 2008 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
4
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.
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
13 Lesser General Public License for more details.
14
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/>. */
18
19 #include <wchar.h>
20 #include <wctype.h>
21
22 #include "../locale/outdigits.h"
23 #include "../locale/outdigitswc.h"
24
25 static CHAR_T *
26 _i18n_number_rewrite (CHAR_T *w, CHAR_T *rear_ptr, CHAR_T *end)
27 {
28 #ifdef COMPILE_WPRINTF
29 # define decimal NULL
30 # define thousands NULL
31 #else
32 char decimal[MB_LEN_MAX + 1];
33 char thousands[MB_LEN_MAX + 1];
34 #endif
35
36 /* "to_outpunct" is a map from ASCII decimal point and thousands-sep
37 to their equivalent in locale. This is defined for locales which
38 use extra decimal point and thousands-sep. */
39 wctrans_t map = __wctrans ("to_outpunct");
40 wint_t wdecimal = __towctrans (L'.', map);
41 wint_t wthousands = __towctrans (L',', map);
42
43 #ifndef COMPILE_WPRINTF
44 if (__builtin_expect (map != NULL, 0))
45 {
46 mbstate_t state;
47 memset (&state, '\0', sizeof (state));
48
49 size_t n = __wcrtomb (decimal, wdecimal, &state);
50 if (n == (size_t) -1)
51 memcpy (decimal, ".", 2);
52 else
53 decimal[n] = '\0';
54
55 memset (&state, '\0', sizeof (state));
56
57 n = __wcrtomb (thousands, wthousands, &state);
58 if (n == (size_t) -1)
59 memcpy (thousands, ",", 2);
60 else
61 thousands[n] = '\0';
62 }
63 #endif
64
65 /* Copy existing string so that nothing gets overwritten. */
66 CHAR_T *src;
67 bool use_alloca = __libc_use_alloca ((rear_ptr - w) * sizeof (CHAR_T));
68 if (__builtin_expect (use_alloca, true))
69 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
70 else
71 {
72 src = (CHAR_T *) malloc ((rear_ptr - w) * sizeof (CHAR_T));
73 if (src == NULL)
74 /* If we cannot allocate the memory don't rewrite the string.
75 It is better than nothing. */
76 return w;
77 }
78
79 CHAR_T *s = (CHAR_T *) __mempcpy (src, w,
80 (rear_ptr - w) * sizeof (CHAR_T));
81
82 w = end;
83
84 /* Process all characters in the string. */
85 while (--s >= src)
86 {
87 if (*s >= '0' && *s <= '9')
88 {
89 if (sizeof (CHAR_T) == 1)
90 w = (CHAR_T *) outdigit_value ((char *) w, *s - '0');
91 else
92 *--w = (CHAR_T) outdigitwc_value (*s - '0');
93 }
94 else if (__builtin_expect (map == NULL, 1) || (*s != '.' && *s != ','))
95 *--w = *s;
96 else
97 {
98 if (sizeof (CHAR_T) == 1)
99 {
100 const char *outpunct = *s == '.' ? decimal : thousands;
101 size_t dlen = strlen (outpunct);
102
103 w -= dlen;
104 while (dlen-- > 0)
105 w[dlen] = outpunct[dlen];
106 }
107 else
108 *--w = *s == '.' ? (CHAR_T) wdecimal : (CHAR_T) wthousands;
109 }
110 }
111
112 if (! use_alloca)
113 free (src);
114
115 return w;
116 }