diff options
author | Greg Studer <greg@10gen.com> | 2014-09-19 13:30:00 -0400 |
---|---|---|
committer | Greg Studer <greg@10gen.com> | 2014-10-16 18:38:12 -0400 |
commit | 22f8b6259602a76f8d22cba8b1098f9e3c90a36f (patch) | |
tree | 7cf4bd3d8b5439c4e42ca1be7ee6161a69af73a2 /src/mongo/db/field_ref.cpp | |
parent | 02c1c52514c7d6b54ff2d6dd6a3c564c3543f0a5 (diff) | |
download | mongo-22f8b6259602a76f8d22cba8b1098f9e3c90a36f.tar.gz |
SERVER-14973 consolidate shard key parsing, cleanup shard key patterns
Diffstat (limited to 'src/mongo/db/field_ref.cpp')
-rw-r--r-- | src/mongo/db/field_ref.cpp | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/src/mongo/db/field_ref.cpp b/src/mongo/db/field_ref.cpp index b6ad89c5938..840a62cdc7b 100644 --- a/src/mongo/db/field_ref.cpp +++ b/src/mongo/db/field_ref.cpp @@ -198,25 +198,36 @@ namespace mongo { } StringData FieldRef::dottedField( size_t offset ) const { - if (_size == 0 || offset >= numParts() ) + return dottedSubstring(offset, numParts()); + } + + StringData FieldRef::dottedSubstring(size_t startPart, size_t endPart) const { + if (_size == 0 || startPart >= endPart || endPart > numParts()) return StringData(); if (!_replacements.empty()) reserialize(); dassert(_replacements.empty()); - // Assume we want the whole thing StringData result(_dotted); - // Strip off any leading parts we were asked to ignore - for (size_t i = 0; i < offset; ++i) { - const StringData part = getPart(i); - result = StringData( - result.rawData() + part.size() + 1, - result.size() - part.size() - 1); + // Fast-path if we want the whole thing + if (startPart == 0 && endPart == numParts()) + return result; + + size_t startChar = 0; + for (size_t i = 0; i < startPart; ++i) { + startChar += getPart(i).size() + 1; // correct for '.' + } + size_t endChar = startChar; + for (size_t i = startPart; i < endPart; ++i) { + endChar += getPart(i).size() + 1; } + // correct for last '.' + if (endPart != numParts()) + --endChar; - return result; + return result.substr(startChar, endChar - startChar); } bool FieldRef::equalsDottedField( const StringData& other ) const { |