Index: include/string_view =================================================================== --- include/string_view +++ include/string_view @@ -746,6 +746,39 @@ return __do_string_hash(__val.data(), __val.data() + __val.size()); } +#if _LIBCPP_STD_VER > 14 +// Literal suffixes for basic_string_view [basic.string_view.literals] +inline namespace literals +{ + inline namespace string_view_literals + { + inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR + string_view operator "" sv(const char* __str, size_t __len) _NOEXCEPT + { + return string_view(__str, __len); + } + + inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR + u16string_view operator "" sv(const char16_t* __str, size_t __len) _NOEXCEPT + { + return u16string_view(__str, __len); + } + + inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR + u32string_view operator "" sv(const char32_t* __str, size_t __len) _NOEXCEPT + { + return u32string_view(__str, __len); + } + + inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR + wstring_view operator "" sv(const wchar_t* __str, size_t __len) _NOEXCEPT + { + return wstring_view(__str, __len); + } + } // namespace string_view_literals +} // namespace literals +#endif + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_STRING_VIEW Index: test/std/strings/string.view/string.view.literals/literal.pass.cpp =================================================================== --- /dev/null +++ test/std/strings/string.view/string.view.literals/literal.pass.cpp @@ -0,0 +1,45 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +#include +#include +#include + +int main() +{ + using namespace std::literals::string_view_literals; + + static_assert ( std::is_same::value, "" ); + static_assert ( std::is_same::value, "" ); + static_assert ( std::is_same::value, "" ); + static_assert ( std::is_same::value, "" ); + + std::string_view foo; + std::wstring_view Lfoo; + std::u16string_view ufoo; + std::u32string_view Ufoo; + + foo = ""sv; assert( foo.size() == 0); + Lfoo = L""sv; assert(Lfoo.size() == 0); + ufoo = u""sv; assert(ufoo.size() == 0); + Ufoo = U""sv; assert(Ufoo.size() == 0); + + foo = " "sv; assert( foo.size() == 1); + Lfoo = L" "sv; assert(Lfoo.size() == 1); + ufoo = u" "sv; assert(ufoo.size() == 1); + Ufoo = U" "sv; assert(Ufoo.size() == 1); + + foo = "ABC"sv; assert( foo == "ABC"); assert( foo == std::string ( "ABC")); + Lfoo = L"ABC"sv; assert(Lfoo == L"ABC"); assert(Lfoo == std::wstring ( L"ABC")); + ufoo = u"ABC"sv; assert(ufoo == u"ABC"); assert(ufoo == std::u16string( u"ABC")); + Ufoo = U"ABC"sv; assert(Ufoo == U"ABC"); assert(Ufoo == std::u32string( U"ABC")); +} Index: test/std/strings/string.view/string.view.literals/literal1.fail.cpp =================================================================== --- /dev/null +++ test/std/strings/string.view/string.view.literals/literal1.fail.cpp @@ -0,0 +1,21 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +#include +#include + +int main() +{ + using std::string_view; + + string_view foo = ""sv; // should fail w/conversion operator not found +} Index: test/std/strings/string.view/string.view.literals/literal1.pass.cpp =================================================================== --- /dev/null +++ test/std/strings/string.view/string.view.literals/literal1.pass.cpp @@ -0,0 +1,21 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +#include +#include + +int main() +{ + using namespace std::literals; + + std::string_view foo = ""sv; +} Index: test/std/strings/string.view/string.view.literals/literal2.fail.cpp =================================================================== --- /dev/null +++ test/std/strings/string.view/string.view.literals/literal2.fail.cpp @@ -0,0 +1,19 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +#include +#include + +int main() +{ + std::string_view foo = ""sv; // should fail w/conversion operator not found +} Index: test/std/strings/string.view/string.view.literals/literal2.pass.cpp =================================================================== --- /dev/null +++ test/std/strings/string.view/string.view.literals/literal2.pass.cpp @@ -0,0 +1,21 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +#include +#include + +int main() +{ + using namespace std::literals::string_view_literals; + + std::string_view foo = ""sv; +} Index: test/std/strings/string.view/string.view.literals/literal3.pass.cpp =================================================================== --- /dev/null +++ test/std/strings/string.view/string.view.literals/literal3.pass.cpp @@ -0,0 +1,21 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +#include +#include + +int main() +{ + using namespace std; + + string_view foo = ""sv; +}