Implement P0403R1, Literal suffixes for basic_string_view.

* include/std/string_view
(operator""sv(const char*, size_t)): New.
(operator""sv(const wchar_t*, size_t)): Likewise.
(operator""sv(const char16_t*, size_t)): Likewise.
(operator""sv(const char32_t*, size_t)): Likewise.
* testsuite/21_strings/basic_string_view/literals/types.cc: New.
* testsuite/21_strings/basic_string_view/literals/values.cc: Likewise.
* testsuite/experimental/string_view/literals/values.cc: Add
tests for literals with embedded NULs.

From-SVN: r242367
This commit is contained in:
Ville Voutilainen 2016-11-13 22:54:27 +02:00 committed by Ville Voutilainen
parent 975672f357
commit 1701800580
5 changed files with 188 additions and 0 deletions

View File

@ -1,3 +1,16 @@
2016-11-13 Ville Voutilainen <ville.voutilainen@gmail.com>
Implement P0403R1, Literal suffixes for basic_string_view.
* include/std/string_view
(operator""sv(const char*, size_t)): New.
(operator""sv(const wchar_t*, size_t)): Likewise.
(operator""sv(const char16_t*, size_t)): Likewise.
(operator""sv(const char32_t*, size_t)): Likewise.
* testsuite/21_strings/basic_string_view/literals/types.cc: New.
* testsuite/21_strings/basic_string_view/literals/values.cc: Likewise.
* testsuite/experimental/string_view/literals/values.cc: Add
tests for literals with embedded NULs.
2016-11-12 Jonathan Wakely <jwakely@redhat.com>
* src/filesystem/ops.cc (is_empty): Fix typo in exception message.

View File

@ -640,6 +640,36 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
#endif
inline namespace literals
{
inline namespace string_view_literals
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
inline constexpr basic_string_view<char>
operator""sv(const char* __str, size_t __len)
{ return basic_string_view<char>{__str, __len}; }
#ifdef _GLIBCXX_USE_WCHAR_T
inline constexpr basic_string_view<wchar_t>
operator""sv(const wchar_t* __str, size_t __len)
{ return basic_string_view<wchar_t>{__str, __len}; }
#endif
#ifdef _GLIBCXX_USE_C99_STDINT_TR1
inline constexpr basic_string_view<char16_t>
operator""sv(const char16_t* __str, size_t __len)
{ return basic_string_view<char16_t>{__str, __len}; }
inline constexpr basic_string_view<char32_t>
operator""sv(const char32_t* __str, size_t __len)
{ return basic_string_view<char32_t>{__str, __len}; }
#endif
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace string_literals
} // namespace literals
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std

View File

@ -0,0 +1,45 @@
// { dg-options "-std=gnu++17" }
// { dg-do compile }
// Copyright (C) 2013-2016 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <string_view>
#include <type_traits>
void
test01()
{
using namespace std::literals::string_view_literals;
static_assert(std::is_same<decltype("Hello"sv), std::string_view>::value,
"\"Hello\"s is std::string_view");
static_assert(std::is_same<decltype(u8"Hello"sv), std::string_view>::value,
"u8\"Hello\"s is std::string_view");
#ifdef _GLIBCXX_USE_WCHAR_T
static_assert(std::is_same<decltype(L"Hello"sv), std::wstring_view>::value,
"L\"Hello\"s is std::wstring_view");
#endif
static_assert(std::is_same<decltype(u"Hello"sv), std::u16string_view>::value,
"u\"Hello\"s is std::u16string_view");
static_assert(std::is_same<decltype(U"Hello"sv), std::u32string_view>::value,
"U\"Hello\"s is std::u32string_view");
}

View File

@ -0,0 +1,72 @@
// { dg-options "-std=gnu++17" }
// Copyright (C) 2013-2016 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <string_view>
#include <testsuite_hooks.h>
void
test01()
{
using namespace std::literals::string_view_literals;
std::string_view planet = "Mercury"sv;
#ifdef _GLIBCXX_USE_WCHAR_T
std::wstring_view wplanet = L"Venus"sv;
#endif
std::string_view u8planet = u8"Mars"sv;
std::u16string_view u16planet = u"Jupiter"sv;
std::u32string_view u32planet = U"Saturn"sv;
VERIFY( planet == std::string_view("Mercury") );
#ifdef _GLIBCXX_USE_WCHAR_T
VERIFY( wplanet == std::wstring_view(L"Venus") );
#endif
VERIFY( u8planet == std::string_view(u8"Mars") );
VERIFY( u16planet == std::u16string_view(u"Jupiter") );
VERIFY( u32planet == std::u32string_view(U"Saturn") );
}
void
test02()
{
using namespace std::literals::string_view_literals;
std::string_view planet_cratered = "Mercury\0cratered"sv;
#ifdef _GLIBCXX_USE_WCHAR_T
std::wstring_view wplanet_cratered = L"Venus\0cratered"sv;
#endif
std::string_view u8planet_cratered = u8"Mars\0cratered"sv;
std::u16string_view u16planet_cratered = u"Jupiter\0cratered"sv;
std::u32string_view u32planet_cratered = U"Saturn\0cratered"sv;
VERIFY( planet_cratered == std::string_view("Mercury\0cratered", 16) );
#ifdef _GLIBCXX_USE_WCHAR_T
VERIFY( wplanet_cratered == std::wstring_view(L"Venus\0cratered", 14) );
#endif
VERIFY( u8planet_cratered == std::string_view(u8"Mars\0cratered", 13) );
VERIFY( u16planet_cratered == std::u16string_view(u"Jupiter\0cratered", 16) );
VERIFY( u32planet_cratered == std::u32string_view(U"Saturn\0cratered", 15) );
}
int
main()
{
test01();
test02();
}

View File

@ -42,8 +42,36 @@ test01()
VERIFY( u32planet == std::experimental::u32string_view(U"Saturn") );
}
void
test02()
{
using namespace std::experimental::literals::string_view_literals;
std::experimental::string_view planet_cratered = "Mercury\0cratered"sv;
#ifdef _GLIBCXX_USE_WCHAR_T
std::experimental::wstring_view wplanet_cratered = L"Venus\0cratered"sv;
#endif
std::experimental::string_view u8planet_cratered = u8"Mars\0cratered"sv;
std::experimental::u16string_view u16planet_cratered = u"Jupiter\0cratered"sv;
std::experimental::u32string_view u32planet_cratered = U"Saturn\0cratered"sv;
VERIFY( planet_cratered ==
std::experimental::string_view("Mercury\0cratered", 16) );
#ifdef _GLIBCXX_USE_WCHAR_T
VERIFY( wplanet_cratered ==
std::experimental::wstring_view(L"Venus\0cratered", 14) );
#endif
VERIFY( u8planet_cratered ==
std::experimental::string_view(u8"Mars\0cratered", 13) );
VERIFY( u16planet_cratered ==
std::experimental::u16string_view(u"Jupiter\0cratered", 16) );
VERIFY( u32planet_cratered ==
std::experimental::u32string_view(U"Saturn\0cratered", 15) );
}
int
main()
{
test01();
test02();
}