PR libstdc++/88084 - Implement LWG 2777

* include/std/string_view (basic_string_view::copy): Use traits to
	copy.
	* testsuite/21_strings/basic_string_view/operations/copy/char/2.cc:
	New test.
	* testsuite/21_strings/basic_string_view/operations/copy/wchar_t/2.cc:
	New test.

From-SVN: r266269
This commit is contained in:
Jonathan Wakely 2018-11-19 10:53:59 +00:00 committed by Jonathan Wakely
parent 0ea52812a8
commit d86b600d89
4 changed files with 125 additions and 3 deletions

View File

@ -1,3 +1,13 @@
2018-11-19 Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/88084 - Implement LWG 2777
* include/std/string_view (basic_string_view::copy): Use traits to
copy.
* testsuite/21_strings/basic_string_view/operations/copy/char/2.cc:
New test.
* testsuite/21_strings/basic_string_view/operations/copy/wchar_t/2.cc:
New test.
2018-11-18 Michele Pezzutti <mpezz@tiscali.it>
Edward Smith-Rowland <3dw4rd@verizon.net>

View File

@ -236,9 +236,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__glibcxx_requires_string_len(__str, __n);
__pos = _M_check(__pos, "basic_string_view::copy");
const size_type __rlen = std::min(__n, _M_len - __pos);
for (auto __begin = this->_M_str + __pos,
__end = __begin + __rlen; __begin != __end;)
*__str++ = *__begin++;
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2777. basic_string_view::copy should use char_traits::copy
traits_type::copy(__str, data() + __pos, __rlen);
return __rlen;
}

View File

@ -0,0 +1,56 @@
// Copyright (C) 2018 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/>.
// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }
#include <string_view>
#include <testsuite_hooks.h>
using char_type = char;
// PR libstdc++/88084
// LWG 2777. basic_string_view::copy should use char_traits::copy
struct traits : std::char_traits<char_type>
{
static char_type*
copy(char_type* s, const char_type* p, std::size_t n)
{
while (n--)
*s++ = 'X';
return s;
}
};
void
test01()
{
std::basic_string_view<char_type, traits> s = "abc";
char_type buf[3] = { '1', '2', '3' };
auto len = s.copy(buf, 3, 1);
VERIFY( len == 2 );
VERIFY( buf[0] == 'X' );
VERIFY( buf[1] == 'X' );
VERIFY( buf[2] == '3' );
}
int
main()
{
test01();
}

View File

@ -0,0 +1,56 @@
// Copyright (C) 2018 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/>.
// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }
#include <string_view>
#include <testsuite_hooks.h>
using char_type = wchar_t;
// PR libstdc++/88084
// LWG 2777. basic_string_view::copy should use char_traits::copy
struct traits : std::char_traits<char_type>
{
static char_type*
copy(char_type* s, const char_type* p, std::size_t n)
{
while (n--)
*s++ = 'X';
return s;
}
};
void
test01()
{
std::basic_string_view<char_type, traits> s = L"abc";
char_type buf[3] = { L'1', L'2', L'3' };
auto len = s.copy(buf, 3, 1);
VERIFY( len == 2 );
VERIFY( buf[0] == L'X' );
VERIFY( buf[1] == L'X' );
VERIFY( buf[2] == L'3' );
}
int
main()
{
test01();
}