Commit 4d35f941 authored by Victor Zverovich's avatar Victor Zverovich

Always use fallback string_view to pass format string (#664)

parent 34cf54c2
...@@ -146,20 +146,23 @@ ...@@ -146,20 +146,23 @@
#if (FMT_HAS_INCLUDE(<string_view>) && __cplusplus > 201402L) || \ #if (FMT_HAS_INCLUDE(<string_view>) && __cplusplus > 201402L) || \
(defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910) (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
# include <string_view> # include <string_view>
namespace fmt { using std::basic_string_view; } # define FMT_USE_STD_STRING_VIEW
// std::experimental::basic_string_view::remove_prefix isn't constexpr until // std::experimental::basic_string_view::remove_prefix isn't constexpr until
// gcc 7.3. // gcc 7.3.
#elif (FMT_HAS_INCLUDE(<experimental/string_view>) && \ #elif (FMT_HAS_INCLUDE(<experimental/string_view>) && \
(FMT_GCC_VERSION == 0 || FMT_GCC_VERSION >= 730) && \ (FMT_GCC_VERSION == 0 || FMT_GCC_VERSION >= 730) && \
__cplusplus >= 201402L) __cplusplus >= 201402L)
# include <experimental/string_view> # include <experimental/string_view>
namespace fmt { using std::experimental::basic_string_view; } # define FMT_USE_EXPERIMENTAL_STRING_VIEW
#else #endif
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
subset of the API. subset of the API. fmt::basic_string_view is used for format strings even if
std::string_view is available to prevent issues when a library is compiled
with different -std option than the client code (which is not recommended).
\endrst \endrst
*/ */
template <typename Char> template <typename Char>
...@@ -172,6 +175,18 @@ class basic_string_view { ...@@ -172,6 +175,18 @@ class basic_string_view {
typedef Char char_type; typedef Char char_type;
typedef const Char *iterator; typedef const Char *iterator;
// Standard basic_string_view type.
#if defined(FMT_USE_STD_STRING_VIEW)
typedef std::basic_string_view<Char> type;
#elif defined(FMT_USE_EXPERIMENTAL_STRING_VIEW)
typedef std::experimental::basic_string_view<Char> type;
#else
struct type {
const char *data() const { return FMT_NULL; }
size_t size() const { return 0; };
};
#endif
FMT_CONSTEXPR basic_string_view() FMT_NOEXCEPT : data_(FMT_NULL), size_(0) {} FMT_CONSTEXPR basic_string_view() FMT_NOEXCEPT : data_(FMT_NULL), size_(0) {}
/** Constructs a string reference object from a C string and a size. */ /** Constructs a string reference object from a C string and a size. */
...@@ -197,6 +212,9 @@ class basic_string_view { ...@@ -197,6 +212,9 @@ 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()) {}
FMT_CONSTEXPR basic_string_view(type s) FMT_NOEXCEPT
: data_(s.data()), size_(s.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_; }
...@@ -239,10 +257,7 @@ class basic_string_view { ...@@ -239,10 +257,7 @@ class basic_string_view {
return lhs.compare(rhs) >= 0; return lhs.compare(rhs) >= 0;
} }
}; };
} // namespace fmt
#endif
namespace fmt {
typedef basic_string_view<char> string_view; typedef basic_string_view<char> string_view;
typedef basic_string_view<wchar_t> wstring_view; typedef basic_string_view<wchar_t> wstring_view;
...@@ -572,6 +587,9 @@ FMT_MAKE_VALUE(cstring_type, unsigned char*, const unsigned char*) ...@@ -572,6 +587,9 @@ FMT_MAKE_VALUE(cstring_type, unsigned char*, const unsigned char*)
FMT_MAKE_VALUE(cstring_type, const unsigned char*, const unsigned char*) FMT_MAKE_VALUE(cstring_type, const unsigned char*, const unsigned char*)
FMT_MAKE_VALUE(string_type, basic_string_view<typename C::char_type>, FMT_MAKE_VALUE(string_type, basic_string_view<typename C::char_type>,
basic_string_view<Char>) basic_string_view<Char>)
FMT_MAKE_VALUE(string_type,
typename basic_string_view<typename C::char_type>::type,
basic_string_view<Char>)
FMT_MAKE_VALUE(string_type, const std::basic_string<typename C::char_type>&, FMT_MAKE_VALUE(string_type, const std::basic_string<typename C::char_type>&,
basic_string_view<Char>) basic_string_view<Char>)
FMT_MAKE_VALUE(pointer_type, void*, const void*) FMT_MAKE_VALUE(pointer_type, void*, const void*)
......
...@@ -1213,6 +1213,12 @@ TEST(FormatterTest, FormatStringView) { ...@@ -1213,6 +1213,12 @@ TEST(FormatterTest, FormatStringView) {
EXPECT_EQ("test", format("{0}", string_view("test"))); EXPECT_EQ("test", format("{0}", string_view("test")));
} }
#ifdef FMT_USE_STD_STRING_VIEW
TEST(FormatterTest, FormatStringView) {
EXPECT_EQ("test", format("{0}", std::string_view("test")));
}
#endif
struct ConvertibleToString { struct ConvertibleToString {
std::string s; std::string s;
ConvertibleToString() : s("foo") {} ConvertibleToString() : s("foo") {}
......
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