Exemplo n.º 1
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;
	}
}
Exemplo n.º 2
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;
}