]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fix basic_string::replace for integral types
authorDoug Gregor <dgregor@apple.com>
Fri, 20 Jun 2003 22:12:18 +0000 (22:12 +0000)
committerDoug Gregor <dgregor@gcc.gnu.org>
Fri, 20 Jun 2003 22:12:18 +0000 (22:12 +0000)
From-SVN: r68286

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

index ada09bf1a8c0ab3205bb0871da11fd955f34c549..8f24748ee8f9173cd1eb0659e70c01fa0b80795c 100644 (file)
@@ -1,3 +1,15 @@
+2003-06-20  Doug Gregor <dgregor@apple.com>
+
+       * include/bits/basic_string.h (basic_string::replace): Dispatch 
+       _InputIterator version based on _Is_integer.
+       * include/bits/basic_string.tcc (basic_string::replace):
+       Renamed replace(iterator, iterator, size_type, _CharT) to
+       _M_replace_aux.
+       * testsuite/21_strings/basic_string/assign/char/1.cc (test01):
+       Test basic_string::assign(_InputIterator, _InputIterator),
+       which calls basic_string::replace(iterator, iterator,
+       _Input_iterator, _InputIterator).
+
 2003-06-20  Benjamin Kosnik  <bkoz@redhat.com>
 
        * testsuite/testsuite_performance.h (resource_counter): Don't use
index c843b75efd5697db1deec4466baff3e5bde63e33..7942c7635e2090ec80984bfe0062f7cf7b751e8a 100644 (file)
@@ -625,14 +625,15 @@ namespace std
       { return this->replace(__i1, __i2, __s, traits_type::length(__s)); }
 
       basic_string&
-      replace(iterator __i1, iterator __i2, size_type __n, _CharT __c);
+      replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
+      { return _M_replace_aux(__i1, __i2, __n, __c); }
 
       template<class _InputIterator>
         basic_string&
         replace(iterator __i1, iterator __i2,
                _InputIterator __k1, _InputIterator __k2)
-        { return _M_replace(__i1, __i2, __k1, __k2,
-            typename iterator_traits<_InputIterator>::iterator_category()); }
+        { typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
+         return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral()); }
 
       // Specializations for the common case of pointer and iterator:
       // useful to avoid the overhead of temporary buffering in _M_replace.
@@ -659,6 +660,25 @@ namespace std
        }
 
     private:
+      template<class _Integer>
+       basic_string&
+       _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n, 
+                           _Integer __val, __true_type)
+        { return _M_replace_aux(__i1, __i2, __n, __val); }
+
+      template<class _InputIterator>
+       basic_string&
+       _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
+                           _InputIterator __k2, __false_type)
+        { 
+         typedef typename iterator_traits<_InputIterator>::iterator_category
+           _Category;
+         return _M_replace(__i1, __i2, __k1, __k2, _Category());
+       }
+
+      basic_string&
+      _M_replace_aux(iterator __i1, iterator __i2, size_type __n2, _CharT __c);
+
       template<class _InputIterator>
         basic_string&
         _M_replace(iterator __i1, iterator __i2, _InputIterator __k1,
index 9b83603521944321cd4bc5d716ab6ef3a43e32a6..a9496c0244a42f4858e14086305aea8da0a56e92 100644 (file)
@@ -621,6 +621,22 @@ namespace std
       // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
     }
 
+  template<typename _CharT, typename _Traits, typename _Alloc>
+    basic_string<_CharT, _Traits, _Alloc>&
+    basic_string<_CharT, _Traits, _Alloc>::
+    _M_replace_aux(iterator __i1, iterator __i2, size_type __n2, _CharT __c)
+    {
+      size_type __n1 = __i2 - __i1;
+      size_type __off1 = __i1 - _M_ibegin();
+      if (max_size() - (this->size() - __n1) <= __n2)
+       __throw_length_error("basic_string::replace");
+      _M_mutate (__off1, __n1, __n2);
+      // Invalidated __i1, __i2
+      if (__n2)
+       traits_type::assign(_M_data() + __off1, __n2, __c);
+      return *this;
+    }
+
   // 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.
@@ -760,22 +776,6 @@ namespace std
       return __str;
     }
 
-  template<typename _CharT, typename _Traits, typename _Alloc>
-    basic_string<_CharT, _Traits, _Alloc>&
-    basic_string<_CharT, _Traits, _Alloc>::
-    replace(iterator __i1, iterator __i2, size_type __n2, _CharT __c)
-    {
-      const size_type __n1 = __i2 - __i1;
-      const size_type __off1 = __i1 - _M_ibegin();
-      if (max_size() - (this->size() - __n1) <= __n2)
-       __throw_length_error("basic_string::replace");
-      _M_mutate (__off1, __n1, __n2);
-      // Invalidated __i1, __i2
-      if (__n2)
-       traits_type::assign(_M_data() + __off1, __n2, __c);
-      return *this;
-    }
-  
   template<typename _CharT, typename _Traits, typename _Alloc>
     typename basic_string<_CharT, _Traits, _Alloc>::size_type
     basic_string<_CharT, _Traits, _Alloc>::
index 735c61c86e007e0e4b9b0bd7cd385dfd0e5be3fe..879d3c139c7f547f97a51de800bdb9d6d5ccd4b6 100644 (file)
@@ -44,6 +44,9 @@ test01()
     aux.assign(aux, i + 1, string::npos);
   VERIFY(aux.c_str()[9] == 'B');
   VERIFY(aux == "/Hanalei Bay/Kauai/Hawaii");
+
+  aux.assign(10, 0);
+  VERIFY(aux.length() == 10);
 }
 
 int main()