summaryrefslogtreecommitdiff
path: root/src/mongo/bson
diff options
context:
space:
mode:
authorBilly Donahue <billy.donahue@mongodb.com>2019-04-03 18:16:29 -0400
committerBilly Donahue <billy.donahue@mongodb.com>2019-04-08 12:15:08 -0400
commit573921791c4d20e0d1603fd0a200d90d74b65bd5 (patch)
tree9ec3957126fecc636cb31874376f09b6161965ff /src/mongo/bson
parentf8f872e029ba3b1f32d8499c912756d48dc1a03b (diff)
downloadmongo-573921791c4d20e0d1603fd0a200d90d74b65bd5.tar.gz
SERVER-40476 remove mongoutils::str::equals
Diffstat (limited to 'src/mongo/bson')
-rw-r--r--src/mongo/bson/bsonelement.h17
-rw-r--r--src/mongo/bson/bsonobj.cpp16
-rw-r--r--src/mongo/bson/bsonobj.h4
3 files changed, 22 insertions, 15 deletions
diff --git a/src/mongo/bson/bsonelement.h b/src/mongo/bson/bsonelement.h
index 09ecf8b9d4b..8ccbd99936c 100644
--- a/src/mongo/bson/bsonelement.h
+++ b/src/mongo/bson/bsonelement.h
@@ -384,9 +384,9 @@ public:
return type() == jstNULL;
}
- /** Size (length) of a std::string element.
- You must assure of type std::string first.
- @return std::string size including terminating null
+ /** Size of a BSON String element.
+ Requires that type() == mongo::String.
+ @return String size including its null-termination.
*/
int valuestrsize() const {
return ConstDataView(value()).read<LittleEndian<int>>();
@@ -404,14 +404,17 @@ public:
return value() + 4;
}
- /** Get the std::string value of the element. If not a std::string returns "". */
+ /** Like valuestr, but returns a valid empty string if `type() != mongo::String`. */
const char* valuestrsafe() const {
return type() == mongo::String ? valuestr() : "";
}
- /** Get the std::string value of the element. If not a std::string returns "". */
+ /** Like valuestrsafe, but returns StringData. */
+ StringData valueStringDataSafe() const {
+ return type() == mongo::String ? StringData(valuestr(), valuestrsize() - 1) : StringData();
+ }
+ /** Like valuestrsafe, but returns std::string. */
std::string str() const {
- return type() == mongo::String ? std::string(valuestr(), valuestrsize() - 1)
- : std::string();
+ return valueStringDataSafe().toString();
}
/**
diff --git a/src/mongo/bson/bsonobj.cpp b/src/mongo/bson/bsonobj.cpp
index 29d07a5965b..376e32016ee 100644
--- a/src/mongo/bson/bsonobj.cpp
+++ b/src/mongo/bson/bsonobj.cpp
@@ -217,7 +217,7 @@ bool BSONObj::isFieldNamePrefixOf(const BSONObj& otherObj) const {
while (a.more() && b.more()) {
BSONElement x = a.next();
BSONElement y = b.next();
- if (!str::equals(x.fieldName(), y.fieldName())) {
+ if (x.fieldNameStringData() != y.fieldNameStringData()) {
return false;
}
}
@@ -382,26 +382,26 @@ Status BSONObj::storageValidEmbedded() const {
bool first = true;
while (i.more()) {
BSONElement e = i.next();
- const char* name = e.fieldName();
+ StringData name = e.fieldNameStringData();
// Cannot start with "$", unless dbref which must start with ($ref, $id)
- if (str::startsWith(name, '$')) {
+ if (name.startsWith("$")) {
if (first &&
// $ref is a collection name and must be a String
- str::equals(name, "$ref") &&
- e.type() == String && str::equals(i.next().fieldName(), "$id")) {
+ (name == "$ref") &&
+ e.type() == String && (i.next().fieldNameStringData() == "$id")) {
first = false;
// keep inspecting fields for optional "$db"
e = i.next();
- name = e.fieldName(); // "" if eoo()
+ name = e.fieldNameStringData(); // "" if eoo()
// optional $db field must be a String
- if (str::equals(name, "$db") && e.type() == String) {
+ if ((name == "$db") && e.type() == String) {
continue; // this element is fine, so continue on to siblings (if any more)
}
// Can't start with a "$", all other checks are done below (outside if blocks)
- if (str::startsWith(name, '$')) {
+ if (name.startsWith("$")) {
return Status(ErrorCodes::DollarPrefixedFieldName,
str::stream() << name << " is not valid for storage.");
}
diff --git a/src/mongo/bson/bsonobj.h b/src/mongo/bson/bsonobj.h
index 336d84eb022..69e85b2117d 100644
--- a/src/mongo/bson/bsonobj.h
+++ b/src/mongo/bson/bsonobj.h
@@ -502,6 +502,10 @@ public:
return *p == EOO ? "" : p + 1;
}
+ StringData firstElementFieldNameStringData() const {
+ return StringData(firstElementFieldName());
+ }
+
BSONType firstElementType() const {
const char* p = objdata() + 4;
return (BSONType)*p;