Blame gl/strverscmp.c

Packit 549fdc
/* Compare strings while treating digits characters numerically.
Packit 549fdc
   Copyright (C) 1997-2016 Free Software Foundation, Inc.
Packit 549fdc
   This file is part of the GNU C Library.
Packit 549fdc
   Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
Packit 549fdc
Packit 549fdc
   The GNU C Library is free software; you can redistribute it and/or
Packit 549fdc
   modify it under the terms of the GNU Lesser General Public
Packit 549fdc
   License as published by the Free Software Foundation; either
Packit 549fdc
   version 2.1 of the License, or (at your option) any later version.
Packit 549fdc
Packit 549fdc
   The GNU C Library is distributed in the hope that it will be useful,
Packit 549fdc
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
   Lesser General Public License for more details.
Packit 549fdc
Packit 549fdc
   You should have received a copy of the GNU Lesser General Public
Packit 549fdc
   License along with the GNU C Library; if not, see
Packit 549fdc
   <http://www.gnu.org/licenses/>.  */
Packit 549fdc
Packit 549fdc
#if !_LIBC
Packit 549fdc
# include <config.h>
Packit 549fdc
#endif
Packit 549fdc
Packit 549fdc
#include <string.h>
Packit 549fdc
#include <ctype.h>
Packit 549fdc
Packit 549fdc
/* states: S_N: normal, S_I: comparing integral part, S_F: comparing
Packit 549fdc
           fractional parts, S_Z: idem but with leading Zeroes only */
Packit 549fdc
#define  S_N    0x0
Packit 549fdc
#define  S_I    0x3
Packit 549fdc
#define  S_F    0x6
Packit 549fdc
#define  S_Z    0x9
Packit 549fdc
Packit 549fdc
/* result_type: CMP: return diff; LEN: compare using len_diff/diff */
Packit 549fdc
#define  CMP    2
Packit 549fdc
#define  LEN    3
Packit 549fdc
Packit 549fdc
#ifndef weak_alias
Packit 549fdc
# define __strverscmp strverscmp
Packit 549fdc
#endif
Packit 549fdc
Packit 549fdc
/* Compare S1 and S2 as strings holding indices/version numbers,
Packit 549fdc
   returning less than, equal to or greater than zero if S1 is less than,
Packit 549fdc
   equal to or greater than S2 (for more info, see the texinfo doc).
Packit 549fdc
*/
Packit 549fdc
Packit 549fdc
int
Packit 549fdc
__strverscmp (const char *s1, const char *s2)
Packit 549fdc
{
Packit 549fdc
  const unsigned char *p1 = (const unsigned char *) s1;
Packit 549fdc
  const unsigned char *p2 = (const unsigned char *) s2;
Packit 549fdc
  unsigned char c1, c2;
Packit 549fdc
  int state;
Packit 549fdc
  int diff;
Packit 549fdc
Packit 549fdc
  /* Symbol(s)    0       [1-9]   others
Packit 549fdc
     Transition   (10) 0  (01) d  (00) x   */
Packit 549fdc
  static const unsigned char next_state[] =
Packit 549fdc
  {
Packit 549fdc
      /* state    x    d    0  */
Packit 549fdc
      /* S_N */  S_N, S_I, S_Z,
Packit 549fdc
      /* S_I */  S_N, S_I, S_I,
Packit 549fdc
      /* S_F */  S_N, S_F, S_F,
Packit 549fdc
      /* S_Z */  S_N, S_F, S_Z
Packit 549fdc
  };
Packit 549fdc
Packit 549fdc
  static const signed char result_type[] =
Packit 549fdc
  {
Packit 549fdc
      /* state   x/x  x/d  x/0  d/x  d/d  d/0  0/x  0/d  0/0  */
Packit 549fdc
Packit 549fdc
      /* S_N */  CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP,
Packit 549fdc
      /* S_I */  CMP, -1,  -1,  +1,  LEN, LEN, +1,  LEN, LEN,
Packit 549fdc
      /* S_F */  CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
Packit 549fdc
      /* S_Z */  CMP, +1,  +1,  -1,  CMP, CMP, -1,  CMP, CMP
Packit 549fdc
  };
Packit 549fdc
Packit 549fdc
  if (p1 == p2)
Packit 549fdc
    return 0;
Packit 549fdc
Packit 549fdc
  c1 = *p1++;
Packit 549fdc
  c2 = *p2++;
Packit 549fdc
  /* Hint: '0' is a digit too.  */
Packit 549fdc
  state = S_N + ((c1 == '0') + (isdigit (c1) != 0));
Packit 549fdc
Packit 549fdc
  while ((diff = c1 - c2) == 0)
Packit 549fdc
    {
Packit 549fdc
      if (c1 == '\0')
Packit 549fdc
        return diff;
Packit 549fdc
Packit 549fdc
      state = next_state[state];
Packit 549fdc
      c1 = *p1++;
Packit 549fdc
      c2 = *p2++;
Packit 549fdc
      state += (c1 == '0') + (isdigit (c1) != 0);
Packit 549fdc
    }
Packit 549fdc
Packit 549fdc
  state = result_type[state * 3 + (((c2 == '0') + (isdigit (c2) != 0)))];
Packit 549fdc
Packit 549fdc
  switch (state)
Packit 549fdc
    {
Packit 549fdc
    case CMP:
Packit 549fdc
      return diff;
Packit 549fdc
Packit 549fdc
    case LEN:
Packit 549fdc
      while (isdigit (*p1++))
Packit 549fdc
        if (!isdigit (*p2++))
Packit 549fdc
          return 1;
Packit 549fdc
Packit 549fdc
      return isdigit (*p2) ? -1 : diff;
Packit 549fdc
Packit 549fdc
    default:
Packit 549fdc
      return state;
Packit 549fdc
    }
Packit 549fdc
}
Packit 549fdc
#ifdef weak_alias
Packit 549fdc
libc_hidden_def (__strverscmp)
Packit 549fdc
weak_alias (__strverscmp, strverscmp)
Packit 549fdc
#endif