From 4f3db60ba863eebeab1df6c7ec21324feba3bca1 Mon Sep 17 00:00:00 2001 From: redi Date: Sun, 1 Jun 2014 17:23:41 +0000 Subject: [PATCH] PR libstdc++/61374 * include/experimental/string_view (operator basic_string): Correct order of arguments. (to_string): Replace with member function. Add inline specifiers. Remove unused header. Remove _S_empty_rep and allow _M_str to be null. * testsuite/experimental/string_view/cons/char/1.cc: Adjust to new default constructor semantics. * testsuite/experimental/string_view/cons/wchar_t/1.cc: Likewise. * testsuite/experimental/string_view/operations/copy/char/1.cc: Fix copyright dates. Remove unused header. * testsuite/experimental/string_view/operations/copy/wchar_t/1.cc: Likewise. * testsuite/experimental/string_view/operations/data/char/1.cc: Fix copyright dates. Adjust to new default constructor semantics. * testsuite/experimental/string_view/operations/data/wchar_t/1.cc: Likewise. * testsuite/experimental/string_view/operations/to_string/1.cc: New. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@211113 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 21 ++++ libstdc++-v3/include/experimental/string_view | 108 +++++++++------------ libstdc++-v3/include/experimental/string_view.tcc | 4 - .../experimental/string_view/cons/char/1.cc | 7 +- .../experimental/string_view/cons/wchar_t/1.cc | 7 +- .../string_view/operations/copy/char/1.cc | 3 +- .../string_view/operations/copy/wchar_t/1.cc | 3 +- .../string_view/operations/data/char/1.cc | 5 +- .../string_view/operations/data/wchar_t/1.cc | 5 +- .../operations/{copy/char => to_string}/1.cc | 29 +++--- 10 files changed, 92 insertions(+), 100 deletions(-) copy libstdc++-v3/testsuite/experimental/string_view/operations/{copy/char => to_string}/1.cc (58%) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 203ea7ddaf80..4d69c259abed 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,24 @@ +2014-06-01 Jonathan Wakely + + PR libstdc++/61374 + * include/experimental/string_view (operator basic_string): Correct + order of arguments. + (to_string): Replace with member function. + Add inline specifiers. Remove unused header. Remove _S_empty_rep and + allow _M_str to be null. + * testsuite/experimental/string_view/cons/char/1.cc: Adjust to new + default constructor semantics. + * testsuite/experimental/string_view/cons/wchar_t/1.cc: Likewise. + * testsuite/experimental/string_view/operations/copy/char/1.cc: Fix + copyright dates. Remove unused header. + * testsuite/experimental/string_view/operations/copy/wchar_t/1.cc: + Likewise. + * testsuite/experimental/string_view/operations/data/char/1.cc: + Fix copyright dates. Adjust to new default constructor semantics. + * testsuite/experimental/string_view/operations/data/wchar_t/1.cc: + Likewise. + * testsuite/experimental/string_view/operations/to_string/1.cc: New. + 2014-05-30 Jonathan Wakely * testsuite/lib/libstdc++.exp (libstdc++_init): Adjust regexp to diff --git a/libstdc++-v3/include/experimental/string_view b/libstdc++-v3/include/experimental/string_view index 6b6588b97708..49f46af544f0 100644 --- a/libstdc++-v3/include/experimental/string_view +++ b/libstdc++-v3/include/experimental/string_view @@ -39,7 +39,6 @@ # include #else -#include #include #include @@ -66,18 +65,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * _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> + template> class basic_string_view { - public: // types @@ -99,7 +90,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr basic_string_view() noexcept - : _M_len{0}, _M_str{_S_empty_str} + : _M_len{0}, _M_str{nullptr} { } constexpr basic_string_view(const basic_string_view&) noexcept = default; @@ -112,12 +103,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr basic_string_view(const _CharT* __str) : _M_len{__str == nullptr ? 0 : traits_type::length(__str)}, - _M_str{__str == nullptr ? _S_empty_str : __str} + _M_str{__str} { } constexpr basic_string_view(const _CharT* __str, size_type __len) - : _M_len{__str == nullptr ? 0 :__len}, - _M_str{__str == nullptr ? _S_empty_str : __str} + : _M_len{__len}, + _M_str{__str} { } basic_string_view& @@ -143,19 +134,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const_reverse_iterator rbegin() const noexcept - { return std::reverse_iterator(this->end()); } + { return const_reverse_iterator(this->end()); } const_reverse_iterator rend() const noexcept - { return std::reverse_iterator(this->begin()); } + { return const_reverse_iterator(this->begin()); } const_reverse_iterator crbegin() const noexcept - { return std::reverse_iterator(this->end()); } + { return const_reverse_iterator(this->end()); } const_reverse_iterator crend() const noexcept - { return std::reverse_iterator(this->begin()); } + { return const_reverse_iterator(this->begin()); } // [string.view.capacity], capacity @@ -169,8 +160,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr size_type max_size() const noexcept - { return ((npos - sizeof(size_type) - sizeof(void*)) - / sizeof(value_type) / 4); } + { + return (npos - sizeof(size_type) - sizeof(void*)) + / sizeof(value_type) / 4; + } constexpr bool empty() const noexcept @@ -195,7 +188,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION "(which is %zu) >= this->size() " "(which is %zu)"), __pos, this->size()), - _S_empty_str[0]); + *this->_M_str); } constexpr const _CharT& @@ -219,11 +212,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return this->_M_str; } // [string.view.modifiers], modifiers: + void clear() noexcept { this->_M_len = 0; - this->_M_str = _S_empty_str; + this->_M_str = nullptr; } void @@ -251,8 +245,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template explicit operator basic_string<_CharT, _Traits, _Allocator>() const { - return basic_string<_CharT, _Traits, _Allocator> - (this->_M_len, this->_M_str); + return { this->_M_str, this->_M_len }; + } + + template> + basic_string<_CharT, _Traits, _Allocator> + to_string(const _Allocator& __alloc = _Allocator()) const + { + return { this->_M_str, this->_M_len, __alloc }; } size_type @@ -431,8 +431,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : static_cast(difference_type{__n1 - __n2}); } - static constexpr value_type _S_empty_str[1]{}; - size_t _M_len; const _CharT* _M_str; }; @@ -456,131 +454,119 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template - bool + inline bool operator==(basic_string_view<_CharT, _Traits> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return __x.compare(__y) == 0; } template - bool + inline bool operator==(basic_string_view<_CharT, _Traits> __x, __detail::__idt> __y) noexcept { return __x.compare(__y) == 0; } template - bool + inline bool operator==(__detail::__idt> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return __x.compare(__y) == 0; } template - bool + inline bool operator!=(basic_string_view<_CharT, _Traits> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return !(__x == __y); } template - bool + inline bool operator!=(basic_string_view<_CharT, _Traits> __x, __detail::__idt> __y) noexcept { return !(__x == __y); } template - bool + inline bool operator!=(__detail::__idt> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return !(__x == __y); } template - bool + inline bool operator< (basic_string_view<_CharT, _Traits> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return __x.compare(__y) < 0; } template - bool + inline bool operator< (basic_string_view<_CharT, _Traits> __x, __detail::__idt> __y) noexcept { return __x.compare(__y) < 0; } template - bool + inline bool operator< (__detail::__idt> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return __x.compare(__y) < 0; } template - bool + inline bool operator> (basic_string_view<_CharT, _Traits> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return __x.compare(__y) > 0; } template - bool + inline bool operator> (basic_string_view<_CharT, _Traits> __x, __detail::__idt> __y) noexcept { return __x.compare(__y) > 0; } template - bool + inline bool operator> (__detail::__idt> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return __x.compare(__y) > 0; } template - bool + inline bool operator<=(basic_string_view<_CharT, _Traits> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return __x.compare(__y) <= 0; } template - bool + inline bool operator<=(basic_string_view<_CharT, _Traits> __x, __detail::__idt> __y) noexcept { return __x.compare(__y) <= 0; } template - bool + inline bool operator<=(__detail::__idt> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return __x.compare(__y) <= 0; } template - bool + inline bool operator>=(basic_string_view<_CharT, _Traits> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return __x.compare(__y) >= 0; } template - bool + inline bool operator>=(basic_string_view<_CharT, _Traits> __x, __detail::__idt> __y) noexcept { return __x.compare(__y) >= 0; } template - bool + inline bool operator>=(__detail::__idt> __x, basic_string_view<_CharT, _Traits> __y) noexcept { return __x.compare(__y) >= 0; } - // [string.view.comparison], sufficient additional overloads of comparison functions - - // [string.view.nonmem], other non-member basic_string_view functions - template, - typename _Allocator = allocator<_CharT>> - basic_string<_CharT, _Traits, _Allocator> - to_string(basic_string_view<_CharT, _Traits> __str, - const _Allocator& __alloc = _Allocator()) - { - return basic_string<_CharT, _Traits, _Allocator> - (__str.begin(), __str.end(), __alloc); - } - + // [string.view.io], Inserters and extractors template - basic_ostream<_CharT, _Traits>& - operator<<(basic_ostream<_CharT, _Traits>& __os, - basic_string_view<_CharT,_Traits> __str) - { return __ostream_insert(__os, __str.data(), __str.size()); } + inline basic_ostream<_CharT, _Traits>& + operator<<(basic_ostream<_CharT, _Traits>& __os, + basic_string_view<_CharT,_Traits> __str) + { return __ostream_insert(__os, __str.data(), __str.size()); } // basic_string_view typedef names diff --git a/libstdc++-v3/include/experimental/string_view.tcc b/libstdc++-v3/include/experimental/string_view.tcc index 1af3a4d9bbd1..44562665abef 100644 --- a/libstdc++-v3/include/experimental/string_view.tcc +++ b/libstdc++-v3/include/experimental/string_view.tcc @@ -47,10 +47,6 @@ namespace experimental _GLIBCXX_BEGIN_NAMESPACE_VERSION template - constexpr _CharT - basic_string_view<_CharT, _Traits>::_S_empty_str[1]; - - template 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 diff --git a/libstdc++-v3/testsuite/experimental/string_view/cons/char/1.cc b/libstdc++-v3/testsuite/experimental/string_view/cons/char/1.cc index c879cc7e00ea..a443b0ac7e03 100644 --- a/libstdc++-v3/testsuite/experimental/string_view/cons/char/1.cc +++ b/libstdc++-v3/testsuite/experimental/string_view/cons/char/1.cc @@ -33,7 +33,7 @@ test01() // 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"; @@ -54,11 +54,6 @@ test01() VERIFY( str05.length() == len_lit01 ); VERIFY( str05.data() == str_lit01 ); - // basic_string_view(const char* s, std::size_t l) - 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'); std::experimental::string_view str07{istr07}; diff --git a/libstdc++-v3/testsuite/experimental/string_view/cons/wchar_t/1.cc b/libstdc++-v3/testsuite/experimental/string_view/cons/wchar_t/1.cc index 12db72f28658..9ba9b84a1805 100644 --- a/libstdc++-v3/testsuite/experimental/string_view/cons/wchar_t/1.cc +++ b/libstdc++-v3/testsuite/experimental/string_view/cons/wchar_t/1.cc @@ -33,7 +33,7 @@ test01() // 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"; @@ -54,11 +54,6 @@ test01() VERIFY( str05.length() == len_lit01 ); VERIFY( str05.data() == str_lit01 ); - // basic_string_view(const wchar_t* s, std::size_t l) - 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'); std::experimental::wstring_view str07{istr07}; diff --git a/libstdc++-v3/testsuite/experimental/string_view/operations/copy/char/1.cc b/libstdc++-v3/testsuite/experimental/string_view/operations/copy/char/1.cc index 25b2af133d30..d0f3e8d69011 100644 --- a/libstdc++-v3/testsuite/experimental/string_view/operations/copy/char/1.cc +++ b/libstdc++-v3/testsuite/experimental/string_view/operations/copy/char/1.cc @@ -1,6 +1,6 @@ // { dg-options "-std=gnu++1y" } -// Copyright (C) 2013 Free Software Foundation, Inc. +// Copyright (C) 2013-2014 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 @@ -20,7 +20,6 @@ // basic_string_view::copy #include -#include #include bool diff --git a/libstdc++-v3/testsuite/experimental/string_view/operations/copy/wchar_t/1.cc b/libstdc++-v3/testsuite/experimental/string_view/operations/copy/wchar_t/1.cc index 0348e1f98fc8..bf3f14b14bd2 100644 --- a/libstdc++-v3/testsuite/experimental/string_view/operations/copy/wchar_t/1.cc +++ b/libstdc++-v3/testsuite/experimental/string_view/operations/copy/wchar_t/1.cc @@ -1,6 +1,6 @@ // { dg-options "-std=gnu++1y" } -// Copyright (C) 2013 Free Software Foundation, Inc. +// Copyright (C) 2013-2014 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 @@ -20,7 +20,6 @@ // basic_string_view::copy #include -#include #include bool diff --git a/libstdc++-v3/testsuite/experimental/string_view/operations/data/char/1.cc b/libstdc++-v3/testsuite/experimental/string_view/operations/data/char/1.cc index be75de917919..a34492606ad7 100644 --- a/libstdc++-v3/testsuite/experimental/string_view/operations/data/char/1.cc +++ b/libstdc++-v3/testsuite/experimental/string_view/operations/data/char/1.cc @@ -1,6 +1,6 @@ // { dg-options "-std=gnu++1y" } -// Copyright (C) 2013 Free Software Foundation, Inc. +// Copyright (C) 2013-2014 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 @@ -29,10 +29,9 @@ test01() std::experimental::string_view empty; - // data() for size == 0 is non-NULL. VERIFY( empty.size() == 0 ); const std::experimental::string_view::value_type* p = empty.data(); - VERIFY( p ); + VERIFY( p == nullptr ); return 0; } diff --git a/libstdc++-v3/testsuite/experimental/string_view/operations/data/wchar_t/1.cc b/libstdc++-v3/testsuite/experimental/string_view/operations/data/wchar_t/1.cc index 5e00b00b601b..41d2d1411d92 100644 --- a/libstdc++-v3/testsuite/experimental/string_view/operations/data/wchar_t/1.cc +++ b/libstdc++-v3/testsuite/experimental/string_view/operations/data/wchar_t/1.cc @@ -1,6 +1,6 @@ // { dg-options "-std=gnu++1y" } -// Copyright (C) 2013 Free Software Foundation, Inc. +// Copyright (C) 2013-2014 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 @@ -29,10 +29,9 @@ test01() std::experimental::wstring_view empty; - // data() for size == 0 is non-NULL. VERIFY( empty.size() == 0 ); const std::experimental::wstring_view::value_type* p = empty.data(); - VERIFY( p ); + VERIFY( p == nullptr ); return 0; } diff --git a/libstdc++-v3/testsuite/experimental/string_view/operations/copy/char/1.cc b/libstdc++-v3/testsuite/experimental/string_view/operations/to_string/1.cc similarity index 58% copy from libstdc++-v3/testsuite/experimental/string_view/operations/copy/char/1.cc copy to libstdc++-v3/testsuite/experimental/string_view/operations/to_string/1.cc index 25b2af133d30..c0a5734c6758 100644 --- a/libstdc++-v3/testsuite/experimental/string_view/operations/copy/char/1.cc +++ b/libstdc++-v3/testsuite/experimental/string_view/operations/to_string/1.cc @@ -1,6 +1,6 @@ // { dg-options "-std=gnu++1y" } -// Copyright (C) 2013 Free Software Foundation, Inc. +// Copyright (C) 2014 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 @@ -17,34 +17,37 @@ // with this library; see the file COPYING3. If not see // . -// basic_string_view::copy +// basic_string_view::to_string #include -#include +#include #include +#include bool test01() { bool test [[gnu::unused]] = true; - typedef std::experimental::string_view::size_type csize_type; - - const char str_lit01[] = "123456789A"; - const std::experimental::string_view str01(str_lit01); + const char str_lit[] = "123456789A"; + const std::experimental::string_view sv(str_lit); char buffer[4] = { 0 }; - csize_type len = str01.copy(buffer, sizeof(buffer), 8); - VERIFY( 2 == len ); - VERIFY( '9' == buffer[0] ); + auto s1 = sv.to_string(); + VERIFY( s1 == str_lit ); + using test_alloc = __gnu_test::tracker_allocator; + auto s2 = sv.to_string( test_alloc{} ); + static_assert( std::is_same::value, + "to_string() uses custom allocator" ); + VERIFY( std::equal(s1.begin(), s1.end(), s2.begin(), s2.end()) ); + auto s3 = static_cast(sv); + VERIFY( s3 == s1 ); return test; } int main() -{ +{ test01(); - - return 0; } -- 2.11.4.GIT