summaryrefslogtreecommitdiff
path: root/util/goodies.h
diff options
context:
space:
mode:
Diffstat (limited to 'util/goodies.h')
-rw-r--r--util/goodies.h29
1 files changed, 26 insertions, 3 deletions
diff --git a/util/goodies.h b/util/goodies.h
index 53a74c200bd..3f843b737a3 100644
--- a/util/goodies.h
+++ b/util/goodies.h
@@ -486,11 +486,30 @@ namespace mongo {
return x;
}
- // for convenience, '{' is greater than anything and stops number parsing
+ /**
+ * Non numeric characters are compared lexicographically; numeric substrings
+ * are compared numerically; dots separate ordered comparable subunits.
+ * For convenience, character 255 is greater than anything else.
+ */
inline int lexNumCmp( const char *s1, const char *s2 ) {
//cout << "START : " << s1 << "\t" << s2 << endl;
+
+ bool startWord = true;
+
while( *s1 && *s2 ) {
+ bool d1 = ( *s1 == '.' );
+ bool d2 = ( *s2 == '.' );
+ if ( d1 && !d2 )
+ return -1;
+ if ( d2 && !d1 )
+ return 1;
+ if ( d1 && d2 ) {
+ ++s1; ++s2;
+ startWord = true;
+ continue;
+ }
+
bool p1 = ( *s1 == (char)255 );
bool p2 = ( *s2 == (char)255 );
//cout << "\t\t " << p1 << "\t" << p2 << endl;
@@ -504,8 +523,10 @@ namespace mongo {
if ( n1 && n2 ) {
// get rid of leading 0s
- while ( *s1 == '0' ) s1++;
- while ( *s2 == '0' ) s2++;
+ if ( startWord ) {
+ while ( *s1 == '0' ) s1++;
+ while ( *s2 == '0' ) s2++;
+ }
char * e1 = (char*)s1;
char * e2 = (char*)s2;
@@ -534,6 +555,7 @@ namespace mongo {
// otherwise, the numbers are equal
s1 = e1;
s2 = e2;
+ startWord = false;
continue;
}
@@ -550,6 +572,7 @@ namespace mongo {
return -1;
s1++; s2++;
+ startWord = false;
}
if ( *s1 )