]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
basic_string.h (replace(i1, i2, _CharT* k1, _CharT* k2), [...]): New specializations...
authorPaolo Carlini <pcarlini@unitus.it>
Sat, 18 May 2002 20:52:51 +0000 (22:52 +0200)
committerPaolo Carlini <paolo@gcc.gnu.org>
Sat, 18 May 2002 20:52:51 +0000 (20:52 +0000)
2002-05-18  Paolo Carlini  <pcarlini@unitus.it>
            Nathan Myers  <ncm@cantrip.org>
            Philip Martin  <philip@codematters.co.uk>

        * include/bits/basic_string.h
        (replace(i1, i2, _CharT* k1, _CharT* k2),
        replace(i2, i2, const _CharT* k1, const _CharT* k2),
        replace(i1, i2, iterator k1, iterator k2,
        replace(i1, i2, const_iterator k1, const_iterator k2):
        New specializations to optimize for the common cases of
        pointers and iterators.
        (replace(pos, n1, s, n2)): Tweak.
        * include/bits/basic_string.tcc: Tweak comments.
        * testsuite/21_strings/replace.cc (test05): New tests.

Co-Authored-By: Nathan Myers <ncm@cantrip.org>
Co-Authored-By: Philip Martin <philip@codematters.co.uk>
From-SVN: r53595

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/basic_string.h
libstdc++-v3/include/bits/basic_string.tcc
libstdc++-v3/testsuite/21_strings/replace.cc

index 21f58fdb6e02c9c084244b12aafe11b33c86b90e..0dc6e6dd858416a3bb1044eb16c4d9b8014cd217 100644 (file)
@@ -1,3 +1,18 @@
+2002-05-18  Paolo Carlini  <pcarlini@unitus.it>
+            Nathan Myers  <ncm@cantrip.org>
+            Philip Martin  <philip@codematters.co.uk>
+
+       * include/bits/basic_string.h
+       (replace(i1, i2, _CharT* k1, _CharT* k2),
+       replace(i2, i2, const _CharT* k1, const _CharT* k2),
+       replace(i1, i2, iterator k1, iterator k2,
+       replace(i1, i2, const_iterator k1, const_iterator k2):
+       New specializations to optimize for the common cases of
+       pointers and iterators.
+       (replace(pos, n1, s, n2)): Tweak.
+       * include/bits/basic_string.tcc: Tweak comments.
+       * testsuite/21_strings/replace.cc (test05): New tests.
+
 2002-05-18  Benjamin Kosnik  <bkoz@redhat.com>
 
        * config/os/solaris/solaris2.5/bits/os_defines.h: Remove
index e05a1c6ef61430bae2ae2ded4b542aafd2a38bd2..a9c02ce37089ad80ca2195f2a4a47c76e6916e2c 100644 (file)
@@ -650,8 +650,11 @@ namespace std
            || 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); 
+       // Todo: optimized in-place replace.
+       else return
+              _M_replace(_M_ibegin() + __pos, _M_ibegin() + __pos + __foldn1,
+                         __s, __s + __n2,
+                         typename iterator_traits<const _CharT*>::iterator_category());
       }
 
       basic_string& 
@@ -685,6 +688,30 @@ namespace std
         { return _M_replace(__i1, __i2, __k1, __k2,
             typename iterator_traits<_InputIterator>::iterator_category()); }
 
+      // Specializations for the common case of pointer and iterator:
+      // useful to avoid the overhead of temporary buffering in _M_replace.
+      basic_string& 
+      replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
+        { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
+                              __k1, __k2 - __k1); }
+
+      basic_string& 
+      replace(iterator __i1, iterator __i2, const _CharT* __k1, const _CharT* __k2)
+        { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
+                              __k1, __k2 - __k1); }
+
+      basic_string& 
+      replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
+        { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
+                              __k1.base(), __k2 - __k1);
+       }
+
+      basic_string& 
+      replace(iterator __i1, iterator __i2, const_iterator __k1, const_iterator __k2)
+        { return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
+                              __k1.base(), __k2 - __k1);
+       }
+
     private:
       template<class _InputIterator>
         basic_string& 
index 35a2f11f7e76159adbcfb6cfab28638e23771bd8..9a6ce501b00008e50fd2f5742b74fdc45ba37be7 100644 (file)
@@ -498,13 +498,10 @@ namespace std
       // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
     }
   
-  // This is the general replace helper, which gets instantiated both
-  // for input-iterators and forward-iterators. It buffers internally and
-  // then calls _M_replace_safe. For input-iterators this is almost the
-  // best we can do, but for forward-iterators many optimizations could be
-  // conceived: f.i., when source and destination ranges do not overlap
-  // buffering is not really needed. In order to easily implement them, it
-  // could become useful to add an _M_replace(forward_iterator_tag)
+
+  // This is the general replace helper, which currently gets instantiated both
+  // for input iterators and reverse iterators. It buffers internally and then
+  // calls _M_replace_safe.
   template<typename _CharT, typename _Traits, typename _Alloc>
     template<typename _InputIter>
       basic_string<_CharT, _Traits, _Alloc>&
@@ -518,10 +515,8 @@ namespace std
       }
 
   // This is a special replace helper, which does not buffer internally
-  // and can be used in the "safe" situations involving forward-iterators,
+  // and can be used in "safe" situations involving forward iterators,
   // i.e., when source and destination ranges are known to not overlap.
-  // Presently, is called by _M_replace, by the various append and by
-  // the assigns.
   template<typename _CharT, typename _Traits, typename _Alloc>
     template<typename _ForwardIter>
       basic_string<_CharT, _Traits, _Alloc>&
index 5d1bbf9f621b2ea06ca20fab26e2541e99bf8487..944301937c57b6209c5696139f760c6480537769 100644 (file)
@@ -154,11 +154,49 @@ test04()
   VERIFY(str01 == "cyd");
 }
 
+// Some more tests for 
+// template<typename InputIter>
+//   string& replace(iterator it1, iterator it2, InputIter j1, InputIter j2)
+void
+test05()
+{
+  bool test = true;
+  std::string str01 = "geogaddi";
+  std::string str02;
+
+  typedef std::string::iterator iterator;
+  typedef std::string::const_iterator const_iterator;
+  
+  iterator it1 = str01.begin();
+  iterator it2 = str01.end();
+  str02.replace(str02.begin(), str02.end(), it1, it2);
+  VERIFY(str02 == "geogaddi");
+
+  str02 = "boards";
+  const_iterator c_it1 = str01.begin();
+  const_iterator c_it2 = str01.end();
+  str02.replace(str02.begin(), str02.end(), c_it1, c_it2);
+  VERIFY(str02 == "geogaddi");
+
+  str02 = "boards";
+  const char* c_ptr1 = str01.c_str();
+  const char* c_ptr2 = str01.c_str() + 8;
+  str02.replace(str02.begin(), str02.end(), c_ptr1, c_ptr2);
+  VERIFY(str02 == "geogaddi");
+
+  str02 = "boards";
+  char* ptr1 = &*str01.begin();
+  char* ptr2 = &*str01.end();
+  str02.replace(str02.begin(), str02.end(), ptr1, ptr2);
+  VERIFY(str02 == "geogaddi");
+}
+
 int main()
 { 
   test01();
   test02();
   test03();
   test04();
+  test05();
   return 0;
 }