From 1701800580852987a23d00c45c3e0d1c30b095da Mon Sep 17 00:00:00 2001 From: Ville Voutilainen Date: Sun, 13 Nov 2016 22:54:27 +0200 Subject: [PATCH] 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 --- libstdc++-v3/ChangeLog | 13 ++++ libstdc++-v3/include/std/string_view | 30 +++++++++ .../21_strings/basic_string_view/literals/types.cc | 45 ++++++++++++++ .../basic_string_view/literals/values.cc | 72 ++++++++++++++++++++++ .../experimental/string_view/literals/values.cc | 28 +++++++++ 5 files changed, 188 insertions(+) create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string_view/literals/types.cc create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string_view/literals/values.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index c53f0ce..3df60de 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,16 @@ +2016-11-13 Ville Voutilainen + + 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 * src/filesystem/ops.cc (is_empty): Fix typo in exception message. diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index b2d2a29..cf728dd 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -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 + operator""sv(const char* __str, size_t __len) + { return basic_string_view{__str, __len}; } + +#ifdef _GLIBCXX_USE_WCHAR_T + inline constexpr basic_string_view + operator""sv(const wchar_t* __str, size_t __len) + { return basic_string_view{__str, __len}; } +#endif + +#ifdef _GLIBCXX_USE_C99_STDINT_TR1 + inline constexpr basic_string_view + operator""sv(const char16_t* __str, size_t __len) + { return basic_string_view{__str, __len}; } + + inline constexpr basic_string_view + operator""sv(const char32_t* __str, size_t __len) + { return basic_string_view{__str, __len}; } +#endif + + _GLIBCXX_END_NAMESPACE_VERSION + } // namespace string_literals + } // namespace literals + _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/literals/types.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/literals/types.cc new file mode 100644 index 0000000..42e8b16 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/literals/types.cc @@ -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 +// . + +#include +#include + +void +test01() +{ + using namespace std::literals::string_view_literals; + + static_assert(std::is_same::value, + "\"Hello\"s is std::string_view"); + + static_assert(std::is_same::value, + "u8\"Hello\"s is std::string_view"); + +#ifdef _GLIBCXX_USE_WCHAR_T + static_assert(std::is_same::value, + "L\"Hello\"s is std::wstring_view"); +#endif + + static_assert(std::is_same::value, + "u\"Hello\"s is std::u16string_view"); + + static_assert(std::is_same::value, + "U\"Hello\"s is std::u32string_view"); +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/literals/values.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/literals/values.cc new file mode 100644 index 0000000..bbaa70e --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/literals/values.cc @@ -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 +// . + +#include +#include + +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(); +} diff --git a/libstdc++-v3/testsuite/experimental/string_view/literals/values.cc b/libstdc++-v3/testsuite/experimental/string_view/literals/values.cc index 19c64ab..b51fd45 100644 --- a/libstdc++-v3/testsuite/experimental/string_view/literals/values.cc +++ b/libstdc++-v3/testsuite/experimental/string_view/literals/values.cc @@ -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(); } -- 2.7.4