[PATCH] Basic_string::replace

Paolo Carlini pcarlini@unitus.it
Sun Jan 13 03:27:00 GMT 2002


Hi,

this patch, the last for basic_string I'm going to submit before 3.1, updates
replace members consistently with the current append/assign/insert members.

Ideally, replace(__pos, __n1, __s, __n2) should be also optimized for the
in-place case, as happens for assign and insert, but this is particularly tricky
here and I would rather prefer leaving this project for post-3.1. Also, as
suggested by Nathan many times, it would be nice to overload the various
basic_string members for source iterators as pointers, another nice project for
post-3.1.

I took the occasion to fix a risk of overflow in insert(__pos, __s, __n).

Tested i686-pc-linux-gnu.

Ok to commit?

Cheers,
Paolo.

/////////////////

2002-01-13  Paolo Carlini  <pcarlini@unitus.it>
            Nathan Myers  <ncm@cantrip.org>

        * include/bits/basic_string.h
        (replace(__pos, __n1, __s, __n2)): Optimize by avoiding
        temporaries (i.e., call _M_replace_safe) when possible.
        (replace(__pos, __n, __str)): Call replace(__pos, __n1, __s, __n2).
        (replace(__pos, __n1, __s)): Call replace(__pos, __n1, __s , __n2).
        (replace(__i1, __i2, __str)): Call replace(__i1, __i2, __s, __n).
        (replace(__i1, __i2, __s)): Call replace(__i1, __i2, __s, __n).
        (replace(__i1, __i2, __s, __n)): Call replace(__pos1, __n1, __s, __n2).
        * include/bits/basic_string.tcc
        (replace(__pos1, __n1, __str, __pos2, __n2)): Call
        replace(__pos, __n1, __s, __n2).
        * testsuite/21_strings/replace.cc (test03): New testcases.

        * include/bits/basic_string.h (insert(__pos, __s, __n)):
        Adjust comparison vs overflow.

diff -urN libstdc++-v3-orig/include/bits/basic_string.h
libstdc++-v3/include/bits/basic_string.h
--- libstdc++-v3-orig/include/bits/basic_string.h Fri Dec 28 15:00:17 2001
+++ libstdc++-v3/include/bits/basic_string.h Sat Jan 12 18:52:22 2002
@@ -1,6 +1,7 @@
 // Components for manipulating sequences of characters -*- C++ -*-

-// Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
+// 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
@@ -551,7 +552,7 @@
  const size_type __size = this->size();
   if (__pos > __size)
    __throw_out_of_range("basic_string::insert");
- if (__n + __size > this->max_size())
+ if (__size > this->max_size() - __n)
    __throw_length_error("basic_string::insert");
  if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
      || less<const _CharT*>()(_M_data() + __size, __s))
@@ -626,10 +627,7 @@

       basic_string&
       replace(size_type __pos, size_type __n, const basic_string& __str)
-      {
- return this->replace(_M_check(__pos), _M_fold(__pos, __n),
-         __str.begin(), __str.end());
-      }
+      { return this->replace(__pos, __n, __str._M_data(), __str.size()); }

       basic_string&
       replace(size_type __pos1, size_type __n1, const basic_string& __str,
@@ -639,36 +637,41 @@
       replace(size_type __pos, size_type __n1, const _CharT* __s,
        size_type __n2)
       {
- return this->replace(_M_check(__pos), _M_fold(__pos, __n1),
-        __s, __s + __n2);
+ const size_type __size = this->size();
+  if (__pos > __size)
+   __throw_out_of_range("basic_string::replace");
+ if (__size - __n1 > this->max_size() - __n2)
+   __throw_length_error("basic_string::replace");
+ const bool __testn1 = __n1 < __size - __pos;
+ const size_type __foldn1 = __testn1 ? __n1 : __size - __pos;
+ if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
+     || less<const _CharT*>()(_M_data() + __size, __s))
+   return _M_replace_safe(_M_ibegin() + __pos,
+     _M_ibegin() + __pos + __foldn1, __s, __s + __n2);
+ else return this->replace(_M_check(__pos), _M_fold(__pos, __n1),
+      __s, __s + __n2);
       }

       basic_string&
       replace(size_type __pos, size_type __n1, const _CharT* __s)
-      {
- return this->replace(_M_check(__pos), _M_fold(__pos, __n1),
-        __s, __s + traits_type::length(__s));
-      }
+      { return this->replace(__pos, __n1, __s, traits_type::length(__s)); }

       basic_string&
       replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
