home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / lib / g++-include / bastring.hi < prev    next >
Text File  |  1994-12-22  |  15KB  |  458 lines

  1. // Main templates for the -*- C++ -*- string classes.
  2. // Copyright (C) 1994 Free Software Foundation
  3.  
  4. // This file is part of the GNU ANSI C++ Library.  This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 2, or (at your option)
  8. // any later version.
  9.  
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with GNU CC; see the file COPYING.  If not, write to
  17. // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. // As a special exception, if you link this library with files
  20. // compiled with a GNU compiler to produce an executable, this does not cause
  21. // the resulting executable to be covered by the GNU General Public License.
  22. // This exception does not however invalidate any other reasons why
  23. // the executable file might be covered by the GNU General Public License.
  24.  
  25. // Written by Jason Merrill based upon the specification by Takanori Adachi
  26. // in ANSI X3J16/94-0013R2.
  27.  
  28. #ifdef __GNUG__
  29. #pragma interface
  30. #endif
  31.  
  32. #include <stddef>
  33. #include <straits.hI>
  34.  
  35. #include <cassert>
  36. #define OUTOFRANGE(cond) assert (!(cond))
  37. #define LENGTHERROR(cond) assert (!(cond))
  38.  
  39. class istream; class ostream;
  40.  
  41. // Should be a nested class basic_string<charT, traits>::Rep, but nested
  42. // classes don't work well with templates in g++.
  43. template <class charT, class traits = string_char_traits<charT> >
  44. struct __bsrep {
  45.   typedef __bsrep Rep;
  46.  
  47.   size_t len, res;
  48.   unsigned char ref;
  49.  
  50.   charT* data () { return (charT *)(this + 1); }
  51.   charT& operator[] (size_t s) { return data () [s]; }
  52.   Rep* grab () { ++ref; return this; }
  53.   void release () { if (--ref == 0) delete this; }
  54.  
  55.   inline static void * operator new (size_t, size_t);
  56.   inline static Rep* create (size_t);
  57.  
  58.   inline void copy (size_t, const charT *, size_t);
  59.   inline void move (size_t, const charT *, size_t);
  60.   inline void set  (size_t, const charT,   size_t);
  61.  
  62. #ifdef _G_ALLOC_CONTROL
  63.   // These function pointers allow you to modify the allocation policy used
  64.   // by the string classes.  By default they expand by powers of two, but
  65.   // this may be excessive for space-critical applications.
  66.  
  67.   // Returns true if ALLOCATED is too much larger than LENGTH
  68.   static bool (*excess_slop) (size_t length, size_t allocated);
  69.   inline static bool default_excess (size_t, size_t);
  70.  
  71.   // Returns a good amount of space to allocate for a string of length LENGTH
  72.   static size_t (*frob_size) (size_t length);
  73.   inline static size_t default_frob (size_t);
  74. #else
  75.   inline static bool excess_slop (size_t, size_t);
  76.   inline static size_t frob_size (size_t);
  77. #endif
  78.  
  79. private:
  80.   Rep &operator= (const Rep &);
  81. };
  82.  
  83. // #include <iterator.h>
  84.  
  85. template <class charT, class traits = string_char_traits<charT> >
  86. class basic_string {
  87. public:
  88.   typedef charT char_type;
  89.   typedef traits traits_type;
  90.   typedef __bsrep<charT, traits> Rep;
  91.  
  92.   const charT* data () const
  93.     { return rep->data(); }
  94.   size_t length () const
  95.     { return rep->len; }
  96.   size_t reserve () const
  97.     { return rep->res; }
  98.  
  99.   basic_string& operator= (const basic_string& str)
  100.     { rep->release (); rep = str.rep->grab (); return *this; }
  101.  
  102.   basic_string (): rep (nilRep.grab ()) { }
  103.   basic_string (const basic_string& str): rep (str.rep->grab ()) { }
  104.   basic_string (size_t size, capacity cap);
  105.   basic_string (const basic_string& str, size_t pos, size_t n = NPOS)
  106.     : rep (nilRep.grab ()) { assign (str, pos, n); }
  107.   basic_string (const charT* s, size_t n)
  108.     : rep (nilRep.grab ()) { assign (s, n); }
  109.   basic_string (const charT* s)
  110.     : rep (nilRep.grab ()) { assign (s); }
  111.   basic_string (charT c, size_t n = 1)
  112.     : rep (nilRep.grab ()) { assign (c, n); }
  113.  
  114.  
  115.   ~basic_string ()
  116.     { rep->release (); }
  117.  
  118.   basic_string& append (const basic_string& str, size_t pos = 0,
  119.             size_t n = NPOS)
  120.     { return replace (length (), 0, str, pos, n); }
  121.   basic_string& append (const charT* s, size_t n)
  122.     { return replace (length (), 0, s, n); }
  123.   basic_string& append (const charT* s)
  124.     { return append (s, traits::length (s)); }
  125.   basic_string& append (charT c, size_t n = 1)
  126.     { return replace (length (), 0, c, n); }
  127.  
  128.   basic_string& assign (const basic_string& str, size_t pos = 0,
  129.             size_t n = NPOS)
  130.     { return replace (0, NPOS, str, pos, n); }
  131.   basic_string& assign (const charT* s, size_t n)
  132.     { return replace (0, NPOS, s, n); }
  133.   basic_string& assign (const charT* s)
  134.     { return assign (s, traits::length (s)); }
  135.   basic_string& assign (charT c, size_t n = 1)
  136.     { return replace (0, NPOS, c, n); }
  137.  
  138.   basic_string& operator= (const charT* s)
  139.     { return assign (s); }
  140.   basic_string& operator= (charT c)
  141.     { return assign (c); }
  142.  
  143.   basic_string& operator+= (const basic_string& rhs)
  144.     { return append (rhs); }
  145.   basic_string& operator+= (const charT* s)
  146.     { return append (s); }
  147.   basic_string& operator+= (charT c)
  148.     { return append (c); }
  149.  
  150.   basic_string& insert (size_t pos1, const basic_string& str,
  151.             size_t pos2 = 0, size_t n = NPOS)
  152.     { return replace (pos1, 0, str, pos2, NPOS); }
  153.   basic_string& insert (size_t pos, const charT* s, size_t n)
  154.     { return replace (pos, 0, s, n); }
  155.   basic_string& insert (size_t pos, const charT* s)
  156.     { return insert (pos, s, traits::length (s)); }
  157.   basic_string& insert (size_t pos, charT c, size_t n = 1)
  158.     { return replace (pos, 0, c, n); }
  159.  
  160.   basic_string& remove (size_t pos = 0, size_t n = NPOS)
  161.     { return replace (pos, n, (const charT *)0, 0); }
  162.  
  163.   basic_string& replace (size_t pos1, size_t n1, const basic_string& str,
  164.              size_t pos2 = 0, size_t n2 = NPOS);
  165.   basic_string& replace (size_t pos, size_t n1, const charT* s, size_t n2);
  166.   basic_string& replace (size_t pos, size_t n1, const charT* s)
  167.     { return replace (pos, n1, s, traits::length (s)); }
  168.   basic_string& replace (size_t pos, size_t n, charT c, size_t n = 1);
  169.  
  170.   charT operator[] (size_t pos) const
  171.     {
  172.       if (pos == length ())
  173.     return eos ();
  174.       return data ()[pos];
  175.     }
  176. private:
  177.   void unique () { if (rep->ref > 1) alloc (reserve (), true); }
  178.  
  179. public:
  180.   charT& operator[] (size_t pos)
  181.     { unique (); return (*rep)[pos]; }
  182.  
  183.   charT get_at (size_t pos) const
  184.     {
  185.       OUTOFRANGE (pos >= length ());
  186.       return data ()[pos];
  187.     }
  188.   void put_at (size_t pos, charT c)
  189.     {
  190.       OUTOFRANGE (pos > length ());
  191.       if (pos == length ())
  192.     append (c);
  193.       else
  194.     (*this)[pos] = c;
  195.     }
  196. private:
  197.   static charT eos () { return traits::eos (); }
  198.   void terminate () const
  199.     {
  200.       if (reserve () < length () + 1)
  201.     alloc (length () + 1, true);
  202.       traits::assign ((*rep)[length ()], eos ());
  203.     }
  204.  
  205. public:
  206.   const charT* c_str () const
  207.     { terminate (); return data (); }
  208.   void resize (size_t n, charT c);
  209.   void resize (size_t n)
  210.     { resize (n, eos ()); }
  211.   void reserve (size_t s) { }
  212.  
  213.   size_t copy (charT* s, size_t n, size_t pos = 0);
  214.  
  215.   size_t find (const basic_string& str, size_t pos = 0) const
  216.     { return find (str.data(), pos, str.length()); }
  217.   size_t find (const charT* s, size_t pos, size_t n) const;
  218.   size_t find (const charT* s, size_t pos = 0) const
  219.     { return find (s, pos, traits::length (s)); }
  220.   size_t find (charT c, size_t pos = 0) const;
  221.  
  222.   size_t rfind (const basic_string& str, size_t pos = NPOS) const
  223.     { return rfind (str.data(), pos, str.length()); }
  224.   size_t rfind (const charT* s, size_t pos, size_t n) const;
  225.   size_t rfind (const charT* s, size_t pos = NPOS) const
  226.     { return rfind (s, pos, traits::length (s)); }
  227.   size_t rfind (charT c, size_t pos = NPOS) const;
  228.  
  229.   size_t find_first_of (const basic_string& str, size_t pos = 0) const
  230.     { return find_first_of (str.data(), pos, str.length()); }
  231.   size_t find_first_of (const charT* s, size_t pos, size_t n) const;
  232.   size_t find_first_of (const charT* s, size_t pos = 0) const
  233.     { return find_first_of (s, pos, traits::length (s)); }
  234.   size_t find_first_of (charT c, siz