summaryrefslogtreecommitdiff
path: root/bson
diff options
context:
space:
mode:
authordwight <dwight@10gen.com>2011-06-27 09:14:55 -0400
committerdwight <dwight@10gen.com>2011-06-27 09:14:55 -0400
commit4f87fbb17c2d24dd065c450f072669bd63856908 (patch)
treed6a6e6ebbdeec7f72273fa56ed670a63eeec9961 /bson
parent1056d38ce5e9a8be098d41e1e887c8dce3527231 (diff)
downloadmongo-4f87fbb17c2d24dd065c450f072669bd63856908.tar.gz
move bson method to the right place for libs and include only use of bson
Diffstat (limited to 'bson')
-rw-r--r--bson/bsonobjiterator.h23
-rw-r--r--bson/util/misc.h19
2 files changed, 42 insertions, 0 deletions
diff --git a/bson/bsonobjiterator.h b/bson/bsonobjiterator.h
index 371a209fcd3..69958cb6331 100644
--- a/bson/bsonobjiterator.h
+++ b/bson/bsonobjiterator.h
@@ -107,6 +107,29 @@ namespace mongo {
int _cur;
};
+ /** transform a BSON array into a vector of BSONElements.
+ we match array # positions with their vector position, and ignore
+ any fields with non-numeric field names.
+ */
+ inline vector<BSONElement> BSONElement::Array() const {
+ chk(mongo::Array);
+ vector<BSONElement> v;
+ BSONObjIterator i(Obj());
+ while( i.more() ) {
+ BSONElement e = i.next();
+ const char *f = e.fieldName();
+ try {
+ unsigned u = stringToNum(f);
+ assert( u < 1000000 );
+ if( u >= v.size() )
+ v.resize(u+1);
+ v[u] = e;
+ }
+ catch(unsigned) { }
+ }
+ return v;
+ }
+
/** Similar to BOOST_FOREACH
*
* because the iterator is defined outside of the for, you must use {} around
diff --git a/bson/util/misc.h b/bson/util/misc.h
index dafc965efc1..33764e380cd 100644
--- a/bson/util/misc.h
+++ b/bson/util/misc.h
@@ -91,4 +91,23 @@ namespace mongo {
return i;
return -1;
}
+
+ inline bool isNumber( char c ) {
+ return c >= '0' && c <= '9';
+ }
+
+ inline unsigned stringToNum(const char *str) {
+ unsigned x = 0;
+ const char *p = str;
+ while( 1 ) {
+ if( !isNumber(*p) ) {
+ if( *p == 0 && p != str )
+ break;
+ throw 0;
+ }
+ x = x * 10 + *p++ - '0';
+ }
+ return x;
+ }
+
}