Exemple #1
0
static int
compare_right(char const *a, char const *b)
{
     int bias = 0;
     int ca, cb;
     
     /* The longest run of digits wins.  That aside, the greatest
        value wins, but we can't know that it will until we've scanned
        both numbers to know that they have the same magnitude, so we
        remember it in BIAS. */
     for (;; a++, b++) {
      ca = to_int(*a);
      cb = to_int(*b);
          if (!nat_isdigit(ca)  &&  !nat_isdigit(cb))
               return bias;
          else if (!nat_isdigit(ca))
               return -1;
          else if (!nat_isdigit(cb))
               return +1;
          else if (ca < cb) {
               if (!bias)
                    bias = -1;
          } else if (ca > cb) {
               if (!bias)
                    bias = +1;
          } else if (!ca  &&  !cb)
               return bias;
     }

     return 0;
}
static S32
compare_right(const nat_char* a, const nat_char* b)
{
   S32 bias = 0;

   /* The longest run of digits wins.  That aside, the greatest
   value wins, but we can't know that it will until we've scanned
   both numbers to know that they have the same magnitude, so we
   remember it in BIAS. */
   for (;; a++, b++) {
      if (!nat_isdigit(*a)  &&  !nat_isdigit(*b))
         break;
      else if (!nat_isdigit(*a))
         return -1;
      else if (!nat_isdigit(*b))
         return +1;
      else if (*a < *b) {
         if (!bias)
            bias = -1;
      } else if (*a > *b) {
         if (!bias)
            bias = +1;
      } else if (!*a  &&  !*b)
         return bias;
   }

   return bias;
}
Exemple #3
0
// -----------------------------------------------------------------------------
int nat_cmprght(const nat_char* a, const nat_char* b)
{
	// compare two right-aligned numbers: the longest run of digits wins

	bool ad, bd;
	int  rc   = 9;
	int  bias = 0;

	// that aside, the greatest value wins, but it's not known which is greater
	// until both numbers are scanned to know if both magnitudes are the same,
	// so it's remembered in bias

	while( rc == 9 )
	{
		ad = nat_isdigit(*a);
		bd = nat_isdigit(*b);

		if(       !ad )       rc = !bd  ? bias : -1;
		else if(  !bd )       rc =               +1;
		else if(  *a <   *b ) if( !bias ) bias = -1;
		else if(  *a >   *b ) if( !bias ) bias = +1;
		else if( !*a && !*b ) rc = bias;

		if( rc == 9 ) { a++; b++; }
	}

     return rc;
}
Exemple #4
0
int strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)
{
	int ai, bi;
	nat_char ca, cb;
	int fractional, result;
     
	assert(a && b);
	ai = bi = 0;
	while (1) 
	{
		ca = a[ai]; cb = b[bi];

		/* skip over leading spaces or zeros */
		while (nat_isspace(ca))
			ca = a[++ai];

		while (nat_isspace(cb))
			cb = b[++bi];

		/* process run of digits */
		if (nat_isdigit(ca)  &&  nat_isdigit(cb)) 
		{
			fractional = (ca == '0' || cb == '0');

			if (fractional) 
			{
				if ((result = compare_left(a+ai, b+bi)) != 0)
					return result;
			} 
			else 
			{
				if ((result = compare_right(a+ai, b+bi)) != 0)
					return result;
			}
		}

		if (!ca && !cb) 
		{
			/* The strings compare the same.  Perhaps the caller
                  will want to call strcmp to break the tie. */
			return 0;
		}

		if (fold_case) 
		{
			ca = nat_toupper(ca);
			cb = nat_toupper(cb);
		}
	  
		if (ca < cb)
			return -1;
		else if (ca > cb)
			return +1;

		++ai; ++bi;
	}
}
Exemple #5
0
static int strnatcmp0(char const *a, char const *b, int fold_case)
{
     int ai, bi;
     int ca, cb;
     int fractional, result;
     
     assert(a && b);
     ai = bi = 0;
     while (1) {
      ca = to_int(a[ai]);
      cb = to_int(b[bi]);

          /* process run of digits */
          if (nat_isdigit(ca)  &&  nat_isdigit(cb)) {
               fractional = (ca == '0' || cb == '0');

               if (fractional) {
                    if ((result = compare_left(a+ai, b+bi)) != 0)
                         return result;
               } else {
                    if ((result = compare_right(a+ai, b+bi)) != 0)
                         return result;
               }
          }

          if (!ca && !cb) {
               /* The strings compare the same.  Call str[case]cmp() to ensure
                  consistent results. */
               if(fold_case)
                   return strcasecmp(a,b);
               else
                   return strcmp(a,b);
          }

          if (fold_case) {
               ca = nat_unify_case(ca);
               cb = nat_unify_case(cb);
          }
          
          if (ca < cb)
               return -1;
          else if (ca > cb)
               return +1;

          ++ai; ++bi;
     }
}
Exemple #6
0
// -----------------------------------------------------------------------------
int strnatcmp0(const nat_char* a, const nat_char* b, bool fold_case)
{
	nat_char ca, cb;
	int      rc = 0;
	bool     ok = a  && b;
	int      ai = 0, bi = 0;

	while( ok && rc == 0 )
	{
		while( nat_isspace(ca = a[ai]) ) ai++; // skip over leading spaces
		while( nat_isspace(cb = b[bi]) ) bi++; // skip over leading spaces

		if( nat_isdigit(ca) && nat_isdigit(cb) ) // process run of digits, if any
		{
			if( ca == '0' || cb == '0' ) // fractional?
			{
				if( (rc = nat_cmpleft(a+ai, b+bi)) != 0 )
					break;
			}
			else
			{
				if( (rc = nat_cmprght(a+ai, b+bi)) != 0 )
					break;
			}
		}

		if( !ca && !cb ) // do the strings compare the same?
			break;       // perhaps the caller will want to call strcmp to break the tie

		if( fold_case )
		{
			ca = nat_toupper(ca);
			cb = nat_toupper(cb);
		}

		if(      ca < cb ) rc = -1;
		else if( ca > cb ) rc = +1;
		else { ai++;  bi++; }
	}

	return rc;
}
static int
compare_left(const nat_char* a, const nat_char* b)
{
   /* Compare two left-aligned numbers: the first to have a
   different value wins. */
   for (;; a++, b++) {
      if (!nat_isdigit(*a)  &&  !nat_isdigit(*b))
         break;
      else if (!nat_isdigit(*a))
         return -1;
      else if (!nat_isdigit(*b))
         return +1;
      else if (*a < *b)
         return -1;
      else if (*a > *b)
         return +1;
   }

   return 0;
}
Exemple #8
0
static int
compare_left(nat_char const *a, nat_char const *b)
{
     /* Compare two left-aligned numbers: the first to have a
        different value wins. */
     for (;; a++, b++) {
	  if (!nat_isdigit(*a)  &&  !nat_isdigit(*b))
	       return 0;
	  else if (!nat_isdigit(*a))
	       return -1;
	  else if (!nat_isdigit(*b))
	       return +1;
	  else if (*a < *b)
	       return -1;
	  else if (*a > *b)
	       return +1;
     }
	  
     /* never reached: return 0; */
}
Exemple #9
0
// -----------------------------------------------------------------------------
int nat_cmpleft(const nat_char* a, const nat_char* b)
{
	// compare two left-aligned numbers: the first to have a different value wins

	bool ad, bd;
	int  rc = 9;

	while( rc == 9 )
	{
		ad = nat_isdigit(*a);
		bd = nat_isdigit(*b);

		if(      !ad )     rc = !bd ? 0 : -1;
		else if( !bd )     rc =           +1;
		else if( *a < *b ) rc =           -1;
		else if( *a > *b ) rc =           +1;
		else { a++; b++; }
	}

	return rc;
}