diff options
Diffstat (limited to 'src/mongo/base')
-rw-r--r-- | src/mongo/base/string_data.h | 10 | ||||
-rw-r--r-- | src/mongo/base/string_data_test.cpp | 6 |
2 files changed, 14 insertions, 2 deletions
diff --git a/src/mongo/base/string_data.h b/src/mongo/base/string_data.h index ffb7941e7ac..4192ccd60f6 100644 --- a/src/mongo/base/string_data.h +++ b/src/mongo/base/string_data.h @@ -36,6 +36,8 @@ #include <stdexcept> #include <string> +#include <fmt/format.h> + #include "mongo/stdx/type_traits.h" #define MONGO_INCLUDE_INVARIANT_H_WHITELISTED #include "mongo/util/invariant.h" @@ -166,11 +168,11 @@ public: * null-terminated, so if using this without checking size(), you are likely doing * something wrong. */ - constexpr const char* rawData() const { + constexpr const char* rawData() const noexcept { return _data; } - constexpr size_t size() const { + constexpr size_t size() const noexcept { return _size; } constexpr bool empty() const { @@ -352,4 +354,8 @@ inline std::string operator+(StringData lhs, std::string rhs) { return rhs; } +constexpr fmt::string_view to_string_view(StringData s) noexcept { + return fmt::string_view(s.rawData(), s.size()); +} + } // namespace mongo diff --git a/src/mongo/base/string_data_test.cpp b/src/mongo/base/string_data_test.cpp index 3e19a557d8e..a1d9d7d7773 100644 --- a/src/mongo/base/string_data_test.cpp +++ b/src/mongo/base/string_data_test.cpp @@ -319,4 +319,10 @@ TEST(ConstIterator, StdReplaceCopy) { } } +TEST(StringDataFmt, Fmt) { + using namespace fmt::literals; + ASSERT_EQUALS(fmt::format("-{}-", "abc"_sd), "-abc-"); + ASSERT_EQUALS("-{}-"_format("abc"_sd), "-abc-"); +} + } // namespace mongo |