diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2017-06-22 19:39:12 +0000 |
---|---|---|
committer | Sara Golemon <sara.golemon@mongodb.com> | 2017-06-29 14:48:26 -0400 |
commit | 35d65ae602e38afe7a4f0046ce3fb107ac38c0f0 (patch) | |
tree | 07d39b2e00b8a95b5bb5f2cb283226518ead9cc3 /src/mongo | |
parent | 2b5259a7a1a7b2a877ed2bb7045ddd559cd3283e (diff) | |
download | mongo-35d65ae602e38afe7a4f0046ce3fb107ac38c0f0.tar.gz |
SERVER-29856 Use StringData instead of std::string in CIDR/AddressRestriction<T>
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/db/auth/address_restriction.h | 4 | ||||
-rw-r--r-- | src/mongo/util/net/cidr.cpp | 14 | ||||
-rw-r--r-- | src/mongo/util/net/cidr.h | 5 |
3 files changed, 14 insertions, 9 deletions
diff --git a/src/mongo/db/auth/address_restriction.h b/src/mongo/db/auth/address_restriction.h index 4f96c4f21df..66b1dce1026 100644 --- a/src/mongo/db/auth/address_restriction.h +++ b/src/mongo/db/auth/address_restriction.h @@ -68,7 +68,7 @@ public: /** * Construct an AddressRestriction based on a human readable subnet spec */ - explicit AddressRestriction(const std::string& cidr) : _cidr(CIDR(cidr)) {} + explicit AddressRestriction(const StringData cidr) : _cidr(CIDR(cidr)) {} /** * If the given BSONElement represents a valid CIDR range, @@ -88,7 +88,7 @@ public: * constructs and returns the AddressRestriction. * Otherwise returns an error. */ - static StatusWith<AddressRestriction<T>> parse(const std::string& from) noexcept { + static StatusWith<AddressRestriction<T>> parse(StringData from) noexcept { auto cidr = CIDR::parse(from); if (cidr.isOK()) { return AddressRestriction<T>(std::move(cidr.getValue())); diff --git a/src/mongo/util/net/cidr.cpp b/src/mongo/util/net/cidr.cpp index dae25c52477..91eca7b7422 100644 --- a/src/mongo/util/net/cidr.cpp +++ b/src/mongo/util/net/cidr.cpp @@ -38,6 +38,10 @@ #include <netinet/in.h> #endif +using std::begin; +using std::find; +using std::end; + namespace mongo { namespace { @@ -87,18 +91,18 @@ StatusWith<CIDR> CIDR::parse(BSONElement from) noexcept { if (from.type() != String) { return {ErrorCodes::UnsupportedFormat, "CIDR range must be a string"}; } - return parse(from.valueStringData().toString()); + return parse(from.valueStringData()); } -StatusWith<CIDR> CIDR::parse(const std::string& s) noexcept try { +StatusWith<CIDR> CIDR::parse(StringData s) noexcept try { return CIDR(s); } catch (const CIDRException& e) { return {ErrorCodes::UnsupportedFormat, e.what()}; } -CIDR::CIDR(const std::string& s) try { - auto slash = std::find(begin(s), end(s), '/'); - auto ip = (slash == end(s)) ? s : s.substr(0, slash - begin(s)); +CIDR::CIDR(StringData s) try { + auto slash = find(begin(s), end(s), '/'); + auto ip = (slash == end(s)) ? s.toString() : s.substr(0, slash - begin(s)).toString(); if (inet_pton(AF_INET, ip.c_str(), _ip.data())) { _family = AF_INET; diff --git a/src/mongo/util/net/cidr.h b/src/mongo/util/net/cidr.h index baca930246f..4b21f307931 100644 --- a/src/mongo/util/net/cidr.h +++ b/src/mongo/util/net/cidr.h @@ -29,6 +29,7 @@ #pragma once #include "mongo/base/status_with.h" +#include "mongo/base/string_data.h" #include "mongo/bson/bsonelement.h" #include "mongo/bson/bsonmisc.h" @@ -51,7 +52,7 @@ public: */ class CIDR { public: - explicit CIDR(const std::string&); + explicit CIDR(StringData); /** * If the given BSONElement represents a valid CIDR range, @@ -65,7 +66,7 @@ public: * constructs and returns the CIDR. * Otherwise returns an error. */ - static StatusWith<CIDR> parse(const std::string& from) noexcept; + static StatusWith<CIDR> parse(StringData from) noexcept; /** * Returns true if the provided address range is contained |