]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/_i18n_number.h
Update sysdeps/unix/sysv/linux/sparc/bits/socket.h
[thirdparty/glibc.git] / stdio-common / _i18n_number.h
CommitLineData
3703468e 1/* Copyright (C) 2000, 2004, 2008 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
AJ
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. */
4295702f 19
7be688b5
UD
20#include <wchar.h>
21#include <wctype.h>
22
69c69fe1 23#include "../locale/outdigits.h"
4295702f
UD
24#include "../locale/outdigitswc.h"
25
69c69fe1 26static CHAR_T *
3703468e 27_i18n_number_rewrite (CHAR_T *w, CHAR_T *rear_ptr, CHAR_T *end)
4295702f 28{
7be688b5 29#ifdef COMPILE_WPRINTF
7be688b5
UD
30# define decimal NULL
31# define thousands NULL
32#else
7be688b5
UD
33 char decimal[MB_LEN_MAX];
34 char thousands[MB_LEN_MAX];
35#endif
36
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");
3bd6014e
UD
41 wint_t wdecimal = __towctrans (L'.', map);
42 wint_t wthousands = __towctrans (L',', map);
43e56b0e
UD
43
44#ifndef COMPILE_WPRINTF
3bd6014e
UD
45 if (__builtin_expect (map != NULL, 0))
46 {
7be688b5
UD
47 mbstate_t state;
48 memset (&state, '\0', sizeof (state));
49
50 if (__wcrtomb (decimal, wdecimal, &state) == (size_t) -1)
51 memcpy (decimal, ".", 2);
52
53 memset (&state, '\0', sizeof (state));
54
55 if (__wcrtomb (thousands, wthousands, &state) == (size_t) -1)
56 memcpy (thousands, ",", 2);
57 }
3bd6014e 58#endif
69c69fe1
UD
59
60 /* Copy existing string so that nothing gets overwritten. */
3703468e
UD
61 CHAR_T *src;
62 bool use_alloca = __libc_use_alloca ((rear_ptr - w) * sizeof (CHAR_T));
63 if (__builtin_expect (use_alloca, true))
64 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
65 else
66 {
67 src = (CHAR_T *) malloc ((rear_ptr - w) * sizeof (CHAR_T));
68 if (src == NULL)
69 /* If we cannot allocate the memory don't rewrite the string.
70 It is better than nothing. */
71 return w;
72 }
73
7be688b5
UD
74 CHAR_T *s = (CHAR_T *) __mempcpy (src, w,
75 (rear_ptr - w) * sizeof (CHAR_T));
3703468e
UD
76
77 w = end;
69c69fe1
UD
78
79 /* Process all characters in the string. */
80 while (--s >= src)
81 {
82 if (*s >= '0' && *s <= '9')
83 {
84 if (sizeof (CHAR_T) == 1)
85 w = (CHAR_T *) outdigit_value ((char *) w, *s - '0');
86 else
87 *--w = (CHAR_T) outdigitwc_value (*s - '0');
88 }
7be688b5 89 else if (__builtin_expect (map == NULL, 1) || (*s != '.' && *s != ','))
69c69fe1 90 *--w = *s;
7be688b5
UD
91 else
92 {
93 if (sizeof (CHAR_T) == 1)
94 {
95 const char *outpunct = *s == '.' ? decimal : thousands;
96 size_t dlen = strlen (outpunct);
97
98 w -= dlen;
99 while (dlen-- > 0)
100 w[dlen] = outpunct[dlen];
101 }
102 else
103 *--w = *s == '.' ? (CHAR_T) wdecimal : (CHAR_T) wthousands;
104 }
69c69fe1
UD
105 }
106
3703468e
UD
107 if (! use_alloca)
108 free (src);
109
69c69fe1 110 return w;
4295702f 111}