mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-13 22:50:05 +00:00
Added support for pre-c++17 experimental string_view (#607)
Added support for pre-c++17 experimental string_view
This commit is contained in:
parent
cabce31f45
commit
5705bf1c71
36
fmt/format.h
36
fmt/format.h
@ -55,8 +55,15 @@
|
||||
(defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
|
||||
# include <string_view>
|
||||
# define FMT_HAS_STRING_VIEW 1
|
||||
# define FMT_HAS_EXPERIMENTAL_STRING_VIEW 0
|
||||
#else
|
||||
# define FMT_HAS_STRING_VIEW 0
|
||||
# if (FMT_HAS_INCLUDE(<experimental/string_view>) && __cplusplus > 201402L)
|
||||
# include <experimental/string_view>
|
||||
# define FMT_HAS_EXPERIMENTAL_STRING_VIEW 1
|
||||
# else
|
||||
# define FMT_HAS_EXPERIMENTAL_STRING_VIEW 0
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined _SECURE_SCL && _SECURE_SCL
|
||||
@ -582,6 +589,26 @@ class BasicStringRef {
|
||||
}
|
||||
#endif
|
||||
|
||||
#if FMT_HAS_EXPERIMENTAL_STRING_VIEW
|
||||
/**
|
||||
\rst
|
||||
Constructs a string reference from a ``std::experimental::basic_string_view`` object.
|
||||
\endrst
|
||||
*/
|
||||
BasicStringRef(
|
||||
const std::experimental::basic_string_view<Char, std::char_traits<Char>> &s)
|
||||
: data_(s.data()), size_(s.size()) {}
|
||||
|
||||
/**
|
||||
\rst
|
||||
Converts a string reference to an ``std::string_view`` object.
|
||||
\endrst
|
||||
*/
|
||||
explicit operator std::experimental::basic_string_view<Char>() const FMT_NOEXCEPT {
|
||||
return std::experimental::basic_string_view<Char>(data_, size_);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
\rst
|
||||
Converts a string reference to an ``std::string`` object.
|
||||
@ -1367,6 +1394,9 @@ class MakeValue : public Arg {
|
||||
MakeValue(typename WCharHelper<const std::wstring &, Char>::Unsupported);
|
||||
#if FMT_HAS_STRING_VIEW
|
||||
MakeValue(typename WCharHelper<const std::wstring_view &, Char>::Unsupported);
|
||||
#endif
|
||||
#if FMT_HAS_EXPERIMENTAL_STRING_VIEW
|
||||
MakeValue(typename WCharHelper<const std::experimental::wstring_view &, Char>::Unsupported);
|
||||
#endif
|
||||
MakeValue(typename WCharHelper<WStringRef, Char>::Unsupported);
|
||||
|
||||
@ -1471,6 +1501,9 @@ class MakeValue : public Arg {
|
||||
FMT_MAKE_STR_VALUE(const std::string &, STRING)
|
||||
#if FMT_HAS_STRING_VIEW
|
||||
FMT_MAKE_STR_VALUE(const std::string_view &, STRING)
|
||||
#endif
|
||||
#if FMT_HAS_EXPERIMENTAL_STRING_VIEW
|
||||
FMT_MAKE_STR_VALUE(const std::experimental::string_view &, STRING)
|
||||
#endif
|
||||
FMT_MAKE_STR_VALUE(StringRef, STRING)
|
||||
FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str())
|
||||
@ -1486,6 +1519,9 @@ class MakeValue : public Arg {
|
||||
FMT_MAKE_WSTR_VALUE(const std::wstring &, WSTRING)
|
||||
#if FMT_HAS_STRING_VIEW
|
||||
FMT_MAKE_WSTR_VALUE(const std::wstring_view &, WSTRING)
|
||||
#endif
|
||||
#if FMT_HAS_EXPERIMENTAL_STRING_VIEW
|
||||
FMT_MAKE_WSTR_VALUE(const std::experimental::wstring_view &, WSTRING)
|
||||
#endif
|
||||
FMT_MAKE_WSTR_VALUE(WStringRef, WSTRING)
|
||||
|
||||
|
@ -11,6 +11,12 @@ target_compile_options(gmock PUBLIC ${CPP11_FLAG})
|
||||
target_compile_definitions(gmock PUBLIC GTEST_HAS_STD_WSTRING=1)
|
||||
target_include_directories(gmock PUBLIC .)
|
||||
|
||||
# Workaround for Cygwin to make google-tests compile and run because the macro
|
||||
# _POSIX_C_SOURCE must be defined to allow fileno(), strdup(), fdopen() calls.
|
||||
if (CYGWIN)
|
||||
target_compile_definitions(gmock PUBLIC _POSIX_C_SOURCE=200809)
|
||||
endif ()
|
||||
|
||||
find_package(Threads)
|
||||
if (Threads_FOUND)
|
||||
target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
@ -156,6 +156,11 @@ TEST(StringRefTest, Ctor) {
|
||||
EXPECT_STREQ("hijk", StringRef(std::string_view("hijk")).data());
|
||||
EXPECT_EQ(4u, StringRef(std::string_view("hijk")).size());
|
||||
#endif
|
||||
|
||||
#if FMT_HAS_EXPERIMENTAL_STRING_VIEW
|
||||
EXPECT_STREQ("hijk", StringRef(std::experimental::string_view("hijk")).data());
|
||||
EXPECT_EQ(4u, StringRef(std::experimental::string_view("hijk")).size());
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(StringRefTest, ConvertToString) {
|
||||
@ -167,6 +172,12 @@ TEST(StringRefTest, ConvertToString) {
|
||||
std::string_view sv = static_cast<std::string_view>(str_ref);
|
||||
EXPECT_EQ("defg", sv);
|
||||
#endif
|
||||
|
||||
#if FMT_HAS_EXPERIMENTAL_STRING_VIEW
|
||||
StringRef str_ref("defg");
|
||||
std::experimental::string_view sv = static_cast<std::experimental::string_view>(str_ref);
|
||||
EXPECT_EQ("defg", sv);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(CStringRefTest, Ctor) {
|
||||
@ -176,6 +187,10 @@ TEST(CStringRefTest, Ctor) {
|
||||
#if FMT_HAS_STRING_VIEW
|
||||
EXPECT_STREQ("hijk", CStringRef(std::string_view("hijk")).c_str());
|
||||
#endif
|
||||
|
||||
#if FMT_HAS_EXPERIMENTAL_STRING_VIEW
|
||||
EXPECT_STREQ("hijk", CStringRef(std::experimental::string_view("hijk")).c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
#if FMT_USE_TYPE_TRAITS
|
||||
@ -1399,6 +1414,12 @@ TEST(FormatterTest, FormatStringView) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#if FMT_HAS_EXPERIMENTAL_STRING_VIEW
|
||||
TEST(FormatterTest, FormatExperimentalStringView) {
|
||||
EXPECT_EQ("test", format("{0}", std::experimental::string_view("test")));
|
||||
}
|
||||
#endif
|
||||
|
||||
void format_arg(fmt::BasicFormatter<char> &f, const char *, const Date &d) {
|
||||
f.writer() << d.year() << '-' << d.month() << '-' << d.day();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user