]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/_i18n_number.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / stdio-common / _i18n_number.h
CommitLineData
b168057a 1/* Copyright (C) 2000-2015 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>
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
c1d0e639
JJ
33 char decimal[MB_LEN_MAX + 1];
34 char thousands[MB_LEN_MAX + 1];
7be688b5
UD
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
a1ffb40e 45 if (__glibc_unlikely (map != NULL))
3bd6014e 46 {
7be688b5
UD
47 mbstate_t state;
48 memset (&state, '\0', sizeof (state));
49
c1d0e639
JJ
50 size_t n = __wcrtomb (decimal, wdecimal, &state);
51 if (n == (size_t) -1)
7be688b5 52 memcpy (decimal, ".", 2);
c1d0e639
JJ
53 else
54 decimal[n] = '\0';
7be688b5
UD
55
56 memset (&state, '\0', sizeof (state));
57
c1d0e639
JJ
58 n = __wcrtomb (thousands, wthousands, &state);
59 if (n == (size_t) -1)
7be688b5 60 memcpy (thousands, ",", 2);
c1d0e639
JJ
61 else
62 thousands[n] = '\0';
7be688b5 63 }
3bd6014e 64#endif
69c69fe1
UD
65
66 /* Copy existing string so that nothing gets overwritten. */
3703468e
UD
67 CHAR_T *src;
68 bool use_alloca = __libc_use_alloca ((rear_ptr - w) * sizeof (CHAR_T));
69 if (__builtin_expect (use_alloca, true))
70 src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
71 else
72 {
73 src = (CHAR_T *) malloc ((rear_ptr - w) * sizeof (CHAR_T));
74 if (src == NULL)
75 /* If we cannot allocate the memory don't rewrite the string.
76 It is better than nothing. */
77 return w;
78 }
79
7be688b5
UD
80 CHAR_T *s = (CHAR_T *) __mempcpy (src, w,
81 (rear_ptr - w) * sizeof (CHAR_T));
3703468e
UD
82
83 w = end;
69c69fe1
UD
84
85 /* Process all characters in the string. */
86 while (--s >= src)
87 {
88 if (*s >= '0' && *s <= '9')
89 {
90 if (sizeof (CHAR_T) == 1)
91 w = (CHAR_T *) outdigit_value ((char *) w, *s - '0');
92 else
93 *--w = (CHAR_T) outdigitwc_value (*s - '0');
94 }
7be688b5 95 else if (__builtin_expect (map == NULL, 1) || (*s != '.' && *s != ','))
69c69fe1 96 *--w = *s;
7be688b5
UD
97 else
98 {
99 if (sizeof (CHAR_T) == 1)
100 {
101 const char *outpunct = *s == '.' ? decimal : thousands;
102 size_t dlen = strlen (outpunct);
103
104 w -= dlen;
105 while (dlen-- > 0)
106 w[dlen] = outpunct[dlen];
107 }
108 else
109 *--w = *s == '.' ? (CHAR_T) wdecimal : (CHAR_T) wthousands;
110 }
69c69fe1
UD
111 }
112
3703468e
UD
113 if (! use_alloca)
114 free (src);
115
69c69fe1 116 return w;
4295702f 117}