summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2011-06-25 23:08:33 -0400
committerEliot Horowitz <eliot@10gen.com>2011-06-25 23:24:44 -0400
commitbef400c02b91ed83d1cb190511dcf6534c32857e (patch)
treeebe4ad07fc91b19d7c3306b3a746b63d4090453a
parenteac151bfd25215b454effc8ecfc47fe25f457672 (diff)
downloadmongo-bef400c02b91ed83d1cb190511dcf6534c32857e.tar.gz
move lexNumCmp to util/stringutils.h
-rw-r--r--db/jsobj.cpp1
-rw-r--r--db/ops/update.cpp5
-rw-r--r--db/ops/update.h4
-rw-r--r--dbtests/basictests.cpp1
-rw-r--r--util/goodies.h73
-rw-r--r--util/stringutils.h73
-rw-r--r--util/version.cpp1
7 files changed, 82 insertions, 76 deletions
diff --git a/db/jsobj.cpp b/db/jsobj.cpp
index 07627ba02da..78c52eb073f 100644
--- a/db/jsobj.cpp
+++ b/db/jsobj.cpp
@@ -27,6 +27,7 @@
#include <limits>
#include "../util/unittest.h"
#include "../util/embedded_builder.h"
+#include "../util/stringutils.h"
#include "json.h"
#include "jsobjmanipulator.h"
#include "../util/optime.h"
diff --git a/db/ops/update.cpp b/db/ops/update.cpp
index 9f385e7ce44..112a73ec9bd 100644
--- a/db/ops/update.cpp
+++ b/db/ops/update.cpp
@@ -23,6 +23,7 @@
#include "../queryoptimizer.h"
#include "../repl.h"
#include "../btree.h"
+#include "../../util/stringutils.h"
#include "update.h"
//#define DEBUGUPDATE(x) cout << x << endl;
@@ -743,6 +744,10 @@ namespace mongo {
return ss.str();
}
+ bool ModSetState::FieldCmp::operator()( const string &l, const string &r ) const {
+ return lexNumCmp( l.c_str(), r.c_str() ) < 0;
+ }
+
BSONObj ModSet::createNewFromQuery( const BSONObj& query ) {
BSONObj newObj;
diff --git a/db/ops/update.h b/db/ops/update.h
index 289613bb3f4..de5805ad382 100644
--- a/db/ops/update.h
+++ b/db/ops/update.h
@@ -542,9 +542,7 @@ namespace mongo {
*/
class ModSetState : boost::noncopyable {
struct FieldCmp {
- bool operator()( const string &l, const string &r ) const {
- return lexNumCmp( l.c_str(), r.c_str() ) < 0;
- }
+ bool operator()( const string &l, const string &r ) const;
};
typedef map<string,ModState,FieldCmp> ModStateHolder;
const BSONObj& _obj;
diff --git a/dbtests/basictests.cpp b/dbtests/basictests.cpp
index bae87fb67e0..97fb748249a 100644
--- a/dbtests/basictests.cpp
+++ b/dbtests/basictests.cpp
@@ -25,6 +25,7 @@
#include "../util/text.h"
#include "../util/queue.h"
#include "../util/paths.h"
+#include "../util/stringutils.h"
#include "../db/db.h"
namespace BasicTests {
diff --git a/util/goodies.h b/util/goodies.h
index 980f62496f8..d996f061b79 100644
--- a/util/goodies.h
+++ b/util/goodies.h
@@ -455,79 +455,6 @@ namespace mongo {
return x;
}
- // 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 == (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 ) {
- // 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 = (int)(e1-s1);
- int len2 = (int)(e2-s2);
-
- int result;
- // if one is longer than the other, return
- if ( len1 > len2 ) {
- return 1;
- }
- 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;
- }
-
- if ( n1 )
- return 1;
-
- if ( n2 )
- return -1;
-
- if ( *s1 > *s2 )
- return 1;
-
- if ( *s2 > *s1 )
- return -1;
-
- s1++; s2++;
- }
-
- if ( *s1 )
- return 1;
- if ( *s2 )
- return -1;
- return 0;
- }
-
/** A generic pointer type for function arguments.
* It will convert from any pointer type except auto_ptr.
* Semantics are the same as passing the pointer returned from get()
diff --git a/util/stringutils.h b/util/stringutils.h
index 7c01c6f0032..1dad4beb9b4 100644
--- a/util/stringutils.h
+++ b/util/stringutils.h
@@ -41,6 +41,79 @@ namespace mongo {
return string(copy);
}
+ // 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 == (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 ) {
+ // 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 = (int)(e1-s1);
+ int len2 = (int)(e2-s2);
+
+ int result;
+ // if one is longer than the other, return
+ if ( len1 > len2 ) {
+ return 1;
+ }
+ 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;
+ }
+
+ if ( n1 )
+ return 1;
+
+ if ( n2 )
+ return -1;
+
+ if ( *s1 > *s2 )
+ return 1;
+
+ if ( *s2 > *s1 )
+ return -1;
+
+ s1++; s2++;
+ }
+
+ if ( *s1 )
+ return 1;
+ if ( *s2 )
+ return -1;
+ return 0;
+ }
+
} // namespace mongo
#endif // UTIL_STRING_UTILS_HEADER
diff --git a/util/version.cpp b/util/version.cpp
index b0e8ec54561..f9c1471c88e 100644
--- a/util/version.cpp
+++ b/util/version.cpp
@@ -23,6 +23,7 @@
#include <string>
#include "unittest.h"
#include "version.h"
+#include "stringutils.h"
#include "../db/jsobj.h"
#include "file.h"