Back to index

glibc  2.9
Defines | Functions
strverscmp.c File Reference
#include <string.h>
#include <ctype.h>

Go to the source code of this file.

Defines

#define S_N   0x0
#define S_I   0x4
#define S_F   0x8
#define S_Z   0xC
#define CMP   2
#define LEN   3

Functions

int __strverscmp (char *s1, const char *s2) const

Define Documentation

#define CMP   2

Definition at line 32 of file strverscmp.c.

#define LEN   3

Definition at line 33 of file strverscmp.c.

#define S_F   0x8

Definition at line 28 of file strverscmp.c.

#define S_I   0x4

Definition at line 27 of file strverscmp.c.

#define S_N   0x0

Definition at line 26 of file strverscmp.c.

#define S_Z   0xC

Definition at line 29 of file strverscmp.c.


Function Documentation

int __strverscmp ( char *  s1,
const char *  s2 
) const

Definition at line 42 of file strverscmp.c.

{
  const unsigned char *p1 = (const unsigned char *) s1;
  const unsigned char *p2 = (const unsigned char *) s2;
  unsigned char c1, c2;
  int state;
  int diff;

  /* Symbol(s)    0       [1-9]   others  (padding)
     Transition   (10) 0  (01) d  (00) x  (11) -   */
  static const unsigned int next_state[] =
  {
      /* state    x    d    0    - */
      /* S_N */  S_N, S_I, S_Z, S_N,
      /* S_I */  S_N, S_I, S_I, S_I,
      /* S_F */  S_N, S_F, S_F, S_F,
      /* S_Z */  S_N, S_F, S_Z, S_Z
  };

  static const int result_type[] =
  {
      /* state   x/x  x/d  x/0  x/-  d/x  d/d  d/0  d/-
                 0/x  0/d  0/0  0/-  -/x  -/d  -/0  -/- */

      /* S_N */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
                 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
      /* S_I */  CMP, -1,  -1,  CMP, +1,  LEN, LEN, CMP,
                 +1,  LEN, LEN, CMP, CMP, CMP, CMP, CMP,
      /* S_F */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
                 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
      /* S_Z */  CMP, +1,  +1,  CMP, -1,  CMP, CMP, CMP,
                 -1,  CMP, CMP, CMP
  };

  if (p1 == p2)
    return 0;

  c1 = *p1++;
  c2 = *p2++;
  /* Hint: '0' is a digit too.  */
  state = S_N | ((c1 == '0') + (isdigit (c1) != 0));

  while ((diff = c1 - c2) == 0 && c1 != '\0')
    {
      state = next_state[state];
      c1 = *p1++;
      c2 = *p2++;
      state |= (c1 == '0') + (isdigit (c1) != 0);
    }

  state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];

  switch (state)
  {
    case CMP:
      return diff;

    case LEN:
      while (isdigit (*p1++))
       if (!isdigit (*p2++))
         return 1;

      return isdigit (*p2) ? -1 : diff;

    default:
      return state;
  }
}