re PR libstdc++/61374 (string_view::operator string() is buggy)

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.

From-SVN: r211113
This commit is contained in:
Jonathan Wakely 2014-06-01 18:23:41 +01:00 committed by Jonathan Wakely
parent 6041d142c4
commit 16545743c6
10 changed files with 129 additions and 87 deletions

View File

@ -1,3 +1,24 @@
2014-06-01 Jonathan Wakely <jwakely@redhat.com>
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 <jwakely@redhat.com>
* testsuite/lib/libstdc++.exp (libstdc++_init): Adjust regexp to

View File

@ -39,7 +39,6 @@
# include <bits/c++14_warning.h>
#else
#include <debug/debug.h>
#include <string>
#include <limits>
@ -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<typename _CharT, typename _Traits = char_traits<_CharT>>
template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
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<const_iterator>(this->end()); }
{ return const_reverse_iterator(this->end()); }
const_reverse_iterator
rend() const noexcept
{ return std::reverse_iterator<const_iterator>(this->begin()); }
{ return const_reverse_iterator(this->begin()); }
const_reverse_iterator
crbegin() const noexcept
{ return std::reverse_iterator<const_iterator>(this->end()); }
{ return const_reverse_iterator(this->end()); }
const_reverse_iterator
crend() const noexcept
{ return std::reverse_iterator<const_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<typename _Allocator>
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<typename _Allocator = std::allocator<_CharT>>
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<int>(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<typename _CharT, typename _Traits>
bool
inline bool
operator==(basic_string_view<_CharT, _Traits> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return __x.compare(__y) == 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator==(basic_string_view<_CharT, _Traits> __x,
__detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
{ return __x.compare(__y) == 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return __x.compare(__y) == 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator!=(basic_string_view<_CharT, _Traits> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return !(__x == __y); }
template<typename _CharT, typename _Traits>
bool
inline bool
operator!=(basic_string_view<_CharT, _Traits> __x,
__detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
{ return !(__x == __y); }
template<typename _CharT, typename _Traits>
bool
inline bool
operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return !(__x == __y); }
template<typename _CharT, typename _Traits>
bool
inline bool
operator< (basic_string_view<_CharT, _Traits> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return __x.compare(__y) < 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator< (basic_string_view<_CharT, _Traits> __x,
__detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
{ return __x.compare(__y) < 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return __x.compare(__y) < 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator> (basic_string_view<_CharT, _Traits> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return __x.compare(__y) > 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator> (basic_string_view<_CharT, _Traits> __x,
__detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
{ return __x.compare(__y) > 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return __x.compare(__y) > 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator<=(basic_string_view<_CharT, _Traits> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return __x.compare(__y) <= 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator<=(basic_string_view<_CharT, _Traits> __x,
__detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
{ return __x.compare(__y) <= 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return __x.compare(__y) <= 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator>=(basic_string_view<_CharT, _Traits> __x,
basic_string_view<_CharT, _Traits> __y) noexcept
{ return __x.compare(__y) >= 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator>=(basic_string_view<_CharT, _Traits> __x,
__detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
{ return __x.compare(__y) >= 0; }
template<typename _CharT, typename _Traits>
bool
inline bool
operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __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 _CharT, typename _Traits = char_traits<_CharT>,
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<typename _CharT, typename _Traits>
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

View File

@ -46,10 +46,6 @@ 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>::

View File

@ -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};

View File

@ -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};

View File

@ -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 <experimental/string_view>
#include <stdexcept>
#include <testsuite_hooks.h>
bool

View File

@ -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 <experimental/string_view>
#include <stdexcept>
#include <testsuite_hooks.h>
bool

View File

@ -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;
}

View File

@ -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;
}

View File

@ -0,0 +1,53 @@
// { dg-options "-std=gnu++1y" }
// 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
// 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/>.
// basic_string_view::to_string
#include <experimental/string_view>
#include <algorithm>
#include <testsuite_hooks.h>
#include <testsuite_allocator.h>
bool
test01()
{
bool test [[gnu::unused]] = true;
const char str_lit[] = "123456789A";
const std::experimental::string_view sv(str_lit);
char buffer[4] = { 0 };
auto s1 = sv.to_string();
VERIFY( s1 == str_lit );
using test_alloc = __gnu_test::tracker_allocator<char>;
auto s2 = sv.to_string( test_alloc{} );
static_assert( std::is_same<decltype(s2)::allocator_type, test_alloc>::value,
"to_string() uses custom allocator" );
VERIFY( std::equal(s1.begin(), s1.end(), s2.begin(), s2.end()) );
auto s3 = static_cast<std::string>(sv);
VERIFY( s3 == s1 );
return test;
}
int
main()
{
test01();
}