]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
basic_string.tcc (replace(size_type, size_type, const _CharT*, size_type)): Implement...
authorPaolo Carlini <pcarlini@suse.de>
Mon, 26 Jan 2004 09:07:18 +0000 (09:07 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Mon, 26 Jan 2004 09:07:18 +0000 (09:07 +0000)
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

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/basic_string.tcc
libstdc++-v3/testsuite/21_strings/basic_string/replace/char/6.cc [new file with mode: 0644]
libstdc++-v3/testsuite/21_strings/basic_string/replace/wchar_t/6.cc [new file with mode: 0644]

index 31453b0aa87c81f6fb0788b1d77658a899a36e06..2ffa536bac684d4e9f9f4c038edb5a2ffb8b59d2 100644 (file)
@@ -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
index 3d9df9f5e4817f5796c0b946299389c5d7a3db49..30075f0d6a2e4035bf2a769d6e97ab3eb71c6518 100644 (file)
@@ -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 (file)
index 0000000..23ab34e
--- /dev/null
@@ -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 (file)
index 0000000..ca16482
--- /dev/null
@@ -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;
+}