Commit 67e070fe authored by Victor Zverovich's avatar Victor Zverovich

Make format work with C++17 std::string_view (#571)

parent 64599973
...@@ -24,6 +24,12 @@ ...@@ -24,6 +24,12 @@
# define FMT_HAS_FEATURE(x) 0 # define FMT_HAS_FEATURE(x) 0
#endif #endif
#if defined(__has_include)
# define FMT_HAS_INCLUDE(x) __has_include(x)
#else
# define FMT_HAS_INCLUDE(x) 0
#endif
#ifdef __GNUC__ #ifdef __GNUC__
# define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) # define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
#else #else
...@@ -137,8 +143,15 @@ ...@@ -137,8 +143,15 @@
Type(const Type &) FMT_DELETED; \ Type(const Type &) FMT_DELETED; \
void operator=(const Type &) FMT_DELETED void operator=(const Type &) FMT_DELETED
#if (FMT_HAS_INCLUDE(<string_view>) && __cplusplus > 201402L) || \
(defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
# include <string_view>
namespace fmt { using std::basic_string_view; }
#elif (FMT_HAS_INCLUDE(<experimental/string_view>) && __cplusplus >= 201402L)
# include <experimental/string_view>
namespace fmt { using std::experimental::basic_string_view; }
#else
namespace fmt { namespace fmt {
/** /**
\rst \rst
An implementation of ``std::basic_string_view`` for pre-C++17. It provides a An implementation of ``std::basic_string_view`` for pre-C++17. It provides a
...@@ -180,15 +193,6 @@ class basic_string_view { ...@@ -180,15 +193,6 @@ class basic_string_view {
const std::basic_string<Char, Alloc> &s) FMT_NOEXCEPT const std::basic_string<Char, Alloc> &s) FMT_NOEXCEPT
: data_(s.c_str()), size_(s.size()) {} : data_(s.c_str()), size_(s.size()) {}
/**
\rst
Converts a string reference to an ``std::string`` object.
\endrst
*/
std::basic_string<Char> to_string() const {
return std::basic_string<Char>(data_, size_);
}
/** Returns a pointer to the string data. */ /** Returns a pointer to the string data. */
const Char *data() const { return data_; } const Char *data() const { return data_; }
...@@ -231,7 +235,10 @@ class basic_string_view { ...@@ -231,7 +235,10 @@ class basic_string_view {
return lhs.compare(rhs) >= 0; return lhs.compare(rhs) >= 0;
} }
}; };
} // namespace fmt
#endif
namespace fmt {
using string_view = basic_string_view<char>; using string_view = basic_string_view<char>;
using wstring_view = basic_string_view<wchar_t>; using wstring_view = basic_string_view<wchar_t>;
......
...@@ -137,11 +137,6 @@ TEST(StringViewTest, Ctor) { ...@@ -137,11 +137,6 @@ TEST(StringViewTest, Ctor) {
EXPECT_EQ(4u, string_view(std::string("defg")).size()); EXPECT_EQ(4u, string_view(std::string("defg")).size());
} }
TEST(StringViewTest, ConvertToString) {
std::string s = string_view("abc").to_string();
EXPECT_EQ("abc", s);
}
TEST(WriterTest, NotCopyConstructible) { TEST(WriterTest, NotCopyConstructible) {
EXPECT_FALSE(std::is_copy_constructible<fmt::writer>::value); EXPECT_FALSE(std::is_copy_constructible<fmt::writer>::value);
} }
......
...@@ -41,7 +41,7 @@ const unsigned BIG_NUM = INT_MAX + 1u; ...@@ -41,7 +41,7 @@ const unsigned BIG_NUM = INT_MAX + 1u;
// Makes format string argument positional. // Makes format string argument positional.
std::string make_positional(fmt::string_view format) { std::string make_positional(fmt::string_view format) {
std::string s(format.to_string()); std::string s(format.data(), format.size());
s.replace(s.find('%'), 1, "%1$"); s.replace(s.find('%'), 1, "%1$");
return s; return s;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment