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

This commit is contained in:
Victor Zverovich 2018-02-07 07:37:05 -08:00
parent 64599973e9
commit 67e070fe5a
3 changed files with 18 additions and 16 deletions

View File

@ -24,6 +24,12 @@
# define FMT_HAS_FEATURE(x) 0
#endif
#if defined(__has_include)
# define FMT_HAS_INCLUDE(x) __has_include(x)
#else
# define FMT_HAS_INCLUDE(x) 0
#endif
#ifdef __GNUC__
# define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
#else
@ -137,8 +143,15 @@
Type(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 {
/**
\rst
An implementation of ``std::basic_string_view`` for pre-C++17. It provides a
@ -180,15 +193,6 @@ class basic_string_view {
const std::basic_string<Char, Alloc> &s) FMT_NOEXCEPT
: 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. */
const Char *data() const { return data_; }
@ -231,7 +235,10 @@ class basic_string_view {
return lhs.compare(rhs) >= 0;
}
};
} // namespace fmt
#endif
namespace fmt {
using string_view = basic_string_view<char>;
using wstring_view = basic_string_view<wchar_t>;

View File

@ -137,11 +137,6 @@ TEST(StringViewTest, Ctor) {
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) {
EXPECT_FALSE(std::is_copy_constructible<fmt::writer>::value);
}

View File

@ -41,7 +41,7 @@ const unsigned BIG_NUM = INT_MAX + 1u;
// Makes format string argument positional.
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$");
return s;
}