Sort efficiently



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

{

for (; *s1; ++s1, ++s2) //No need to check s2, as we compare it to s1, and it won't equal if it's null

{

if (isdigit(*s1) && isdigit(*s2)) //If they're both digits, use this special handling

{

while (*s1 == '0' ) { ++s1; } while (*s2 == '0' ) { ++s2; } //First skip leading zeros



register bool d1 = isdigit(*s1), d2 = isdigit(*s2); //Are we still in a run of digits for both strings?

if (d1 != d2) { return (d1-d2); } //If for only one of them, return that as the difference



for (; d1 && (*s1 == *s2); ++s2) { d1 = isdigit(*++s1); } //Keep going while we have matching digits



d2 = isdigit(*s2); //If we broke the above loop because a single string ran out of digits

if (d1 != d2) { return (d1-d2); } //Return that as the difference



for ( const char *p1 = s1, *p2 = s2; d1; ) //Otherwise, difference in the digits themselves, clarify magnitude

{

d1 = isdigit(*++p1); d2 = isdigit(*++p2); //First string to run out of digits first

if (d1 != d2) { return (d1-d2); } //Will lose right here

} //If loop breaks, both strings are out of digits, the difference found above will be handled below

}

if (NOT_EQUAL(*s1, *s2)) { break ; } //In all other cases, fall through with difference in current position - if any

}

return (*s1-*s2);

}



