From 5a4a879c40a08ecc539b09862dfd49435689c168 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Fri, 26 Sep 1997 03:01:01 +0000 Subject: [PATCH] bastring.h: Enable exceptions. * std/bastring.h: Enable exceptions. (assign, append, insert, replace): Implement member template versions. * tstring.cc: Add some tests for the member templates. From-SVN: r15717 --- libstdc++/ChangeLog | 5 +++ libstdc++/std/bastring.h | 83 +++++++++++++++++++++++++++++--------- libstdc++/tests/ChangeLog | 4 ++ libstdc++/tests/tstring.cc | 14 ++++++- 4 files changed, 86 insertions(+), 20 deletions(-) diff --git a/libstdc++/ChangeLog b/libstdc++/ChangeLog index 868f61d0ce5..b4bb2bb36bb 100644 --- a/libstdc++/ChangeLog +++ b/libstdc++/ChangeLog @@ -1,3 +1,8 @@ +Thu Sep 25 19:55:56 1997 Jason Merrill + + * std/bastring.h: Enable exceptions. + (assign, append, insert, replace): Implement member template versions. + 1997-09-15 02:37 Ulrich Drepper * config/linux.mt: New file. Make sure _PTHREADS is defined diff --git a/libstdc++/std/bastring.h b/libstdc++/std/bastring.h index 9070a2e857d..ba763a901c6 100644 --- a/libstdc++/std/bastring.h +++ b/libstdc++/std/bastring.h @@ -35,7 +35,7 @@ #include #include -#if _G_USE_EXCEPTIONS +#ifdef __STL_USE_EXCEPTIONS #include #define OUTOFRANGE(cond) \ @@ -149,14 +149,13 @@ public: : dat (nilRep.grab ()) { assign (s); } basic_string (size_type n, charT c) : dat (nilRep.grab ()) { assign (n, c); } -#if 0 +#ifdef __STL_MEMBER_TEMPLATES template - basic_string(InputIterator begin, InputIterator end, - Allocator& = Allocator()); + basic_string(InputIterator begin, InputIterator end) #else basic_string(const_iterator begin, const_iterator end) - : dat (nilRep.grab ()) { assign (begin, end); } #endif + : dat (nilRep.grab ()) { assign (begin, end); } ~basic_string () { rep ()->release (); } @@ -172,13 +171,13 @@ public: { return append (s, traits::length (s)); } basic_string& append (size_type n, charT c) { return replace (length (), 0, n, c); } -#if 0 +#ifdef __STL_MEMBER_TEMPLATES template - basic_string& append(InputIterator first, InputIterator last); + basic_string& append(InputIterator first, InputIterator last) #else basic_string& append(const_iterator first, const_iterator last) - { return replace (length (), 0, first, last - first); } #endif + { return replace (iend (), iend (), first, last); } basic_string& assign (const basic_string& str, size_type pos = 0, size_type n = npos) @@ -189,13 +188,13 @@ public: { return assign (s, traits::length (s)); } basic_string& assign (size_type n, charT c) { return replace (0, npos, n, c); } -#if 0 +#ifdef __STL_MEMBER_TEMPLATES template - basic_string& assign(InputIterator first, InputIterator last); + basic_string& assign(InputIterator first, InputIterator last) #else basic_string& assign(const_iterator first, const_iterator last) - { return replace (0, npos, first, last - first); } #endif + { return replace (ibegin (), iend (), first, last); } basic_string& operator= (const charT* s) { return assign (s); } @@ -222,13 +221,13 @@ public: { size_type pos = p - begin (); insert (pos, 1, c); return pos +begin (); } iterator insert(iterator p, size_type n, charT c) { size_type pos = p - begin (); insert (pos, n, c); return pos +begin (); } -#if 0 +#ifdef __STL_MEMBER_TEMPLATES template - void insert(iterator p, InputIterator first, InputIterator last); + void insert(iterator p, InputIterator first, InputIterator last) #else void insert(iterator p, const_iterator first, const_iterator last) - { size_type pos = p - begin(); insert (pos, first, last - first); } #endif + { replace (p, p, first, last); } basic_string& remove (size_type pos = 0, size_type n = npos) { return replace (pos, n, (size_type)0, (charT)0); } @@ -254,14 +253,13 @@ public: { return replace (i1 - begin (), i2 - i1, s); } basic_string& replace (iterator i1, iterator i2, size_type n, charT c) { return replace (i1 - begin (), i2 - i1, n, c); } -#if 0 +#ifdef __STL_MEMBER_TEMPLATES template basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2); #else basic_string& replace(iterator i1, iterator i2, - const_iterator j1, const_iterator j2) - { return replace (i1, i2, j1, j2 - j1); } + const_iterator j1, const_iterator j2); #endif private: @@ -360,8 +358,14 @@ public: iterator begin () { selfish (); return &(*this)[0]; } iterator end () { selfish (); return &(*this)[length ()]; } - const_iterator begin () const { return &(*rep ())[0]; } - const_iterator end () const { return &(*rep ())[length ()]; } + +private: + iterator ibegin () const { return &(*rep ())[0]; } + iterator iend () const { return &(*rep ())[length ()]; } + +public: + const_iterator begin () const { return ibegin (); } + const_iterator end () const { return iend (); } reverse_iterator rbegin() { return reverse_iterator (end ()); } const_reverse_iterator rbegin() const @@ -379,6 +383,47 @@ private: charT *dat; }; +#ifdef __STL_MEMBER_TEMPLATES +template template +basic_string & basic_string :: +replace (iterator i1, iterator i2, InputIterator j1, InputIterator j2) +#else +template +basic_string & basic_string :: +replace (iterator i1, iterator i2, const_iterator j1, const_iterator j2) +#endif +{ + const size_type len = length (); + size_type pos = i1 - ibegin (); + size_type n1 = i2 - i1; + size_type n2 = j2 - j1; + + OUTOFRANGE (pos > len); + if (n1 > len - pos) + n1 = len - pos; + LENGTHERROR (len - n1 > max_size () - n2); + size_t newlen = len - n1 + n2; + + if (check_realloc (newlen)) + { + Rep *p = Rep::create (newlen); + p->copy (0, data (), pos); + p->copy (pos + n2, data () + pos + n1, len - (pos + n1)); + for (; j1 != j2; ++j1, ++pos) + traits::assign ((*p)[pos], *j1); + repup (p); + } + else + { + rep ()->move (pos + n2, data () + pos + n1, len - (pos + n1)); + for (; j1 != j2; ++j1, ++pos) + traits::assign ((*rep ())[pos], *j1); + } + rep ()->len = newlen; + + return *this; +} + template inline basic_string operator+ (const basic_string & lhs, diff --git a/libstdc++/tests/ChangeLog b/libstdc++/tests/ChangeLog index 7f884a126e5..ac76090276e 100644 --- a/libstdc++/tests/ChangeLog +++ b/libstdc++/tests/ChangeLog @@ -1,3 +1,7 @@ +Thu Sep 25 19:48:22 1997 Jason Merrill + + * tstring.cc: Add some tests for the member templates. + Wed Jun 11 11:00:10 1997 Jason Merrill * tmap.cc: Explicitly instantiate allocator classes. diff --git a/libstdc++/tests/tstring.cc b/libstdc++/tests/tstring.cc index d4f65c55b6e..954d77fd4e2 100644 --- a/libstdc++/tests/tstring.cc +++ b/libstdc++/tests/tstring.cc @@ -14,8 +14,9 @@ // 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. - + #include +#include #include #include #include @@ -65,6 +66,10 @@ void decltest() assert(i == 20); assert(f == 20); + int ar[] = { 'H', 'e', 'l', 'l', 'o' }; + string is (ar, ar+sizeof(ar)/sizeof(ar[0])); + cout << "is = " << is << endl; + assert (is == "Hello"); } void cattest() @@ -142,6 +147,13 @@ void substrtest() x.replace (0, 1, 'j'); cout << "x.replace (0, 1, 'j'); x = " << x << "\n"; assert(x == "jello"); + + int ar[] = { 'H', 'e', 'l', 'l', 'o' }; + x.replace (find (x.begin (), x.end (), 'l'), + find (x.rbegin (), x.rend (), 'l').base (), + ar, ar+sizeof(ar)/sizeof(ar[0])); + cout << "x = " << x << endl; + assert (x == "jeHelloo"); } void iotest()