From 2cb612d1b0459fdb72a0af8928741869f2fdaaed Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Mon, 26 Jan 2004 09:07:18 +0000 Subject: [PATCH] basic_string.tcc (replace(size_type, size_type, const _CharT*, size_type)): Implement optimized in-place algorithm for non-overlapping ranges. 2004-01-26 Paolo Carlini * include/bits/basic_string.tcc (replace(size_type, size_type, const _CharT*, size_type)): Implement optimized in-place algorithm for non-overlapping ranges. * testsuite/21_strings/basic_string/replace/char/6.cc: New. * testsuite/21_strings/basic_string/replace/wchar_t/6.cc: New. * include/bits/basic_string.tcc (insert(size_type, const _CharT*, size_type)): Tweak slightly. From-SVN: r76625 --- libstdc++-v3/ChangeLog | 11 ++++ libstdc++-v3/include/bits/basic_string.tcc | 22 +++++++- .../21_strings/basic_string/replace/char/6.cc | 55 +++++++++++++++++++ .../basic_string/replace/wchar_t/6.cc | 55 +++++++++++++++++++ 4 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/replace/char/6.cc create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/replace/wchar_t/6.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 31453b0aa87..2ffa536bac6 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,14 @@ +2004-01-26 Paolo Carlini + + * include/bits/basic_string.tcc (replace(size_type, + size_type, const _CharT*, size_type)): Implement optimized + in-place algorithm for non-overlapping ranges. + * testsuite/21_strings/basic_string/replace/char/6.cc: New. + * testsuite/21_strings/basic_string/replace/wchar_t/6.cc: New. + + * include/bits/basic_string.tcc (insert(size_type, + const _CharT*, size_type)): Tweak slightly. + 2004-01-26 Andreas Schwab * config/locale/gnu/monetary_members.cc: Restore locale before diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 3d9df9f5e48..30075f0d6a2 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -319,8 +319,9 @@ namespace std traits_type::copy(__p, __s + __n, __n); else { - traits_type::copy(__p, __s, __p - __s); - traits_type::copy(__p + (__p-__s), __p + __n, __n - (__p-__s)); + const size_type __nleft = __p - __s; + traits_type::copy(__p, __s, __nleft); + traits_type::copy(__p + __nleft, __p + __n, __n - __nleft); } return *this; } @@ -337,12 +338,27 @@ namespace std __n1 = _M_limit(__pos, __n1); if (this->max_size() - (this->size() - __n1) < __n2) __throw_length_error("basic_string::replace"); + bool __left; if (_M_rep()->_M_is_shared() || less()(__s, _M_data()) || less()(_M_data() + this->size(), __s)) return _M_replace_safe(__pos, __n1, __s, __n2); + else if ((__left = __s + __n2 <= _M_data() + __pos) + || _M_data() + __pos + __n1 <= __s) + { + // Work in-place: non-overlapping case. + const size_type __off = __s - _M_data(); + _M_mutate(__pos, __n1, __n2); + if (__left) + traits_type::copy(_M_data() + __pos, + _M_data() + __off, __n2); + else + traits_type::copy(_M_data() + __pos, + _M_data() + __off + __n2 - __n1, __n2); + return *this; + } else { - // Todo: optimized in-place replace. + // Todo: overlapping case. const basic_string __tmp(__s, __n2); return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2); } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/replace/char/6.cc b/libstdc++-v3/testsuite/21_strings/basic_string/replace/char/6.cc new file mode 100644 index 00000000000..23ab34e0ef7 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/replace/char/6.cc @@ -0,0 +1,55 @@ +// 2004-01-26 Paolo Carlini + +// Copyright (C) 2004 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 2, 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 COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 21.3.5.6 basic_string::replace + +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + + std::string str01("Valle Del Salto"); + str01.replace(0, 5, str01.data() + 10, 5); + VERIFY( str01 == "Salto Del Salto" ); + + std::string str02("Colle di Val d'Elsa"); + str02.replace(0, 9, str02.data() + 10, 0); + VERIFY( str02 == "Val d'Elsa" ); + + std::string str03("Novi Ligure"); + str03.replace(11, 0, str03.data() + 4, 7); + VERIFY( str03 == "Novi Ligure Ligure"); + + std::string str04("Trebisacce"); + str04.replace(3, 4, str04.data(), 0); + VERIFY( str04 == "Trecce" ); + + std::string str05("Altopiano della Sila"); + str05.replace(1, 18, str05.data() + 19, 1); + VERIFY( str05 == "Aaa" ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/replace/wchar_t/6.cc b/libstdc++-v3/testsuite/21_strings/basic_string/replace/wchar_t/6.cc new file mode 100644 index 00000000000..ca16482a8f2 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/replace/wchar_t/6.cc @@ -0,0 +1,55 @@ +// 2004-01-26 Paolo Carlini + +// Copyright (C) 2004 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 2, 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 COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 21.3.5.6 basic_string::replace + +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + + std::wstring str01(L"Valle Del Salto"); + str01.replace(0, 5, str01.data() + 10, 5); + VERIFY( str01 == L"Salto Del Salto" ); + + std::wstring str02(L"Colle di Val d'Elsa"); + str02.replace(0, 9, str02.data() + 10, 0); + VERIFY( str02 == L"Val d'Elsa" ); + + std::wstring str03(L"Novi Ligure"); + str03.replace(11, 0, str03.data() + 4, 7); + VERIFY( str03 == L"Novi Ligure Ligure"); + + std::wstring str04(L"Trebisacce"); + str04.replace(3, 4, str04.data(), 0); + VERIFY( str04 == L"Trecce" ); + + std::wstring str05(L"Altopiano della Sila"); + str05.replace(1, 18, str05.data() + 19, 1); + VERIFY( str05 == L"Aaa" ); +} + +int main() +{ + test01(); + return 0; +} -- 2.30.2