diff mbox

[v3] Implement P0403R1, Literal suffixes for basic_string_view.

Message ID CAFk2RUbrw+_SNtFjrE+yKQ1951OrGW4dLO9LezcAFhc+dwoX4Q@mail.gmail.com
State New
Headers show

Commit Message

Ville Voutilainen Nov. 13, 2016, 8:41 p.m. UTC
Tested on Linux-x64.

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.

Comments

Jonathan Wakely Nov. 13, 2016, 8:46 p.m. UTC | #1
On 13/11/16 22:41 +0200, Ville Voutilainen wrote:
>    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.

OK for trunk, thanks.
diff mbox

Patch

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<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
 
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
+// <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");
+}
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
+// <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();
+}
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();
 }