FieldCompareResult compareDottedFieldNames( const string& l , const string& r ){ size_t lstart = 0; size_t rstart = 0; while ( 1 ){ if ( lstart >= l.size() ){ if ( rstart >= r.size() ) return SAME; return RIGHT_SUBFIELD; } if ( rstart >= r.size() ) return LEFT_SUBFIELD; size_t a = l.find( '.' , lstart ); size_t b = r.find( '.' , rstart ); size_t lend = a == string::npos ? l.size() : a; size_t rend = b == string::npos ? r.size() : b; const string& c = l.substr( lstart , lend - lstart ); const string& d = r.substr( rstart , rend - rstart ); int x = lexNumCmp( c.c_str(), d.c_str() ); if ( x < 0 ) return LEFT_BEFORE; if ( x > 0 ) return RIGHT_BEFORE; lstart = lend + 1; rstart = rend + 1; } }
int versionCmp(StringData rhs, StringData lhs) { if (strcmp(rhs.data(),lhs.data()) == 0) return 0; // handle "1.2.3-" and "1.2.3-pre" if (rhs.size() < lhs.size()) { if (strncmp(rhs.data(), lhs.data(), rhs.size()) == 0 && lhs.data()[rhs.size()] == '-') return +1; } else if (rhs.size() > lhs.size()) { if (strncmp(rhs.data(), lhs.data(), lhs.size()) == 0 && rhs.data()[lhs.size()] == '-') return -1; } return lexNumCmp(rhs.data(), lhs.data()); }
void run() { ASSERT( ! isNumber( (char)255 ) ); ASSERT_EQUALS( 0, lexNumCmp( "a", "a" ) ); ASSERT_EQUALS( -1, lexNumCmp( "a", "aa" ) ); ASSERT_EQUALS( 1, lexNumCmp( "aa", "a" ) ); ASSERT_EQUALS( -1, lexNumCmp( "a", "b" ) ); ASSERT_EQUALS( 1, lexNumCmp( "100", "50" ) ); ASSERT_EQUALS( -1, lexNumCmp( "50", "100" ) ); ASSERT_EQUALS( 1, lexNumCmp( "b", "a" ) ); ASSERT_EQUALS( 0, lexNumCmp( "aa", "aa" ) ); ASSERT_EQUALS( -1, lexNumCmp( "aa", "ab" ) ); ASSERT_EQUALS( 1, lexNumCmp( "ab", "aa" ) ); ASSERT_EQUALS( 1, lexNumCmp( "0", "a" ) ); ASSERT_EQUALS( 1, lexNumCmp( "a0", "aa" ) ); ASSERT_EQUALS( -1, lexNumCmp( "a", "0" ) ); ASSERT_EQUALS( -1, lexNumCmp( "aa", "a0" ) ); ASSERT_EQUALS( 0, lexNumCmp( "0", "0" ) ); ASSERT_EQUALS( 0, lexNumCmp( "10", "10" ) ); ASSERT_EQUALS( -1, lexNumCmp( "1", "10" ) ); ASSERT_EQUALS( 1, lexNumCmp( "10", "1" ) ); ASSERT_EQUALS( 1, lexNumCmp( "11", "10" ) ); ASSERT_EQUALS( -1, lexNumCmp( "10", "11" ) ); ASSERT_EQUALS( 1, lexNumCmp( "f11f", "f10f" ) ); ASSERT_EQUALS( -1, lexNumCmp( "f10f", "f11f" ) ); ASSERT_EQUALS( -1, lexNumCmp( "f11f", "f111" ) ); ASSERT_EQUALS( 1, lexNumCmp( "f111", "f11f" ) ); ASSERT_EQUALS( -1, lexNumCmp( "f12f", "f12g" ) ); ASSERT_EQUALS( 1, lexNumCmp( "f12g", "f12f" ) ); ASSERT_EQUALS( 1, lexNumCmp( "aa{", "aab" ) ); ASSERT_EQUALS( -1, lexNumCmp( "aa{", "aa1" ) ); ASSERT_EQUALS( -1, lexNumCmp( "a1{", "a11" ) ); ASSERT_EQUALS( 1, lexNumCmp( "a1{a", "a1{" ) ); ASSERT_EQUALS( -1, lexNumCmp( "a1{", "a1{a" ) ); ASSERT_EQUALS( 1, lexNumCmp("21", "11") ); ASSERT_EQUALS( -1, lexNumCmp("11", "21") ); ASSERT_EQUALS( -1 , lexNumCmp( "a.0" , "a.1" ) ); ASSERT_EQUALS( -1 , lexNumCmp( "a.0.b" , "a.1" ) ); ASSERT_EQUALS( -1 , lexNumCmp( "b." , "b.|" ) ); ASSERT_EQUALS( -1 , lexNumCmp( "b.0e" , (string("b.") + (char)255).c_str() ) ); ASSERT_EQUALS( -1 , lexNumCmp( "b." , "b.0e" ) ); ASSERT_EQUALS( 0, lexNumCmp( "238947219478347782934718234", "238947219478347782934718234")); ASSERT_EQUALS( 0, lexNumCmp( "000238947219478347782934718234", "238947219478347782934718234")); ASSERT_EQUALS( 1, lexNumCmp( "000238947219478347782934718235", "238947219478347782934718234")); ASSERT_EQUALS( -1, lexNumCmp( "238947219478347782934718234", "238947219478347782934718234.1")); ASSERT_EQUALS( 0, lexNumCmp( "238", "000238")); ASSERT_EQUALS( 0, lexNumCmp( "002384", "0002384")); ASSERT_EQUALS( 0, lexNumCmp( "00002384", "0002384")); ASSERT_EQUALS( 0, lexNumCmp( "0", "0")); ASSERT_EQUALS( 0, lexNumCmp( "0000", "0")); ASSERT_EQUALS( 0, lexNumCmp( "0", "000")); ASSERT_EQUALS( -1, lexNumCmp( "0000", "0.0")); ASSERT_EQUALS( 1, lexNumCmp( "2380", "238")); ASSERT_EQUALS( 1, lexNumCmp( "2385", "2384")); ASSERT_EQUALS( 1, lexNumCmp( "2385", "02384")); ASSERT_EQUALS( 1, lexNumCmp( "2385", "002384")); ASSERT_EQUALS( -1, lexNumCmp( "123.234.4567", "00238")); ASSERT_EQUALS( 0, lexNumCmp( "123.234", "00123.234")); ASSERT_EQUALS( 0, lexNumCmp( "a.123.b", "a.00123.b")); ASSERT_EQUALS( 1, lexNumCmp( "a.123.b", "a.b.00123.b")); ASSERT_EQUALS( -1, lexNumCmp( "a.00.0", "a.0.1")); ASSERT_EQUALS( 0, lexNumCmp( "01.003.02", "1.3.2")); ASSERT_EQUALS( -1, lexNumCmp( "1.3.2", "10.300.20")); ASSERT_EQUALS( 0, lexNumCmp( "10.300.20", "000000000000010.0000300.000000020")); ASSERT_EQUALS( 0, lexNumCmp( "0000a", "0a")); ASSERT_EQUALS( -1, lexNumCmp( "a", "0a")); ASSERT_EQUALS( -1, lexNumCmp( "000a", "001a")); ASSERT_EQUALS( 0, lexNumCmp( "010a", "0010a")); }
int BSONElementFieldSorter( const void * a , const void * b ){ const char * x = *((const char**)a); const char * y = *((const char**)b); x++; y++; return lexNumCmp( x , y ); }
void run() { ASSERT_EQUALS( 0, lexNumCmp( "a", "a" ) ); ASSERT_EQUALS( -1, lexNumCmp( "a", "aa" ) ); ASSERT_EQUALS( 1, lexNumCmp( "aa", "a" ) ); ASSERT_EQUALS( -1, lexNumCmp( "a", "b" ) ); ASSERT_EQUALS( 1, lexNumCmp( "100", "50" ) ); ASSERT_EQUALS( -1, lexNumCmp( "50", "100" ) ); ASSERT_EQUALS( 1, lexNumCmp( "b", "a" ) ); ASSERT_EQUALS( 0, lexNumCmp( "aa", "aa" ) ); ASSERT_EQUALS( -1, lexNumCmp( "aa", "ab" ) ); ASSERT_EQUALS( 1, lexNumCmp( "ab", "aa" ) ); ASSERT_EQUALS( 1, lexNumCmp( "0", "a" ) ); ASSERT_EQUALS( 1, lexNumCmp( "a0", "aa" ) ); ASSERT_EQUALS( -1, lexNumCmp( "a", "0" ) ); ASSERT_EQUALS( -1, lexNumCmp( "aa", "a0" ) ); ASSERT_EQUALS( 0, lexNumCmp( "0", "0" ) ); ASSERT_EQUALS( 0, lexNumCmp( "10", "10" ) ); ASSERT_EQUALS( -1, lexNumCmp( "1", "10" ) ); ASSERT_EQUALS( 1, lexNumCmp( "10", "1" ) ); ASSERT_EQUALS( 1, lexNumCmp( "11", "10" ) ); ASSERT_EQUALS( -1, lexNumCmp( "10", "11" ) ); ASSERT_EQUALS( 1, lexNumCmp( "f11f", "f10f" ) ); ASSERT_EQUALS( -1, lexNumCmp( "f10f", "f11f" ) ); ASSERT_EQUALS( -1, lexNumCmp( "f11f", "f111" ) ); ASSERT_EQUALS( 1, lexNumCmp( "f111", "f11f" ) ); ASSERT_EQUALS( -1, lexNumCmp( "f12f", "f12g" ) ); ASSERT_EQUALS( 1, lexNumCmp( "f12g", "f12f" ) ); ASSERT_EQUALS( 1, lexNumCmp( "aa{", "aab" ) ); ASSERT_EQUALS( 1, lexNumCmp( "aa{", "aa1" ) ); ASSERT_EQUALS( -1, lexNumCmp( "a1{", "a11" ) ); ASSERT_EQUALS( 1, lexNumCmp( "a1{a", "a1{" ) ); ASSERT_EQUALS( -1, lexNumCmp( "a1{", "a1{a" ) ); ASSERT_EQUALS( 1, lexNumCmp("21", "11") ); ASSERT_EQUALS( -1, lexNumCmp("11", "21") ); ASSERT_EQUALS( -1 , lexNumCmp( "a.0" , "a.1" ) ); ASSERT_EQUALS( -1 , lexNumCmp( "a.0.b" , "a.1" ) ); }