-      {
- return this->replace(_M_check(__pos), _M_fold(__pos, __n1), __n2, __c);
-      }
+      { return this->replace(_M_check(__pos), _M_fold(__pos, __n1), __n2, __c);
}

       basic_string&
       replace(iterator __i1, iterator __i2, const basic_string& __str)
-      { return this->replace(__i1, __i2, __str.begin(), __str.end()); }
+      { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }

       basic_string&
       replace(iterator __i1, iterator __i2,
                            const _CharT* __s, size_type __n)
-      { return this->replace(__i1, __i2, __s, __s + __n); }
+      { return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n); }

       basic_string&
       replace(iterator __i1, iterator __i2, const _CharT* __s)
-      { return this->replace(__i1, __i2, __s,
-        __s + traits_type::length(__s)); }
+      { return this->replace(__i1, __i2, __s, traits_type::length(__s)); }

       basic_string&
       replace(iterator __i1, iterator __i2, size_type __n, _CharT __c);
diff -urN libstdc++-v3-orig/include/bits/basic_string.tcc
libstdc++-v3/include/bits/basic_string.tcc
--- libstdc++-v3-orig/include/bits/basic_string.tcc Sun Dec 16 02:02:17 2001
+++ libstdc++-v3/include/bits/basic_string.tcc Sat Jan 12 18:01:15 2002
@@ -1,6 +1,7 @@
 // Components for manipulating sequences of characters -*- C++ -*-

-// Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
+// 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
@@ -543,9 +544,13 @@
     replace(size_type __pos1, size_type __n1, const basic_string& __str,
      size_type __pos2, size_type __n2)
     {
-      return this->replace(_M_check(__pos1), _M_fold(__pos1, __n1),
-      __str._M_check(__pos2),
-      __str._M_fold(__pos2, __n2));
+      const size_type __strsize = __str.size();
+      if (__pos2 > __strsize)
+ __throw_out_of_range("basic_string::replace");
+      const bool __testn2 = __n2 < __strsize - __pos2;
+      const size_type __foldn2 = __testn2 ? __n2 : __strsize - __pos2;
+      return this->replace(__pos1, __n1,
+      __str._M_data() + __pos2, __foldn2);
     }

   template<typename _CharT, typename _Traits, typename _Alloc>
diff -urN libstdc++-v3-orig/testsuite/21_strings/replace.cc
libstdc++-v3/testsuite/21_strings/replace.cc
--- libstdc++-v3-orig/testsuite/21_strings/replace.cc Wed Oct 31 09:27:20 2001
+++ libstdc++-v3/testsuite/21_strings/replace.cc Sat Jan 12 18:00:49 2002
@@ -1,6 +1,6 @@
 // 1999-06-10 bkoz

-// Copyright (C) 1994, 1999 Free Software Foundation, Inc.
+// Copyright (C) 1994, 1999, 2001, 2002 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
@@ -96,9 +96,53 @@
   VERIFY(aux == "../the lone long pier/Hanr/Hanalei Bay/Kauai/Hawaii");
 }

+// Some more miscellaneous tests
+void
+test03()
+{
+  const char* title01 = "nine types of ambiguity";
+  const char* title02 = "ultra";
+  std::string str01 = title01;
+  std::string str02 = title02;
+
+  str01.replace(0, 4, str02);
+  VERIFY(str01 == "ultra types of ambiguity");
+
+  str01.replace(15, 9, str02, 2, 2);
+  VERIFY(str01 == "ultra types of tr");
+
+  str01 = title01;
+  str02.replace(0, 0, str01, 0, std::string::npos);
+  VERIFY(str02 == "nine types of ambiguityultra");
+
+  str02.replace(11, 2, title02, 5);
+  VERIFY(str02 == "nine types ultra ambiguityultra");
+
+  str02.replace(11, 5, title01, 2);
+  VERIFY(str02 == "nine types ni ambiguityultra");
+
+  str01.replace(str01.size(), 0, title02);
+  VERIFY(str01 == "nine types of ambiguityultra");
+
+  str01 = title01;
+  str02 = title02;
+  str01.replace(str01.begin(), str01.end(), str02);
+  VERIFY(str01 == "ultra");
+
+  str01.replace(str01.begin(), str01.begin(), title01, 4);
+  VERIFY(str01 == "nineultra");
+
+  str01.replace(str01.end(), str01.end(), title01 + 5, 5);
+  VERIFY(str01 == "nineultratypes");
+
+  str01.replace(str01.begin(), str01.end(), title02);
+  VERIFY(str01 == "ultra");
+}
+
 int main()
 {
   test01();
   test02();
+  test03();
   return 0;
 }




More information about the Libstdc++ mailing list