Implement P0403R1, Literal suffixes for basic_string_view.
authorVille Voutilainen <ville.voutilainen@gmail.com>
Sun, 13 Nov 2016 20:54:27 +0000 (22:54 +0200)
committerVille Voutilainen <ville@gcc.gnu.org>
Sun, 13 Nov 2016 20:54:27 +0000 (22:54 +0200)
* 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
libstdc++-v3/include/std/string_view
libstdc++-v3/testsuite/21_strings/basic_string_view/literals/types.cc [new file with mode: 0644]
libstdc++-v3/testsuite/21_strings/basic_string_view/literals/values.cc [new file with mode: 0644]
libstdc++-v3/testsuite/experimental/string_view/literals/values.cc

index c53f0ce10ff711d6312f2e0192bd62b838335c9c..3df60de099734a7750853ba74391b7c5e435a0c8 100644 (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.
index b2d2a296ba4e3e3e84f430434547f6e4cd0e4707..cf728dd83e97fc4b08678474e65222aaf11bb2f9 100644 (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
 
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 (file)
index 0000000..42e8b16
--- /dev/null
@@ -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 (file)
index 0000000..bbaa70e
--- /dev/null
@@ -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();
+}
index 19c64abdc1bfee31bb37f473f9c5a82af7aa76ba..b51fd4555ff196c4a8f299b3561866915c33f54f 100644 (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();
 }