summaryrefslogtreecommitdiff
path: root/src/mongo/base/string_data.h
diff options
context:
space:
mode:
authorBilly Donahue <billy.donahue@mongodb.com>2020-03-25 13:12:06 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-03-25 20:20:45 +0000
commitba96c6924affb1923d4cffa728003469e1aebc4b (patch)
tree62e3a8431eaff9d937a43272ee348955d0cf4346 /src/mongo/base/string_data.h
parent81885344c49450f1618d056543ff35a05440324e (diff)
downloadmongo-ba96c6924affb1923d4cffa728003469e1aebc4b.tar.gz
SERVER-46503 inline namespace mongo::literals
Diffstat (limited to 'src/mongo/base/string_data.h')
-rw-r--r--src/mongo/base/string_data.h29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/mongo/base/string_data.h b/src/mongo/base/string_data.h
index 3a943348680..d551b36d416 100644
--- a/src/mongo/base/string_data.h
+++ b/src/mongo/base/string_data.h
@@ -59,6 +59,7 @@ namespace mongo {
* rawData() terminates with a null.
*/
class StringData {
+ /** Tag used to bypass the invariant of the {c,len} constructor. */
struct TrustedInitTag {};
constexpr StringData(const char* c, size_t len, TrustedInitTag) : _data(c), _size(len) {}
@@ -88,20 +89,19 @@ public:
/**
* Constructs a StringData with an explicit length. 'c' must
- * either be NULL (in which case len must be zero), or be a
+ * either be nullptr (in which case len must be zero), or be a
* pointer into a character array. The StringData will refer to
* the first 'len' characters starting at 'c'. The range of
- * characters c to c+len must be valid.
+ * characters in the half-open interval `[c, c + len)` must be valid.
*/
StringData(const char* c, size_t len) : StringData(c, len, TrustedInitTag()) {
invariant(_data || (_size == 0));
}
- /**
- * Constructs a StringData from a user defined literal. This allows
- * for constexpr creation of StringData's that are known at compile time.
- */
- constexpr friend StringData operator"" _sd(const char* c, std::size_t len);
+ /** Helper for the `literals::operator""_sd` function below. Conceptually non-public. */
+ static constexpr StringData _literalHelper(const char* c, std::size_t len) {
+ return StringData(c, len, TrustedInitTag{});
+ }
explicit operator std::string() const {
return toString();
@@ -226,10 +226,6 @@ inline bool operator>=(StringData lhs, StringData rhs) {
std::ostream& operator<<(std::ostream& stream, StringData value);
-constexpr StringData operator"" _sd(const char* c, std::size_t len) {
- return StringData(c, len, StringData::TrustedInitTag{});
-}
-
inline int StringData::compare(StringData other) const {
// It is illegal to pass nullptr to memcmp. It is an invariant of
// StringData that if _data is nullptr, _size is zero. If asked to
@@ -358,4 +354,15 @@ constexpr fmt::string_view to_string_view(StringData s) noexcept {
return fmt::string_view(s.rawData(), s.size());
}
+inline namespace literals {
+
+/**
+ * Makes a constexpr StringData from a user defined literal (e.g. "hello"_sd).
+ * This allows for constexpr creation of `StringData` that are known at compile time.
+ */
+constexpr StringData operator"" _sd(const char* c, std::size_t len) {
+ return StringData::_literalHelper(c, len);
+}
+} // namespace literals
+
} // namespace mongo