Blame stdio-common/_i18n_number.h

Packit 6c4009
/* Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <wctype.h>
Packit 6c4009
#include <scratch_buffer.h>
Packit 6c4009
Packit 6c4009
#include "../locale/outdigits.h"
Packit 6c4009
#include "../locale/outdigitswc.h"
Packit 6c4009
Packit 6c4009
static CHAR_T *
Packit 6c4009
_i18n_number_rewrite (CHAR_T *w, CHAR_T *rear_ptr, CHAR_T *end)
Packit 6c4009
{
Packit 6c4009
#ifdef COMPILE_WPRINTF
Packit 6c4009
# define decimal NULL
Packit 6c4009
# define thousands NULL
Packit 6c4009
#else
Packit 6c4009
  char decimal[MB_LEN_MAX + 1];
Packit 6c4009
  char thousands[MB_LEN_MAX + 1];
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* "to_outpunct" is a map from ASCII decimal point and thousands-sep
Packit 6c4009
     to their equivalent in locale. This is defined for locales which
Packit 6c4009
     use extra decimal point and thousands-sep.  */
Packit 6c4009
  wctrans_t map = __wctrans ("to_outpunct");
Packit 6c4009
  wint_t wdecimal = __towctrans (L'.', map);
Packit 6c4009
  wint_t wthousands = __towctrans (L',', map);
Packit 6c4009
Packit 6c4009
#ifndef COMPILE_WPRINTF
Packit 6c4009
  if (__glibc_unlikely (map != NULL))
Packit 6c4009
    {
Packit 6c4009
      mbstate_t state;
Packit 6c4009
      memset (&state, '\0', sizeof (state));
Packit 6c4009
Packit 6c4009
      size_t n = __wcrtomb (decimal, wdecimal, &state);
Packit 6c4009
      if (n == (size_t) -1)
Packit 6c4009
	memcpy (decimal, ".", 2);
Packit 6c4009
      else
Packit 6c4009
	decimal[n] = '\0';
Packit 6c4009
Packit 6c4009
      memset (&state, '\0', sizeof (state));
Packit 6c4009
Packit 6c4009
      n = __wcrtomb (thousands, wthousands, &state);
Packit 6c4009
      if (n == (size_t) -1)
Packit 6c4009
	memcpy (thousands, ",", 2);
Packit 6c4009
      else
Packit 6c4009
	thousands[n] = '\0';
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* Copy existing string so that nothing gets overwritten.  */
Packit 6c4009
  CHAR_T *src;
Packit 6c4009
  struct scratch_buffer buffer;
Packit 6c4009
  scratch_buffer_init (&buffer);
Packit 6c4009
  if (!scratch_buffer_set_array_size (&buffer, rear_ptr - w, sizeof (CHAR_T)))
Packit 6c4009
    /* If we cannot allocate the memory don't rewrite the string.
Packit 6c4009
       It is better than nothing.  */
Packit 6c4009
    return w;
Packit 6c4009
  src = buffer.data;
Packit 6c4009
Packit 6c4009
  CHAR_T *s = (CHAR_T *) __mempcpy (src, w,
Packit 6c4009
				    (rear_ptr - w) * sizeof (CHAR_T));
Packit 6c4009
Packit 6c4009
  w = end;
Packit 6c4009
Packit 6c4009
  /* Process all characters in the string.  */
Packit 6c4009
  while (--s >= src)
Packit 6c4009
    {
Packit 6c4009
      if (*s >= '0' && *s <= '9')
Packit 6c4009
	{
Packit 6c4009
	  if (sizeof (CHAR_T) == 1)
Packit 6c4009
	    w = (CHAR_T *) outdigit_value ((char *) w, *s - '0');
Packit 6c4009
	  else
Packit 6c4009
	    *--w = (CHAR_T) outdigitwc_value (*s - '0');
Packit 6c4009
	}
Packit 6c4009
      else if (__builtin_expect (map == NULL, 1) || (*s != '.' && *s != ','))
Packit 6c4009
	*--w = *s;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  if (sizeof (CHAR_T) == 1)
Packit 6c4009
	    {
Packit 6c4009
	      const char *outpunct = *s == '.' ? decimal : thousands;
Packit 6c4009
	      size_t dlen = strlen (outpunct);
Packit 6c4009
Packit 6c4009
	      w -= dlen;
Packit 6c4009
	      while (dlen-- > 0)
Packit 6c4009
		w[dlen] = outpunct[dlen];
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    *--w = *s == '.' ? (CHAR_T) wdecimal : (CHAR_T) wthousands;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  scratch_buffer_free (&buffer);
Packit 6c4009
  return w;
Packit 6c4009
}