aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3
diff options
context:
space:
mode:
authorPaolo Carlini <pcarlini@suse.de>2004-01-26 09:07:18 +0000
committerPaolo Carlini <paolo@gcc.gnu.org>2004-01-26 09:07:18 +0000
commit2cb612d1b0459fdb72a0af8928741869f2fdaaed (patch)
tree36afdfcaedf56568d0cbb4e4cfeca1d1d58a4a59 /libstdc++-v3
parent6865f4cd9fcc09aa8166eb1cd18e473d9378ab34 (diff)
downloadgcc-2cb612d1b0459fdb72a0af8928741869f2fdaaed.zip
gcc-2cb612d1b0459fdb72a0af8928741869f2fdaaed.tar.gz
gcc-2cb612d1b0459fdb72a0af8928741869f2fdaaed.tar.bz2
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 <pcarlini@suse.de> * 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
Diffstat (limited to 'libstdc++-v3')
-rw-r--r--libstdc++-v3/ChangeLog11
-rw-r--r--libstdc++-v3/include/bits/basic_string.tcc22
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string/replace/char/6.cc55
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string/replace/wchar_t/6.cc55
4 files changed, 140 insertions, 3 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 31453b0..2ffa536 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,14 @@
+2004-01-26 Paolo Carlini <pcarlini@suse.de>
+
+ * 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 <schwab@suse.de>
* 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 3d9df9f..30075f0 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<const _CharT*>()(__s, _M_data())
|| less<const _CharT*>()(_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 0000000..23ab34e
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/replace/char/6.cc
@@ -0,0 +1,55 @@
+// 2004-01-26 Paolo Carlini <pcarlini@suse.de>
+
+// 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 <string>
+#include <testsuite_hooks.h>
+
+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 0000000..ca16482
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/replace/wchar_t/6.cc
@@ -0,0 +1,55 @@
+// 2004-01-26 Paolo Carlini <pcarlini@suse.de>
+
+// 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 <string>
+#include <testsuite_hooks.h>
+
+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;
+}