2.3.90-16
[glibc/history.git] / stdio-common / _i18n_number.h
blob04d6619b4c0c7f0469554ff6198e4f9f42f15521
1 /* Copyright (C) 2000, 2004 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <wchar.h>
21 #include <wctype.h>
23 #include "../locale/outdigits.h"
24 #include "../locale/outdigitswc.h"
26 static CHAR_T *
27 _i18n_number_rewrite (CHAR_T *w, CHAR_T *rear_ptr)
29 #ifdef COMPILE_WPRINTF
30 # define decimal NULL
31 # define thousands NULL
32 #else
33 char decimal[MB_LEN_MAX];
34 char thousands[MB_LEN_MAX];
35 #endif
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 (__builtin_expect (map != NULL, 0))
47 mbstate_t state;
48 memset (&state, '\0', sizeof (state));
50 if (__wcrtomb (decimal, wdecimal, &state) == (size_t) -1)
51 memcpy (decimal, ".", 2);
53 memset (&state, '\0', sizeof (state));
55 if (__wcrtomb (thousands, wthousands, &state) == (size_t) -1)
56 memcpy (thousands, ",", 2);
58 #endif
60 /* Copy existing string so that nothing gets overwritten. */
61 CHAR_T *src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
62 CHAR_T *s = (CHAR_T *) __mempcpy (src, w,
63 (rear_ptr - w) * sizeof (CHAR_T));
64 w = rear_ptr;
66 /* Process all characters in the string. */
67 while (--s >= src)
69 if (*s >= '0' && *s <= '9')
71 if (sizeof (CHAR_T) == 1)
72 w = (CHAR_T *) outdigit_value ((char *) w, *s - '0');
73 else
74 *--w = (CHAR_T) outdigitwc_value (*s - '0');
76 else if (__builtin_expect (map == NULL, 1) || (*s != '.' && *s != ','))
77 *--w = *s;
78 else
80 if (sizeof (CHAR_T) == 1)
82 const char *outpunct = *s == '.' ? decimal : thousands;
83 size_t dlen = strlen (outpunct);
85 w -= dlen;
86 while (dlen-- > 0)
87 w[dlen] = outpunct[dlen];
89 else
90 *--w = *s == '.' ? (CHAR_T) wdecimal : (CHAR_T) wthousands;
94 return w;