Back to index

glibc  2.9
_i18n_number.h
Go to the documentation of this file.
00001 /* Copyright (C) 2000, 2004, 2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <wchar.h>
00021 #include <wctype.h>
00022 
00023 #include "../locale/outdigits.h"
00024 #include "../locale/outdigitswc.h"
00025 
00026 static CHAR_T *
00027 _i18n_number_rewrite (CHAR_T *w, CHAR_T *rear_ptr, CHAR_T *end)
00028 {
00029 #ifdef COMPILE_WPRINTF
00030 # define decimal NULL
00031 # define thousands NULL
00032 #else
00033   char decimal[MB_LEN_MAX];
00034   char thousands[MB_LEN_MAX];
00035 #endif
00036 
00037   /* "to_outpunct" is a map from ASCII decimal point and thousands-sep
00038      to their equivalent in locale. This is defined for locales which
00039      use extra decimal point and thousands-sep.  */
00040   wctrans_t map = __wctrans ("to_outpunct");
00041   wint_t wdecimal = __towctrans (L'.', map);
00042   wint_t wthousands = __towctrans (L',', map);
00043 
00044 #ifndef COMPILE_WPRINTF
00045   if (__builtin_expect (map != NULL, 0))
00046     {
00047       mbstate_t state;
00048       memset (&state, '\0', sizeof (state));
00049 
00050       if (__wcrtomb (decimal, wdecimal, &state) == (size_t) -1)
00051        memcpy (decimal, ".", 2);
00052 
00053       memset (&state, '\0', sizeof (state));
00054 
00055       if (__wcrtomb (thousands, wthousands, &state) == (size_t) -1)
00056        memcpy (thousands, ",", 2);
00057     }
00058 #endif
00059 
00060   /* Copy existing string so that nothing gets overwritten.  */
00061   CHAR_T *src;
00062   bool use_alloca = __libc_use_alloca ((rear_ptr - w) * sizeof (CHAR_T));
00063   if (__builtin_expect (use_alloca, true))
00064     src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
00065   else
00066     {
00067       src = (CHAR_T *) malloc ((rear_ptr - w) * sizeof (CHAR_T));
00068       if (src == NULL)
00069        /* If we cannot allocate the memory don't rewrite the string.
00070           It is better than nothing.  */
00071        return w;
00072     }
00073 
00074   CHAR_T *s = (CHAR_T *) __mempcpy (src, w,
00075                                 (rear_ptr - w) * sizeof (CHAR_T));
00076 
00077   w = end;
00078 
00079   /* Process all characters in the string.  */
00080   while (--s >= src)
00081     {
00082       if (*s >= '0' && *s <= '9')
00083        {
00084          if (sizeof (CHAR_T) == 1)
00085            w = (CHAR_T *) outdigit_value ((char *) w, *s - '0');
00086          else
00087            *--w = (CHAR_T) outdigitwc_value (*s - '0');
00088        }
00089       else if (__builtin_expect (map == NULL, 1) || (*s != '.' && *s != ','))
00090        *--w = *s;
00091       else
00092        {
00093          if (sizeof (CHAR_T) == 1)
00094            {
00095              const char *outpunct = *s == '.' ? decimal : thousands;
00096              size_t dlen = strlen (outpunct);
00097 
00098              w -= dlen;
00099              while (dlen-- > 0)
00100               w[dlen] = outpunct[dlen];
00101            }
00102          else
00103            *--w = *s == '.' ? (CHAR_T) wdecimal : (CHAR_T) wthousands;
00104        }
00105     }
00106 
00107   if (! use_alloca)
00108     free (src);
00109 
00110   return w;
00111 }