diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 0a07cabb438d..e885dca7d2c7 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -430,6 +430,7 @@ SUBDIR_UNITTESTS_SRCS = \
 	unittests/scoped_fd-selftests.c \
 	unittests/scoped_mmap-selftests.c \
 	unittests/scoped_restore-selftests.c \
+	unittests/string_view-selftests.c \
 	unittests/tracepoint-selftests.c \
 	unittests/unpack-selftests.c \
 	unittests/utils-selftests.c \
diff --git a/gdb/unittests/basic_string_view/capacity/1.cc b/gdb/unittests/basic_string_view/capacity/1.cc
index d49ecbd9dd53..f297853f0b7a 100644
--- a/gdb/unittests/basic_string_view/capacity/1.cc
+++ b/gdb/unittests/basic_string_view/capacity/1.cc
@@ -19,9 +19,7 @@
 
 // string_view size, length
 
-#include <string_view>
-#include <cstring>
-#include <testsuite_hooks.h>
+namespace capacity_1 {
 
 template<typename T>
   struct A { };
@@ -36,13 +34,19 @@ template<typename T>
 
 struct B { };
 
+} // namespace capacity_1
+} // namespace string_view
+} // namespace selftests
+
 // char_traits specialization
 namespace std
 {
   template<>
-    struct char_traits<A<B> >
+    struct char_traits<selftests::string_view::capacity_1::A<
+	selftests::string_view::capacity_1::B> >
     {
-      typedef A<B> 		char_type;
+      typedef selftests::string_view::capacity_1::A<
+	  selftests::string_view::capacity_1::B> char_type;
       // Unsigned as wint_t in unsigned.
       typedef unsigned long  	int_type;
       typedef streampos 	pos_type;
@@ -123,11 +127,15 @@ namespace std
     };
 } // namespace std
 
+namespace selftests {
+namespace string_view {
+namespace capacity_1 {
+
 void
 test01()
 {
-  std::basic_string_view<A<B>> str02;
-  typedef std::basic_string_view< A<B> >::size_type size_type_o;
+  gdb::basic_string_view<A<B>> str02;
+  typedef gdb::basic_string_view< A<B> >::size_type size_type_o;
   size_type_o sz03;
   size_type_o sz04;
 
@@ -160,3 +168,5 @@ main()
 
   return 0;
 }
+
+} // namespace capacity_1
diff --git a/gdb/unittests/basic_string_view/cons/char/1.cc b/gdb/unittests/basic_string_view/cons/char/1.cc
index f80ae92f2f80..cfe9cce4b55a 100644
--- a/gdb/unittests/basic_string_view/cons/char/1.cc
+++ b/gdb/unittests/basic_string_view/cons/char/1.cc
@@ -19,43 +19,40 @@
 
 // basic_string_view constructors.
 
-#include <string_view>
-#include <string>
-#include <cstring>
-#include <testsuite_hooks.h>
+namespace cons_1 {
 
 void
 test01()
 {
-  typedef std::string_view::size_type csize_type;
+  typedef gdb::string_view::size_type csize_type;
 
   // basic_string_view()
-  const std::string_view str00{};
+  const gdb::string_view str00{};
   VERIFY( str00.length() == 0 );
   VERIFY( str00.data() == nullptr );
 
   // basic_string_view(const char*)
   const char str_lit01[] = "rodeo beach, marin";
-  const std::string_view str01{str_lit01};
+  const gdb::string_view str01{str_lit01};
   VERIFY( str01.length() == 18 );
   VERIFY( str01.data() == str_lit01 );
-  const std::string_view str02{"baker beach, san francisco"};
+  const gdb::string_view str02{"baker beach, san francisco"};
   VERIFY( str02.length() == 26 );
 
   // basic_string_view(const string_view&)
-  std::string_view str04{str01};
+  gdb::string_view str04{str01};
   VERIFY( str04.length() == str01.length() );
   VERIFY( str04.data() == str01.data() );
 
   // basic_string_view(const char* s)
   csize_type len_lit01 = strlen(str_lit01);
-  std::string_view str05{str_lit01, len_lit01};
+  gdb::string_view str05{str_lit01, len_lit01};
   VERIFY( str05.length() == len_lit01 );
   VERIFY( str05.data() == str_lit01 );
 
   // basic_string_view(basic_string& s)
   std::string istr07(10, 'z');
-  std::string_view str07{istr07};
+  gdb::string_view str07{istr07};
   VERIFY( str07.length() == 10 );
 }
 
@@ -66,3 +63,5 @@ main()
 
   return 0;
 }
+
+} // namespace cons_1
diff --git a/gdb/unittests/basic_string_view/cons/char/2.cc b/gdb/unittests/basic_string_view/cons/char/2.cc
index 8dbca7a621e7..a1b45367e009 100644
--- a/gdb/unittests/basic_string_view/cons/char/2.cc
+++ b/gdb/unittests/basic_string_view/cons/char/2.cc
@@ -19,10 +19,7 @@
 
 // basic_string_view constructors.
 
-#include <new>
-#include <string_view>
-#include <stdexcept>
-#include <testsuite_hooks.h>
+namespace cons_2 {
 
 void
 test03()
@@ -32,9 +29,9 @@ test03()
   // These are tests to see how basic_string_view handles data with NUL
   // bytes.  Obviously basic_string_view(char*) will halt at the first one, but
   // nothing else should.
-  std::string_view s1(with_nulls, 28);
+  gdb::string_view s1(with_nulls, 28);
   VERIFY( s1.size() == 28 );
-  std::string_view s2(s1);
+  gdb::string_view s2(s1);
   VERIFY( s2.size() == 28 );
 }
 
@@ -45,3 +42,5 @@ main()
 
   return 0;
 }
+
+} // namespace cons_2
diff --git a/gdb/unittests/basic_string_view/cons/char/3.cc b/gdb/unittests/basic_string_view/cons/char/3.cc
index c892cbc62720..358c118508e8 100644
--- a/gdb/unittests/basic_string_view/cons/char/3.cc
+++ b/gdb/unittests/basic_string_view/cons/char/3.cc
@@ -19,15 +19,13 @@
 
 // basic_string_view constructors.
 
-#include <string_view>
-#include <vector>
-#include <testsuite_hooks.h>
+namespace cons_3 {
 
 void
 test05()
 {
   char const * s = 0;
-  std::string_view zero_length_built_with_NULL(s, 0);
+  gdb::string_view zero_length_built_with_NULL(s, 0);
 }
 
 int
@@ -37,3 +35,5 @@ main()
 
   return 0;
 }
+
+} // namespace cons_3
diff --git a/gdb/unittests/basic_string_view/element_access/char/1.cc b/gdb/unittests/basic_string_view/element_access/char/1.cc
index 03c588efb0d7..4a6cff9cfa02 100644
--- a/gdb/unittests/basic_string_view/element_access/char/1.cc
+++ b/gdb/unittests/basic_string_view/element_access/char/1.cc
@@ -19,21 +19,19 @@
 
 // basic_string element access
 
-#include <string_view>
-#include <stdexcept>
-#include <testsuite_hooks.h>
+namespace element_access_1 {
 
 void
 test01()
 {
-  typedef std::string_view::size_type csize_type;
-  typedef std::string_view::const_reference cref;
-  typedef std::string_view::reference ref;
+  typedef gdb::string_view::size_type csize_type;
+  typedef gdb::string_view::const_reference cref;
+  typedef gdb::string_view::reference ref;
   csize_type csz01, csz02;
 
-  const std::string_view str01("tamarindo, costa rica");
-  std::string_view str02("41st street beach, capitola, california");
-  std::string_view str03;
+  const gdb::string_view str01("tamarindo, costa rica");
+  gdb::string_view str02("41st street beach, capitola, california");
+  gdb::string_view str03;
 
   // const_reference operator[] (size_type pos) const;
   csz01 = str01.size();
@@ -47,19 +45,20 @@ test01()
   csz01 = str01.size();
   cref cref3 = str01.at(csz01 - 1);
   VERIFY( cref3 == 'a' );
-  try
+
+  bool threw = false;
+  TRY
   {
     str01.at(csz01);
     VERIFY( false ); // Should not get here, as exception thrown.
   }
-  catch (std::out_of_range& fail)
-  {
-    VERIFY( true );
-  }
-  catch (...)
+  CATCH (ex, RETURN_MASK_ALL)
   {
-    VERIFY( false );
+    threw = true;
   }
+  END_CATCH
+
+  VERIFY ( threw );
 }
 
 int
@@ -68,3 +67,5 @@ main()
   test01();
   return 0;
 }
+
+} // namespace element_access_1
diff --git a/gdb/unittests/basic_string_view/element_access/char/empty.cc b/gdb/unittests/basic_string_view/element_access/char/empty.cc
index fad5eb798ba6..97e85d675f26 100644
--- a/gdb/unittests/basic_string_view/element_access/char/empty.cc
+++ b/gdb/unittests/basic_string_view/element_access/char/empty.cc
@@ -18,21 +18,22 @@
 // <http://www.gnu.org/licenses/>.
 //
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace element_access_empty {
 
 int
 main()
 {
   {
-    std::string_view empty;
+    gdb::string_view empty;
     VERIFY( empty.empty() );
   }
 
   {
-    const std::string_view empty;
+    const gdb::string_view empty;
     VERIFY( empty.empty() );
   }
 
   return 0;
 }
+
+} // namespace element_access_empty
diff --git a/gdb/unittests/basic_string_view/element_access/char/front_back.cc b/gdb/unittests/basic_string_view/element_access/char/front_back.cc
index efff787e365e..450364726cfa 100644
--- a/gdb/unittests/basic_string_view/element_access/char/front_back.cc
+++ b/gdb/unittests/basic_string_view/element_access/char/front_back.cc
@@ -18,14 +18,13 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace element_access_front_back {
 
 void
 test01()
 {
-  std::string_view str("ramifications");
-  const std::string_view cstr("melodien");
+  gdb::string_view str("ramifications");
+  const gdb::string_view cstr("melodien");
 
   VERIFY( str.front() == 'r' );
   VERIFY( str.back() == 's' );
@@ -40,3 +39,5 @@ main()
 
   return 0;
 }
+
+} // namespace element_access_front_back
diff --git a/gdb/unittests/basic_string_view/inserters/char/1.cc b/gdb/unittests/basic_string_view/inserters/char/1.cc
index 26bd0fdc918d..d7edbaffabde 100644
--- a/gdb/unittests/basic_string_view/inserters/char/1.cc
+++ b/gdb/unittests/basic_string_view/inserters/char/1.cc
@@ -23,21 +23,16 @@
 // working, not to mention other major details like char_traits, and
 // all of the string_view class.
 
-#include <string_view>
-#include <stdexcept>
-#include <sstream>
-#include <fstream>
-#include <iostream>
-#include <testsuite_hooks.h>
+namespace inserters_1 {
 
 void
 test01()
 {
-  typedef std::string_view::size_type csize_type;
-  typedef std::string_view::const_reference cref;
-  typedef std::string_view::reference ref;
+  typedef gdb::string_view::size_type csize_type;
+  typedef gdb::string_view::const_reference cref;
+  typedef gdb::string_view::reference ref;
 
-  const std::string_view str01("sailing grand traverse bay\n"
+  const gdb::string_view str01("sailing grand traverse bay\n"
 	       "\t\t\t    from Elk Rapids to the point reminds me of miles");
     
   // ostream& operator<<(ostream&, const basic_string_view&)
@@ -52,7 +47,7 @@ test01()
       VERIFY( false );
     }
   
-  std::string_view hello_world;
+  gdb::string_view hello_world;
   std::cout << hello_world;
 }
 
@@ -63,3 +58,5 @@ main()
 
   return 0;
 }
+
+} // namespace inserters_1
diff --git a/gdb/unittests/basic_string_view/inserters/char/2.cc b/gdb/unittests/basic_string_view/inserters/char/2.cc
index 6562d58615df..64b82de66a49 100644
--- a/gdb/unittests/basic_string_view/inserters/char/2.cc
+++ b/gdb/unittests/basic_string_view/inserters/char/2.cc
@@ -25,11 +25,7 @@
 // { dg-options "-std=gnu++17" }
 // { dg-require-fileio "" }
 
-#include <string_view>
-#include <string>
-#include <fstream>
-#include <iostream>
-#include <testsuite_hooks.h>
+namespace inserters_2 {
 
 // testing basic_filebuf::xsputn via stress testing with large string_views
 // based on a bug report libstdc++ 9
@@ -37,13 +33,13 @@
 void
 test05(std::size_t size)
 {
-  bool test = true;
+  bool test ATTRIBUTE_UNUSED = true;
 
   const char filename[] = "inserters_extractors-2.txt";
   const char fillc = 'f';
   std::ofstream ofs(filename);
   std::string str(size, fillc);
-  std::string_view strv{str};
+  gdb::string_view strv{str};
 
   // sanity checks
   VERIFY( str.size() == size );
@@ -91,3 +87,5 @@ main()
 
   return 0;
 }
+
+} // namespace inserters_2
diff --git a/gdb/unittests/basic_string_view/inserters/char/3.cc b/gdb/unittests/basic_string_view/inserters/char/3.cc
index 7905661396da..bd086c50451e 100644
--- a/gdb/unittests/basic_string_view/inserters/char/3.cc
+++ b/gdb/unittests/basic_string_view/inserters/char/3.cc
@@ -23,16 +23,13 @@
 // working, not to mention other major details like char_traits, and
 // all of the string_view class.
 
-#include <string_view>
-#include <sstream>
-#include <iomanip>
-#include <testsuite_hooks.h>
+namespace inserters_3 {
 
 // libstdc++/2830
 void
 test09()
 {
-  std::string_view foo{"peace\0\0\0& love"};
+  gdb::string_view foo{"peace\0\0\0& love"};
 
   std::ostringstream oss1;
   oss1 << foo;
@@ -52,3 +49,5 @@ main()
 
   return 0;
 }
+
+} // namespace inserters_3
diff --git a/gdb/unittests/basic_string_view/literals/types.cc b/gdb/unittests/basic_string_view/literals/types.cc
index 8b3b467efc43..2efa9b98a2c7 100644
--- a/gdb/unittests/basic_string_view/literals/types.cc
+++ b/gdb/unittests/basic_string_view/literals/types.cc
@@ -18,20 +18,20 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-#include <string_view>
-#include <type_traits>
+namespace literals_types {
 
 void
 test01()
 {
-  using namespace std::literals::string_view_literals;
+  using namespace gdb::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("Hello"sv), gdb::string_view>::value,
+		"\"Hello\"s is gdb::string_view");
 
-  static_assert(std::is_same<decltype(u8"Hello"sv), std::string_view>::value,
-		"u8\"Hello\"s is std::string_view");
+  static_assert(std::is_same<decltype(u8"Hello"sv), gdb::string_view>::value,
+		"u8\"Hello\"s is gdb::string_view");
 
+#ifndef GDB_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");
@@ -42,4 +42,7 @@ test01()
 
   static_assert(std::is_same<decltype(U"Hello"sv), std::u32string_view>::value,
 		"U\"Hello\"s is std::u32string_view");
+#endif
 }
+
+} // namespace literals_types
diff --git a/gdb/unittests/basic_string_view/literals/values.cc b/gdb/unittests/basic_string_view/literals/values.cc
index b2897a136b8b..79e99af25903 100644
--- a/gdb/unittests/basic_string_view/literals/values.cc
+++ b/gdb/unittests/basic_string_view/literals/values.cc
@@ -17,51 +17,66 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace literals_values {
 
 void
 test01()
 {
-  using namespace std::literals::string_view_literals;
+  using namespace gdb::literals::string_view_literals;
 
-  std::string_view planet = "Mercury"sv;
+  gdb::string_view planet = "Mercury"sv;
+#ifndef GDB_STRING_VIEW
 #ifdef _GLIBCXX_USE_WCHAR_T
   std::wstring_view wplanet = L"Venus"sv;
 #endif
-  std::string_view u8planet = u8"Mars"sv;
+#endif
+  gdb::string_view u8planet = u8"Mars"sv;
+#ifndef GDB_STRING_VIEW
   std::u16string_view u16planet = u"Jupiter"sv;
   std::u32string_view u32planet = U"Saturn"sv;
+#endif
 
-  VERIFY( planet == std::string_view("Mercury") );
+  VERIFY( planet == gdb::string_view("Mercury") );
+#ifndef GDB_STRING_VIEW
 #ifdef _GLIBCXX_USE_WCHAR_T
   VERIFY( wplanet == std::wstring_view(L"Venus") );
 #endif
-  VERIFY( u8planet == std::string_view(u8"Mars") );
+#endif
+  VERIFY( u8planet == gdb::string_view(u8"Mars") );
+#ifndef GDB_STRING_VIEW
   VERIFY( u16planet == std::u16string_view(u"Jupiter") );
   VERIFY( u32planet == std::u32string_view(U"Saturn") );
+#endif
 }
 
 void
 test02()
 {
-  using namespace std::literals::string_view_literals;
+  using namespace gdb::literals::string_view_literals;
 
-  std::string_view planet_cratered = "Mercury\0cratered"sv;
+  gdb::string_view planet_cratered = "Mercury\0cratered"sv;
+#ifndef GDB_STRING_VIEW
 #ifdef _GLIBCXX_USE_WCHAR_T
   std::wstring_view wplanet_cratered = L"Venus\0cratered"sv;
 #endif
-  std::string_view u8planet_cratered = u8"Mars\0cratered"sv;
+#endif
+  gdb::string_view u8planet_cratered = u8"Mars\0cratered"sv;
+#ifndef GDB_STRING_VIEW
   std::u16string_view u16planet_cratered = u"Jupiter\0cratered"sv;
   std::u32string_view u32planet_cratered = U"Saturn\0cratered"sv;
+#endif
 
-  VERIFY( planet_cratered == std::string_view("Mercury\0cratered", 16) );
+  VERIFY( planet_cratered == gdb::string_view("Mercury\0cratered", 16) );
+#ifndef GDB_STRING_VIEW
 #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) );
+#endif
+  VERIFY( u8planet_cratered == gdb::string_view(u8"Mars\0cratered", 13) );
+#ifndef GDB_STRING_VIEW
   VERIFY( u16planet_cratered == std::u16string_view(u"Jupiter\0cratered", 16) );
   VERIFY( u32planet_cratered == std::u32string_view(U"Saturn\0cratered", 15) );
+#endif
 }
 
 int
@@ -69,4 +84,7 @@ main()
 {
   test01();
   test02();
+  return 0;
 }
+
+} // namespace literals_values
diff --git a/gdb/unittests/basic_string_view/modifiers/remove_prefix/char/1.cc b/gdb/unittests/basic_string_view/modifiers/remove_prefix/char/1.cc
index ba08a98eeaec..f73fdc72012b 100644
--- a/gdb/unittests/basic_string_view/modifiers/remove_prefix/char/1.cc
+++ b/gdb/unittests/basic_string_view/modifiers/remove_prefix/char/1.cc
@@ -17,13 +17,12 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace modifiers_remove_prefix {
 
 void
 test01()
 {
-  using std::string_view;
+  using gdb::string_view;
 
   string_view str0{"olympus mons"};
   string_view::pointer p = str0.data();
@@ -33,6 +32,7 @@ test01()
   VERIFY( str0 == string_view{"pus mons"} );
 }
 
+#ifndef GDB_STRING_VIEW
 constexpr bool
 test02()
 {
@@ -50,12 +50,17 @@ test02()
 
   return true;
 }
+#endif
 
 int
 main()
 { 
   test01();
+#ifndef GDB_STRING_VIEW
   static_assert( test02() );
+#endif
 
   return 0;
 }
+
+} // namespace modifiers_remove_prefix
diff --git a/gdb/unittests/basic_string_view/modifiers/remove_suffix/char/1.cc b/gdb/unittests/basic_string_view/modifiers/remove_suffix/char/1.cc
index 1b71ee936e8c..0407f37d227b 100644
--- a/gdb/unittests/basic_string_view/modifiers/remove_suffix/char/1.cc
+++ b/gdb/unittests/basic_string_view/modifiers/remove_suffix/char/1.cc
@@ -17,13 +17,12 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace modifiers_remove_suffix {
 
 void
 test01()
 {
-  using std::string_view;
+  using gdb::string_view;
 
   string_view str0{"olympus mons"};
   string_view::pointer p = str0.data();
@@ -33,6 +32,7 @@ test01()
   VERIFY( str0 == string_view{"olympus mo"} );
 }
 
+#ifndef GDB_STRING_VIEW
 constexpr bool
 test02()
 {
@@ -50,12 +50,17 @@ test02()
 
   return true;
 }
+#endif
 
 int
 main()
 { 
   test01();
+#ifndef GDB_STRING_VIEW
   static_assert( test02() );
+#endif
 
   return 0;
 }
+
+} // namespace modifiers_remove_suffix
diff --git a/gdb/unittests/basic_string_view/modifiers/swap/char/1.cc b/gdb/unittests/basic_string_view/modifiers/swap/char/1.cc
index 90d26692599c..3bc7f01fa7ab 100644
--- a/gdb/unittests/basic_string_view/modifiers/swap/char/1.cc
+++ b/gdb/unittests/basic_string_view/modifiers/swap/char/1.cc
@@ -18,18 +18,19 @@
 // { dg-options "-std=gnu++17" }
 // { dg-do compile { target c++17 } }
 
-#include <string_view>
+namespace modifiers_swap {
 
-constexpr bool
+void
 test01()
 {
-  using std::string_view;
+  using gdb::string_view;
 
   string_view s1{"last"};
   string_view s2{"first"};
 
   s1.swap(s2);
-  return s1 == "first" && s2 == "last";
+  VERIFY( s1 == "first" );
+  VERIFY( s2 == "last" );
 }
 
-static_assert( test01() );
+} // namespace modifiers_swap
diff --git a/gdb/unittests/basic_string_view/operations/compare/char/1.cc b/gdb/unittests/basic_string_view/operations/compare/char/1.cc
index 46691ebd52a1..66d2613a25b9 100644
--- a/gdb/unittests/basic_string_view/operations/compare/char/1.cc
+++ b/gdb/unittests/basic_string_view/operations/compare/char/1.cc
@@ -29,9 +29,7 @@
 // NB compare should be thought of as a lexographical compare, ie how
 // things would be sorted in a dictionary.
 
-#include <string_view>
-#include <cstring>
-#include <testsuite_hooks.h>
+namespace operations_compare_1 {
 
 enum want_value {lt=0, z=1, gt=2};
 
@@ -66,7 +64,7 @@ test_value(int result, want_value expected)
 int
 test01()
 {
-  using std::string_view;
+  using gdb::string_view;
 
   string_view 	str_0("costa rica");
   string_view 	str_1("costa marbella");
@@ -130,3 +128,5 @@ main()
 
   return 0;
 }
+
+} // namespace operations_compare_1
diff --git a/gdb/unittests/basic_string_view/operations/compare/char/13650.cc b/gdb/unittests/basic_string_view/operations/compare/char/13650.cc
index ef9df20e6eec..0cc3ee824cbf 100644
--- a/gdb/unittests/basic_string_view/operations/compare/char/13650.cc
+++ b/gdb/unittests/basic_string_view/operations/compare/char/13650.cc
@@ -19,14 +19,13 @@
 
 // basic_string_view::compare [lib.string_view::compare]
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operations_compare_13650 {
 
 // libstdc++/13650
 void
 test01()
 {
-  using std::string_view;
+  using gdb::string_view;
 
   const char lit_01[]{ 'w', 'e', '\0', 'r', 'd' };
   const char lit_02[]{ 'w', 'e', 'i', '\0', 'd' };
@@ -47,3 +46,5 @@ main()
 
   return 0;
 }
+
+} // namespace operations_compare_13650
diff --git a/gdb/unittests/basic_string_view/operations/copy/char/1.cc b/gdb/unittests/basic_string_view/operations/copy/char/1.cc
index 6caf0e1507a6..5b6dd346583f 100644
--- a/gdb/unittests/basic_string_view/operations/copy/char/1.cc
+++ b/gdb/unittests/basic_string_view/operations/copy/char/1.cc
@@ -19,16 +19,15 @@
 
 // basic_string_view::copy
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operations_copy_1 {
 
 void
 test01()
 {
-  typedef std::string_view::size_type csize_type;
+  typedef gdb::string_view::size_type csize_type;
 
   const char str_lit01[] = "123456789A";
-  const std::string_view str01(str_lit01);
+  const gdb::string_view str01(str_lit01);
   char buffer[4] = { 0 };
 
   csize_type len = str01.copy(buffer, sizeof(buffer), 8);
@@ -43,3 +42,5 @@ main()
 
   return 0;
 }
+
+} // namespace operations_copy_1
diff --git a/gdb/unittests/basic_string_view/operations/data/char/1.cc b/gdb/unittests/basic_string_view/operations/data/char/1.cc
index 18fc9f5fe888..4149e16b22dd 100644
--- a/gdb/unittests/basic_string_view/operations/data/char/1.cc
+++ b/gdb/unittests/basic_string_view/operations/data/char/1.cc
@@ -19,16 +19,15 @@
 
 // string_view operations
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operations_data_1 {
 
 int
 test01()
 {
-  std::string_view empty;
+  gdb::string_view empty;
 
   VERIFY( empty.size() == 0 );
-  const std::string_view::value_type* p = empty.data();
+  const gdb::string_view::value_type* p = empty.data();
   VERIFY( p == nullptr );
 
   return 0;
@@ -41,3 +40,5 @@ main()
 
   return 0;
 }
+
+} // namespace operations_data_1
diff --git a/gdb/unittests/basic_string_view/operations/find/char/1.cc b/gdb/unittests/basic_string_view/operations/find/char/1.cc
index d7d2f37706eb..a68434a22a9d 100644
--- a/gdb/unittests/basic_string_view/operations/find/char/1.cc
+++ b/gdb/unittests/basic_string_view/operations/find/char/1.cc
@@ -19,23 +19,22 @@
 
 // basic_string_view find
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operations_find_1 {
 
 void
 test01()
 {
-  typedef std::string_view::size_type csize_type;
-  typedef std::string_view::const_reference cref;
-  typedef std::string_view::reference ref;
-  csize_type npos = std::string_view::npos;
+  typedef gdb::string_view::size_type csize_type;
+  typedef gdb::string_view::const_reference cref;
+  typedef gdb::string_view::reference ref;
+  csize_type npos = gdb::string_view::npos;
   csize_type csz01, csz02;
 
   const char str_lit01[] = "mave";
-  const std::string_view str01("mavericks, santa cruz");
-  std::string_view str02(str_lit01);
-  std::string_view str03("s, s");
-  std::string_view str04;
+  const gdb::string_view str01("mavericks, santa cruz");
+  gdb::string_view str02(str_lit01);
+  gdb::string_view str03("s, s");
+  gdb::string_view str04;
 
   // size_type find(const string_view&, size_type pos = 0) const;
   csz01 = str01.find(str01);
@@ -85,6 +84,7 @@ test01()
   VERIFY( csz01 == npos );
 }
 
+#ifndef GDB_STRING_VIEW
 constexpr bool
 test02()
 {
@@ -152,13 +152,17 @@ test02()
 
   return true;
 }
-
+#endif
 
 int
 main()
 {
   test01();
+#ifndef GDB_STRING_VIEW
   static_assert( test02() );
+#endif
 
   return 0;
 }
+
+} // namespace operations_find_1
diff --git a/gdb/unittests/basic_string_view/operations/find/char/2.cc b/gdb/unittests/basic_string_view/operations/find/char/2.cc
index af8a526d12c7..ce4579eaf952 100644
--- a/gdb/unittests/basic_string_view/operations/find/char/2.cc
+++ b/gdb/unittests/basic_string_view/operations/find/char/2.cc
@@ -19,24 +19,23 @@
 
 // basic_string_view find_first_of
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operations_find_2 {
 
 void
 test02()
 {
-  typedef std::string_view::size_type csize_type;
-  csize_type npos = std::string_view::npos;
+  typedef gdb::string_view::size_type csize_type;
+  csize_type npos = gdb::string_view::npos;
   csize_type csz01, csz02;
 
   const char str_lit01[] = "mave";
-  const std::string_view str01("mavericks, santa cruz");
-  std::string_view str02(str_lit01);
-  std::string_view str03("s, s");
-  std::string_view str04;
+  const gdb::string_view str01("mavericks, santa cruz");
+  gdb::string_view str02(str_lit01);
+  gdb::string_view str03("s, s");
+  gdb::string_view str04;
 
   // size_type find_first_of(const string_view&, size_type pos = 0) const;
-  std::string_view str05("xena rulez");
+  gdb::string_view str05("xena rulez");
   csz01 = str01.find_first_of(str01);
   VERIFY( csz01 == 0 );
   csz01 = str01.find_first_of(str01, 4);
@@ -84,6 +83,7 @@ test02()
   VERIFY( csz01 == csz02 );
 }
 
+#ifndef GDB_STRING_VIEW
 constexpr bool
 test03()
 {
@@ -150,12 +150,17 @@ test03()
 
   return true;
 }
+#endif
 
 int
 main()
 {
   test02();
+#ifndef GDB_STRING_VIEW
   static_assert( test03() );
+#endif
 
   return 0;
 }
+
+} // namespace operations_find_2 {
diff --git a/gdb/unittests/basic_string_view/operations/find/char/3.cc b/gdb/unittests/basic_string_view/operations/find/char/3.cc
index 5ec651ef3f93..71fd83b727c6 100644
--- a/gdb/unittests/basic_string_view/operations/find/char/3.cc
+++ b/gdb/unittests/basic_string_view/operations/find/char/3.cc
@@ -19,21 +19,20 @@
 
 // basic_string_view find_first_not_of
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operations_find_3 {
 
 void
 test03()
 {
-  typedef std::string_view::size_type csize_type;
-  csize_type npos = std::string_view::npos;
+  typedef gdb::string_view::size_type csize_type;
+  csize_type npos = gdb::string_view::npos;
   csize_type csz01;
 
-  const std::string_view str01("Bob Rock, per me");
+  const gdb::string_view str01("Bob Rock, per me");
   const char str_lit01[] = "Bob Rock";
-  std::string_view str02("ovvero Trivi");
-  std::string_view str03(str_lit01);
-  std::string_view str04;
+  gdb::string_view str02("ovvero Trivi");
+  gdb::string_view str03(str_lit01);
+  gdb::string_view str04;
 
   // size_type find_first_not_of(const string_view&, size_type pos = 0) const;
   csz01 = str01.find_first_not_of(str01);
@@ -84,6 +83,7 @@ test03()
   VERIFY( csz01 == npos );
 }
 
+#ifndef GDB_STRING_VIEW
 constexpr bool
 test04()
 {
@@ -150,12 +150,17 @@ test04()
 
   return true;
 }
+#endif
 
 int
 main()
 {
   test03();
+#ifndef GDB_STRING_VIEW
   static_assert( test04() );
+#endif
 
   return 0;
 }
+
+} // namespace operations_find_3
diff --git a/gdb/unittests/basic_string_view/operations/find/char/4.cc b/gdb/unittests/basic_string_view/operations/find/char/4.cc
index ca4b1f17db42..23d15b291347 100644
--- a/gdb/unittests/basic_string_view/operations/find/char/4.cc
+++ b/gdb/unittests/basic_string_view/operations/find/char/4.cc
@@ -19,17 +19,16 @@
 
 // basic_string_view find
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operations_find_4 {
 
 // libstdc++/31401
 void
 test01()
 {
-  typedef std::string_view::size_type csize_type;
-  csize_type npos = std::string_view::npos;
+  typedef gdb::string_view::size_type csize_type;
+  csize_type npos = gdb::string_view::npos;
 
-  std::string_view use = "anu";
+  gdb::string_view use = "anu";
   csize_type pos1 = use.find("a", npos);
 
   VERIFY( pos1 == npos );
@@ -42,3 +41,5 @@ main()
 
   return 0;
 }
+
+} // namespace operations_find_4
diff --git a/gdb/unittests/basic_string_view/operations/rfind/char/1.cc b/gdb/unittests/basic_string_view/operations/rfind/char/1.cc
index ef26688fd761..8b0c49800d75 100644
--- a/gdb/unittests/basic_string_view/operations/rfind/char/1.cc
+++ b/gdb/unittests/basic_string_view/operations/rfind/char/1.cc
@@ -17,25 +17,24 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operations_rfind_1 {
 
 // basic_string_view rfind
 
 void
 test01()
 {
-  typedef std::string_view::size_type csize_type;
-  typedef std::string_view::const_reference cref;
-  typedef std::string_view::reference ref;
-  csize_type npos = std::string_view::npos;
+  typedef gdb::string_view::size_type csize_type;
+  typedef gdb::string_view::const_reference cref;
+  typedef gdb::string_view::reference ref;
+  csize_type npos = gdb::string_view::npos;
   csize_type csz01, csz02;
 
   const char str_lit01[] = "mave";
-  const std::string_view str01("mavericks, santa cruz");
-  std::string_view str02(str_lit01);
-  std::string_view str03("s, s");
-  std::string_view str04;
+  const gdb::string_view str01("mavericks, santa cruz");
+  gdb::string_view str02(str_lit01);
+  gdb::string_view str03("s, s");
+  gdb::string_view str04;
 
   // size_type rfind(const string_view&, size_type pos = 0) const;
   csz01 = str01.rfind(str01);
@@ -92,3 +91,5 @@ main()
 
   return 0;
 }
+
+} // namespace operations_rfind_1
diff --git a/gdb/unittests/basic_string_view/operations/rfind/char/2.cc b/gdb/unittests/basic_string_view/operations/rfind/char/2.cc
index df37f8c7745b..e51022da3faf 100644
--- a/gdb/unittests/basic_string_view/operations/rfind/char/2.cc
+++ b/gdb/unittests/basic_string_view/operations/rfind/char/2.cc
@@ -17,16 +17,15 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operations_rfind_2 {
 
 // basic_string_view::find_last_of
 
 void
 test02()
 {
-  std::string_view z("ab");
-  std::string_view::size_type pos;
+  gdb::string_view z("ab");
+  gdb::string_view::size_type pos;
   pos = z.find_last_of("ab");
   VERIFY( pos == 1 );
   pos = z.find_last_of("Xa");
@@ -34,13 +33,13 @@ test02()
   pos = z.find_last_of("Xb");
   VERIFY( pos == 1 );
   pos = z.find_last_of("XYZ");
-  VERIFY( pos == std::string_view::npos );
+  VERIFY( pos == gdb::string_view::npos );
   pos = z.find_last_of('a');
   VERIFY( pos == 0 );
   pos = z.find_last_of('b');
   VERIFY( pos == 1 );
   pos = z.find_last_of('X');
-  VERIFY( pos == std::string_view::npos );
+  VERIFY( pos == gdb::string_view::npos );
 }
 
 int
@@ -50,3 +49,5 @@ main()
 
   return 0;
 }
+
+} // namespace operations_rfind_2
diff --git a/gdb/unittests/basic_string_view/operations/rfind/char/3.cc b/gdb/unittests/basic_string_view/operations/rfind/char/3.cc
index 37f01a28af7f..02d6a7325315 100644
--- a/gdb/unittests/basic_string_view/operations/rfind/char/3.cc
+++ b/gdb/unittests/basic_string_view/operations/rfind/char/3.cc
@@ -17,25 +17,24 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operations_rfind_3 {
 
 // basic_string_view::find_last_not_of
 
 void
 test03()
 {
-  typedef std::string_view::size_type csize_type;
-  std::string_view::size_type pos;
-  csize_type npos = std::string_view::npos;
+  typedef gdb::string_view::size_type csize_type;
+  gdb::string_view::size_type pos;
+  csize_type npos = gdb::string_view::npos;
 
-  std::string_view x;
+  gdb::string_view x;
   pos = x.find_last_not_of('X');
   VERIFY( pos == npos );
   pos = x.find_last_not_of("XYZ");
   VERIFY( pos == npos );
 
-  std::string_view y("a");
+  gdb::string_view y("a");
   pos = y.find_last_not_of('X');
   VERIFY( pos == 0 );
   pos = y.find_last_not_of('a');
@@ -45,7 +44,7 @@ test03()
   pos = y.find_last_not_of("a");
   VERIFY( pos == npos );
 
-  std::string_view z("ab");
+  gdb::string_view z("ab");
   pos = z.find_last_not_of('X');
   VERIFY( pos == 1 );
   pos = z.find_last_not_of("XYZ");
@@ -64,3 +63,5 @@ main()
 
   return 0;
 }
+
+} // namespace operations_rfind_3
diff --git a/gdb/unittests/basic_string_view/operations/substr/char/1.cc b/gdb/unittests/basic_string_view/operations/substr/char/1.cc
index ac65c23c54be..e694611f00c0 100644
--- a/gdb/unittests/basic_string_view/operations/substr/char/1.cc
+++ b/gdb/unittests/basic_string_view/operations/substr/char/1.cc
@@ -19,21 +19,19 @@
 
 // basic_string_view::substr
 
-#include <string_view>
-#include <stdexcept>
-#include <testsuite_hooks.h>
+namespace operations_substr_1 {
 
 void
 test01()
 {
-  typedef std::string_view::size_type csize_type;
-  typedef std::string_view::const_reference cref;
-  typedef std::string_view::reference ref;
+  typedef gdb::string_view::size_type csize_type;
+  typedef gdb::string_view::const_reference cref;
+  typedef gdb::string_view::reference ref;
   csize_type csz01;
 
   const char str_lit01[] = "rockaway, pacifica";
-  const std::string_view str01(str_lit01);
-  std::string_view str02;
+  const gdb::string_view str01(str_lit01);
+  gdb::string_view str02;
 
   // basic_string_view<charT, _Traits, _Alloc>
   //  substr(size_type pos = 0, size_type n = npos) const;
@@ -43,31 +41,31 @@ test01()
   str02 = str01.substr(10);
   VERIFY( str02 == "pacifica" );
 
-  try
+  bool threw = false;
+  TRY
   {
     str02 = str01.substr(csz01 + 1);
     VERIFY( false ); 
   }
-  catch(std::out_of_range& fail)
+  CATCH (ex, RETURN_MASK_ALL)
   {
-    VERIFY( true );
-  }
-  catch(...)
-  {
-    VERIFY( false );
+    threw = true;
   }
+  END_CATCH
+  VERIFY ( threw );
 
-  try
+  TRY
   {
     str02 = str01.substr(csz01);
     VERIFY( str02.size() == 0 );
     VERIFY( str02.begin() == str01.end() );
     VERIFY( true );
   }
-  catch(...)
+  CATCH (ex, RETURN_MASK_ALL)
   {
     VERIFY( false );
   }
+  END_CATCH
 }
 
 int
@@ -77,3 +75,5 @@ main()
 
   return 0;
 }
+
+} // namespace operations_substr_1
diff --git a/gdb/unittests/basic_string_view/operators/char/2.cc b/gdb/unittests/basic_string_view/operators/char/2.cc
index 4be652daafb2..963bc4cec865 100644
--- a/gdb/unittests/basic_string_view/operators/char/2.cc
+++ b/gdb/unittests/basic_string_view/operators/char/2.cc
@@ -109,17 +109,16 @@ template<class charT, class traits, class Allocator>
                   const basic_string<charT,traits,Allocator>& rhs);
 */
 
