Blame stdlib/grouping.c

Packit 6c4009
/* Internal header for proving correct grouping in strings of numbers.
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
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 <limits.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#ifndef MAX
Packit 6c4009
#define MAX(a,b)	({ typeof(a) _a = (a); typeof(b) _b = (b); \
Packit 6c4009
			   _a > _b ? _a : _b; })
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifdef USE_WIDE_CHAR
Packit 6c4009
# include <wctype.h>
Packit 6c4009
# define L_(Ch) L##Ch
Packit 6c4009
# define UCHAR_TYPE wint_t
Packit 6c4009
# define STRING_TYPE wchar_t
Packit 6c4009
#else
Packit 6c4009
# define L_(Ch) Ch
Packit 6c4009
# define UCHAR_TYPE unsigned char
Packit 6c4009
# define STRING_TYPE char
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include "grouping.h"
Packit 6c4009
Packit 6c4009
/* Find the maximum prefix of the string between BEGIN and END which
Packit 6c4009
   satisfies the grouping rules.  It is assumed that at least one digit
Packit 6c4009
   follows BEGIN directly.  */
Packit 6c4009
Packit 6c4009
const STRING_TYPE *
Packit 6c4009
#ifdef USE_WIDE_CHAR
Packit 6c4009
__correctly_grouped_prefixwc (const STRING_TYPE *begin, const STRING_TYPE *end,
Packit 6c4009
			      wchar_t thousands,
Packit 6c4009
#else
Packit 6c4009
__correctly_grouped_prefixmb (const STRING_TYPE *begin, const STRING_TYPE *end,
Packit 6c4009
			      const char *thousands,
Packit 6c4009
#endif
Packit 6c4009
			      const char *grouping)
Packit 6c4009
{
Packit 6c4009
#ifndef USE_WIDE_CHAR
Packit 6c4009
  size_t thousands_len;
Packit 6c4009
  int cnt;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (grouping == NULL)
Packit 6c4009
    return end;
Packit 6c4009
Packit 6c4009
#ifndef USE_WIDE_CHAR
Packit 6c4009
  thousands_len = strlen (thousands);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  while (end > begin)
Packit 6c4009
    {
Packit 6c4009
      const STRING_TYPE *cp = end - 1;
Packit 6c4009
      const char *gp = grouping;
Packit 6c4009
Packit 6c4009
      /* Check first group.  */
Packit 6c4009
      while (cp >= begin)
Packit 6c4009
	{
Packit 6c4009
#ifdef USE_WIDE_CHAR
Packit 6c4009
	  if (*cp == thousands)
Packit 6c4009
	    break;
Packit 6c4009
#else
Packit 6c4009
	  if (cp[thousands_len - 1] == *thousands)
Packit 6c4009
	    {
Packit 6c4009
	      for (cnt = 1; thousands[cnt] != '\0'; ++cnt)
Packit 6c4009
		if (thousands[cnt] != cp[thousands_len - 1 - cnt])
Packit 6c4009
		  break;
Packit 6c4009
	      if (thousands[cnt] == '\0')
Packit 6c4009
		break;
Packit 6c4009
	    }
Packit 6c4009
#endif
Packit 6c4009
	  --cp;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* We allow the representation to contain no grouping at all even if
Packit 6c4009
	 the locale specifies we can have grouping.  */
Packit 6c4009
      if (cp < begin)
Packit 6c4009
	return end;
Packit 6c4009
Packit 6c4009
      if (end - cp == (int) *gp + 1)
Packit 6c4009
	{
Packit 6c4009
	  /* This group matches the specification.  */
Packit 6c4009
Packit 6c4009
	  const STRING_TYPE *new_end;
Packit 6c4009
Packit 6c4009
	  if (cp < begin)
Packit 6c4009
	    /* There is just one complete group.  We are done.  */
Packit 6c4009
	    return end;
Packit 6c4009
Packit 6c4009
	  /* CP points to a thousands separator character.  The preceding
Packit 6c4009
	     remainder of the string from BEGIN to NEW_END is the part we
Packit 6c4009
	     will consider if there is a grouping error in this trailing
Packit 6c4009
	     portion from CP to END.  */
Packit 6c4009
	  new_end = cp - 1;
Packit 6c4009
Packit 6c4009
	  /* Loop while the grouping is correct.  */
Packit 6c4009
	  while (1)
Packit 6c4009
	    {
Packit 6c4009
	      /* Get the next grouping rule.  */
Packit 6c4009
	      ++gp;
Packit 6c4009
	      if (*gp == 0)
Packit 6c4009
		/* If end is reached use last rule.  */
Packit 6c4009
	        --gp;
Packit 6c4009
Packit 6c4009
	      /* Skip the thousands separator.  */
Packit 6c4009
	      --cp;
Packit 6c4009
Packit 6c4009
	      if (*gp == CHAR_MAX
Packit 6c4009
#if CHAR_MIN < 0
Packit 6c4009
		  || *gp < 0
Packit 6c4009
#endif
Packit 6c4009
		  )
Packit 6c4009
	        {
Packit 6c4009
	          /* No more thousands separators are allowed to follow.  */
Packit 6c4009
	          while (cp >= begin)
Packit 6c4009
		    {
Packit 6c4009
#ifdef USE_WIDE_CHAR
Packit 6c4009
		      if (*cp == thousands)
Packit 6c4009
			break;
Packit 6c4009
#else
Packit 6c4009
		      for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
Packit 6c4009
			if (thousands[cnt] != cp[thousands_len - cnt - 1])
Packit 6c4009
			  break;
Packit 6c4009
		      if (thousands[cnt] == '\0')
Packit 6c4009
			break;
Packit 6c4009
#endif
Packit 6c4009
		      --cp;
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
	          if (cp < begin)
Packit 6c4009
		    /* OK, only digits followed.  */
Packit 6c4009
		    return end;
Packit 6c4009
	        }
Packit 6c4009
	      else
Packit 6c4009
	        {
Packit 6c4009
		  /* Check the next group.  */
Packit 6c4009
	          const STRING_TYPE *group_end = cp;
Packit 6c4009
Packit 6c4009
		  while (cp >= begin)
Packit 6c4009
		    {
Packit 6c4009
#ifdef USE_WIDE_CHAR
Packit 6c4009
		      if (*cp == thousands)
Packit 6c4009
			break;
Packit 6c4009
#else
Packit 6c4009
		      for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
Packit 6c4009
			if (thousands[cnt] != cp[thousands_len - cnt - 1])
Packit 6c4009
			  break;
Packit 6c4009
		      if (thousands[cnt] == '\0')
Packit 6c4009
			break;
Packit 6c4009
#endif
Packit 6c4009
		      --cp;
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  if (cp < begin && group_end - cp <= (int) *gp)
Packit 6c4009
		    /* Final group is correct.  */
Packit 6c4009
		    return end;
Packit 6c4009
Packit 6c4009
		  if (cp < begin || group_end - cp != (int) *gp)
Packit 6c4009
		    /* Incorrect group.  Punt.  */
Packit 6c4009
		    break;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* The trailing portion of the string starting at NEW_END
Packit 6c4009
	     contains a grouping error.  So we will look for a correctly
Packit 6c4009
	     grouped number in the preceding portion instead.  */
Packit 6c4009
	  end = new_end;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Even the first group was wrong; determine maximum shift.  */
Packit 6c4009
	  if (end - cp > (int) *gp + 1)
Packit 6c4009
	    end = cp + (int) *gp + 1;
Packit 6c4009
	  else if (cp < begin)
Packit 6c4009
	    /* This number does not fill the first group, but is correct.  */
Packit 6c4009
	    return end;
Packit 6c4009
	  else
Packit 6c4009
	    /* CP points to a thousands separator character.  */
Packit 6c4009
	    end = cp;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return MAX (begin, end);
Packit 6c4009
}