Last active
April 28, 2016 16:08
-
-
Save BenGoldberg1/4a91e9b7c106736c0ffaa2c350bbd75c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int goldberg_compare( const void *ap, const void bp ) { | |
const char **aa = ap, **bb = bp; | |
const char *a = *aa, *b = *bb; | |
do { | |
int achars, bchars; | |
long aval, bval; | |
int shorter, cmp; | |
while( isspace(*a) ) ++a; | |
while( isspace(*b) ) ++b; | |
achars = strcspn( a, "0123456789" ); | |
bchars = strcspn( b, "0123456789" ); | |
shorter = min( achars, bchars ); | |
cmp = memcmp( a, b, shorter ); | |
if( cmp ) return cmp; | |
if( achars != bchars ) return bchars - achars; | |
a += achars; | |
b += bchars; | |
if( !*a || !*b ) return (!!*b) - (!!*a); | |
aval = strtol( a, &a, 10 ); | |
bval = strtol( b, &b, 10 ); | |
if( aval != bval ) return bval - aval; | |
} while(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment