Implement N3762 string_view: a non-owning reference to a string.

Ed Smith-Rowland 3dw4rd@verizon.net
Wed Nov 20 14:09:00 GMT 2013


On 11/18/2013 09:08 AM, Daniel Krügler wrote:
> 2013/11/15 Ed Smith-Rowland <3dw4rd@verizon.net>:
>> Greetings,
>>
>> As the title says.  It's a pretty simple class" a non-mutating read-only
>> view into a const CharT* or a basic_string.
>>
>> Built and tested on x86_64-linux.
>>
>> OK?
> Sorry, if this is documented somewhere, but is this intended to implement
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3762.html
>
> ? I don't think that your implementation is valid, because it maps an
> empty string view into a NULL data pointer. But according to the
> specification of data() the return type, this isn't feasible:
>
> <quote>
> constexpr const charT* data() const noexcept;
>
> Returns: A non-null pointer p such that p + i == &operator[](i) for
> each i in [0,size()).
> </quote>
>
> - Daniel
>
Right.

Here is a fix that introduces an constexpr unit length string to 
represent empty strings so that refs to empty strings don't point to the 
ether.

Built and tested on x86_64-linux.

OK?

-------------- next part --------------
2013-11-20  Edward Smith-Rowland  <3dw4rd@verizon.net>

	* include/experimental/string_view: Rep empty string with unit-length
	static constexpr string. Uncomment _GLIBCXX_VISIBILITY. Enforce
	invariant of no nullptr string pointer.
	* include/experimental/string_view.tcc: Ditto.
	* testsuite/experimental/string_view/cons/char/1.cc: data() for empty
	string_view is no longer nullptr.
	* testsuite/experimental/string_view/cons/wchar_t/1.cc: Ditto.
	* testsuite/experimental/string_view/operations/data/char/1.cc: Ditto.
	* testsuite/experimental/string_view/operations/data/wchar_t/1.cc:
	Ditto.
-------------- next part --------------
Index: include/experimental/string_view
===================================================================
--- include/experimental/string_view	(revision 205087)
+++ include/experimental/string_view	(working copy)
@@ -43,7 +43,7 @@
 #include <string>
 #include <limits>
 
-namespace std //_GLIBCXX_VISIBILITY(default)
+namespace std _GLIBCXX_VISIBILITY(default)
 {
 namespace experimental
 {
@@ -66,6 +66,13 @@
    *    _CharT*    _M_str
    *    size_t     _M_len
    *  @endcode
+   *
+   *  A basic_string_view represents an empty string with a static constexpr
+   *  length one string:
+   *
+   *  @code
+   *    static constexpr value_type _S_empty_str[1]{0};
+   *  @endcode
    */
   template<typename _CharT, typename _Traits = char_traits<_CharT>>
     class basic_string_view
@@ -92,7 +99,7 @@
 
       constexpr
       basic_string_view() noexcept
-      : _M_len{0}, _M_str{nullptr}
+      : _M_len{0}, _M_str{_S_empty_str}
       { }
 
       constexpr basic_string_view(const basic_string_view&) noexcept = default;
@@ -104,7 +111,8 @@
         { }
 
       constexpr basic_string_view(const _CharT* __str)
-      : _M_len{__str == nullptr ? 0 : traits_type::length(__str)}, _M_str{__str}
+      : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
+	_M_str{__str == nullptr ? _S_empty_str : __str}
       { }
 
       constexpr basic_string_view(const _CharT* __str, size_type __len)
@@ -185,7 +193,7 @@
 					     "(which is %zu) >= this->size() "
 					     "(which is %zu)"),
 					 __pos, this->size()),
-		*static_cast<pointer>(nullptr));
+		_S_empty_str[0]);
       }
 
       constexpr const _CharT&
@@ -211,7 +219,7 @@
       clear() noexcept
       {
 	this->_M_len = 0;
-	this->_M_str = nullptr;
+	this->_M_str = _S_empty_str;
       }
 
       void
@@ -408,6 +416,7 @@
       }
 
     private:
+
       static constexpr const int
       _S_compare(size_type __n1, size_type __n2) noexcept
       {
@@ -418,6 +427,8 @@
 	     : static_cast<int>(difference_type{__n1 - __n2});
       }
 
+      static constexpr value_type _S_empty_str[1]{};
+
       size_t	    _M_len;
       const _CharT* _M_str;
     };
Index: include/experimental/string_view.tcc
===================================================================
--- include/experimental/string_view.tcc	(revision 205087)
+++ include/experimental/string_view.tcc	(working copy)
@@ -40,13 +40,17 @@
 # include <bits/c++14_warning.h>
 #else
 
-namespace std //_GLIBCXX_VISIBILITY(default)
+namespace std _GLIBCXX_VISIBILITY(default)
 {
 namespace experimental
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   template<typename _CharT, typename _Traits>
+    constexpr _CharT
+    basic_string_view<_CharT, _Traits>::_S_empty_str[1];
+
+  template<typename _CharT, typename _Traits>
     typename basic_string_view<_CharT, _Traits>::size_type
     basic_string_view<_CharT, _Traits>::
     find(const _CharT* __str, size_type __pos, size_type __n) const noexcept
Index: testsuite/experimental/string_view/cons/char/1.cc
===================================================================
--- testsuite/experimental/string_view/cons/char/1.cc	(revision 205087)
+++ testsuite/experimental/string_view/cons/char/1.cc	(working copy)
@@ -33,7 +33,7 @@
   // basic_string_view()
   const std::experimental::string_view str00{};
   VERIFY( str00.length() == 0 );
-  VERIFY( str00.data() == nullptr );
+  VERIFY( str00.data() != nullptr );
 
   // basic_string_view(const char*)
   const char str_lit01[] = "rodeo beach, marin";
@@ -53,10 +53,6 @@
   std::experimental::string_view str05{str_lit01, len_lit01};
   VERIFY( str05.length() == len_lit01 );
   VERIFY( str05.data() == str_lit01 );
-  //  This is invalid and unchecked.
-  //std::experimental::string_view str06{nullptr, len_lit01};
-  //VERIFY( str06.length() == 0 );
-  //VERIFY( str06.data() == nullptr );
 
   // basic_string_view(basic_string& s)
   std::string istr07(10, 'z');
Index: testsuite/experimental/string_view/cons/wchar_t/1.cc
===================================================================
--- testsuite/experimental/string_view/cons/wchar_t/1.cc	(revision 205087)
+++ testsuite/experimental/string_view/cons/wchar_t/1.cc	(working copy)
@@ -33,7 +33,7 @@
   // basic_string_view()
   const std::experimental::wstring_view str00{};
   VERIFY( str00.length() == 0 );
-  VERIFY( str00.data() == nullptr );
+  VERIFY( str00.data() != nullptr );
 
   // basic_string_view(const char*)
   const wchar_t str_lit01[] = L"rodeo beach, marin";
@@ -53,10 +53,6 @@
   std::experimental::wstring_view str05{str_lit01, len_lit01};
   VERIFY( str05.length() == len_lit01 );
   VERIFY( str05.data() == str_lit01 );
-  //  This is invalid and unchecked.
-  //std::experimental::wstring_view str06{nullptr, len_lit01};
-  //VERIFY( str06.length() == 0 );
-  //VERIFY( str06.data() == nullptr );
 
   // basic_string_view(basic_string& s)
   std::wstring istr07(10, L'z');
Index: testsuite/experimental/string_view/operations/data/char/1.cc
===================================================================
--- testsuite/experimental/string_view/operations/data/char/1.cc	(revision 205087)
+++ testsuite/experimental/string_view/operations/data/char/1.cc	(working copy)
@@ -29,10 +29,10 @@
 
   std::experimental::string_view empty;
 
-  // data() for size == 0 is non-NULL?
+  // data() for size == 0 is non-NULL.
   VERIFY( empty.size() == 0 );
   const std::experimental::string_view::value_type* p = empty.data();
-  VERIFY( p == nullptr );
+  VERIFY( p );
 
   return 0;
 }
Index: testsuite/experimental/string_view/operations/data/wchar_t/1.cc
===================================================================
--- testsuite/experimental/string_view/operations/data/wchar_t/1.cc	(revision 205087)
+++ testsuite/experimental/string_view/operations/data/wchar_t/1.cc	(working copy)
@@ -29,10 +29,10 @@
 
   std::experimental::wstring_view empty;
 
-  // data() for size == 0 is non-NULL?
+  // data() for size == 0 is non-NULL.
   VERIFY( empty.size() == 0 );
   const std::experimental::wstring_view::value_type* p = empty.data();
-  VERIFY( p == nullptr );
+  VERIFY( p );
 
   return 0;
 }


More information about the Libstdc++ mailing list