diff options
Diffstat (limited to 'util/goodies.h')
-rw-r--r-- | util/goodies.h | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/util/goodies.h b/util/goodies.h index c43f356..7b73996 100644 --- a/util/goodies.h +++ b/util/goodies.h @@ -651,29 +651,50 @@ namespace mongo { // for convenience, '{' is greater than anything and stops number parsing inline int lexNumCmp( const char *s1, const char *s2 ) { + //cout << "START : " << s1 << "\t" << s2 << endl; while( *s1 && *s2 ) { - bool p1 = ( *s1 == '{' ); - bool p2 = ( *s2 == '{' ); + bool p1 = ( *s1 == (char)255 ); + bool p2 = ( *s2 == (char)255 ); + //cout << "\t\t " << p1 << "\t" << p2 << endl; if ( p1 && !p2 ) return 1; if ( p2 && !p1 ) return -1; - + bool n1 = isNumber( *s1 ); bool n2 = isNumber( *s2 ); if ( n1 && n2 ) { - char * e1; - char * e2; - long l1 = strtol( s1 , &e1 , 10 ); - long l2 = strtol( s2 , &e2 , 10 ); - - if ( l1 > l2 ) + // get rid of leading 0s + while ( *s1 == '0' ) s1++; + while ( *s2 == '0' ) s2++; + + char * e1 = (char*)s1; + char * e2 = (char*)s2; + + // find length + // if end of string, will break immediately ('\0') + while ( isNumber (*e1) ) e1++; + while ( isNumber (*e2) ) e2++; + + int len1 = e1-s1; + int len2 = e2-s2; + + int result; + // if one is longer than the other, return + if ( len1 > len2 ) { return 1; - else if ( l1 < l2 ) + } + else if ( len2 > len1 ) { return -1; - + } + // if the lengths are equal, just strcmp + else if ( (result = strncmp(s1, s2, len1)) != 0 ) { + return result; + } + + // otherwise, the numbers are equal s1 = e1; s2 = e2; continue; |