-#include <string_view>
-#include <testsuite_hooks.h>
+namespace operators_2 {
 
 void
 test01()
 {
-  std::string_view 	str_0("costa rica");
-  std::string_view 	str_1("costa marbella");
-  std::string_view 	str_2("cost");
-  std::string_view	str_3("costa ricans");
-  std::string_view        str_4;
+  gdb::string_view 	str_0("costa rica");
+  gdb::string_view 	str_1("costa marbella");
+  gdb::string_view 	str_2("cost");
+  gdb::string_view	str_3("costa ricans");
+  gdb::string_view      str_4;
 
   str_4 = str_0;
   //comparisons between string objects
@@ -233,6 +232,7 @@ test01()
   VERIFY( str_0 <= "costa rica" );
 }
 
+#ifndef GDB_STRING_VIEW
 constexpr bool
 test02()
 {
@@ -358,10 +358,16 @@ test02()
 
   return true;
 }
+#endif
 
 int
 main()
 {
   test01();
+#ifndef GDB_STRING_VIEW
   static_assert( test02() );
+#endif
+  return 0;
 }
+
+} // namespace operators_2
diff --git a/gdb/unittests/string_view-selftests.c b/gdb/unittests/string_view-selftests.c
new file mode 100644
index 000000000000..0e5776108926
--- /dev/null
+++ b/gdb/unittests/string_view-selftests.c
@@ -0,0 +1,138 @@
+/* Self tests for string_view for GDB, the GNU debugger.
+
+   Copyright (C) 2018 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program 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 of the License, or
+   (at your option) any later version.
+
+   This program 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 program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* No need to test string_view if we're using C++17, since we're going to use
+   the "real" version.  */
+#if __cplusplus < 201703L
+
+#include "defs.h"
+#include "selftest.h"
+#include "common/string_view.h"
+
+/* Used by the included .cc files below.  Included here because the
+   included test files are wrapped in a namespace.  */
+#include <string>
+#include <sstream>
+#include <fstream>
+#include <iostream>
+
+/* libstdc++'s testsuite uses VERIFY.  */
+#define VERIFY SELF_CHECK
+
+/* Used to disable testing features not supported by
+   gdb::optional.  */
+#define GDB_STRING_VIEW
+
+namespace selftests {
+namespace string_view {
+
+/* The actual tests live in separate files, which were originally
+   copied over from libstdc++'s testsuite.  To preserve the structure
+   and help with comparison with the original tests, the file names
+   have been preserved, and only minimal modification was done to have
+   them compile against gdb::optional instead of std::optional:
+
+     - std::optional->gdb:optional, etc.
+     - ATTRIBUTE_UNUSED in a few places
+     - wrap each file in a namespace so they can all be compiled as a
+       single unit.
+     - libstdc++'s license and formatting style was preserved.
+*/
+
+#include "basic_string_view/capacity/1.cc"
+#include "basic_string_view/cons/char/1.cc"
+#include "basic_string_view/cons/char/2.cc"
+#include "basic_string_view/cons/char/3.cc"
+#include "basic_string_view/element_access/char/1.cc"
+#include "basic_string_view/element_access/char/empty.cc"
+#include "basic_string_view/element_access/char/front_back.cc"
+#include "basic_string_view/inserters/char/1.cc"
+#include "basic_string_view/inserters/char/2.cc"
+#include "basic_string_view/inserters/char/3.cc"
+#include "basic_string_view/literals/types.cc"
+#include "basic_string_view/literals/values.cc"
+#include "basic_string_view/modifiers/remove_prefix/char/1.cc"
+#include "basic_string_view/modifiers/remove_suffix/char/1.cc"
+#include "basic_string_view/modifiers/swap/char/1.cc"
+#include "basic_string_view/operations/compare/char/1.cc"
+#include "basic_string_view/operations/compare/char/13650.cc"
+#include "basic_string_view/operations/copy/char/1.cc"
+#include "basic_string_view/operations/data/char/1.cc"
+#include "basic_string_view/operations/find/char/1.cc"
+#include "basic_string_view/operations/find/char/2.cc"
+#include "basic_string_view/operations/find/char/3.cc"
+#include "basic_string_view/operations/find/char/4.cc"
+#include "basic_string_view/operations/rfind/char/1.cc"
+#include "basic_string_view/operations/rfind/char/2.cc"
+#include "basic_string_view/operations/rfind/char/3.cc"
+#include "basic_string_view/operations/substr/char/1.cc"
+#include "basic_string_view/operators/char/2.cc"
+
+static void
+run_tests ()
+{
+  capacity_1::main ();
+  cons_1::main ();
+  cons_2::main ();
+  cons_3::main ();
+  element_access_1::main ();
+  element_access_empty::main ();
+  element_access_front_back::main ();
+  inserters_1::test01 ();
+  inserters_2::main ();
+  inserters_3::main ();
+  literals_values::main ();
+  modifiers_remove_prefix::main ();
+  modifiers_remove_suffix::main ();
+  modifiers_swap::test01 ();
+  operations_compare_1::main ();
+  operations_compare_13650::main ();
+  operations_copy_1::main ();
+  operations_data_1::main ();
+  operations_find_1::main ();
+  operations_find_2::main ();
+  operations_find_3::main ();
+  operations_find_4::main ();
+  operations_rfind_1::main ();
+  operations_rfind_2::main ();
+  operations_rfind_3::main ();
+  operations_substr_1::main ();
+  operators_2::main ();
+
+  std::string std_string = "fika";
+  gdb::string_view sv1 (std_string);
+  SELF_CHECK (sv1 == "fika");
+
+  gdb::string_view sv2 ("fika");
+  SELF_CHECK (sv2 == "fika");
+
+  constexpr gdb::string_view sv3 ("fika", 4);
+  SELF_CHECK (sv3 == "fika");
+}
+
+} /* namespace string_view */
+} /* namespace selftests */
+
+void
+_initialize_string_view_selftests ()
+{
+  selftests::register_test ("string_view", selftests::string_view::run_tests);
+}
+
+#endif /* __cplusplus < 201703L */
