lib/mystring/mystring.h
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #ifndef MYSTRING__H__
       
    18 #define MYSTRING__H__
       
    19 
       
    20 #include <sys/types.h>
       
    21 #include "mystring/rep.h"
       
    22 
       
    23 class mystringjoin;
       
    24 class mystring
       
    25 {
       
    26   friend class mystringtmp;
       
    27   friend class mystringjoin;
       
    28 private:
       
    29   mystringrep* rep;
       
    30 
       
    31 protected:
       
    32   void dupnil();
       
    33   void dup(const char*, size_t);
       
    34   void dup(const char*);
       
    35   void append(const char*);
       
    36   void append(const char*, size_t);
       
    37   void assign(const char*);
       
    38   void assign(const char*, size_t);
       
    39 public:
       
    40   static const mystring NUL;
       
    41   
       
    42   mystring() { dupnil(); }
       
    43   mystring(const char* s) { dup(s); }
       
    44   mystring(const mystring& s) { dup(s.rep->buf, s.rep->length); }
       
    45   mystring(const char* str, size_t len) { dup(str, len); }
       
    46   mystring(const mystringjoin&);
       
    47   ~mystring();
       
    48 
       
    49   const char* c_str() const { return rep->buf; }
       
    50 
       
    51   bool operator!() const { return empty(); }
       
    52   
       
    53   char operator[](size_t i) const { return rep->buf[i]; }
       
    54   
       
    55   size_t length() const { return rep->length; }
       
    56 
       
    57   bool empty() const { return rep->length == 0; }
       
    58   
       
    59   int operator!=(const char* in) const;
       
    60   int operator!=(const mystring& in) const;
       
    61   bool operator==(const char* in) const
       
    62     {
       
    63       return !operator!=(in);
       
    64     }
       
    65   bool operator==(const mystring& in) const
       
    66     {
       
    67       return !operator!=(in);
       
    68     }
       
    69 
       
    70   void operator=(const char* in) { assign(in); }
       
    71   void operator=(const mystring& in) { assign(in.rep->buf, in.rep->length); }
       
    72   void operator=(const mystringjoin& in);
       
    73 
       
    74   mystring subst(char from, char to) const;
       
    75   
       
    76   mystring lower() const;
       
    77   mystring upper() const;
       
    78 
       
    79   int find_first(char, size_t = 0) const;
       
    80   int find_first_of(const mystring&, size_t = 0) const;
       
    81   int find_first_of(const char*, size_t = 0) const;
       
    82   int find_first_of(const char*, size_t, size_t) const;
       
    83 
       
    84   int find_last(char, size_t = (size_t)-1) const;
       
    85   int find_last_of(const mystring&, size_t = (size_t)-1) const;
       
    86   int find_last_of(const char*, size_t = 0) const;
       
    87   int find_last_of(const char*, size_t, size_t) const;
       
    88 
       
    89   mystring left(size_t) const;
       
    90   mystring right(size_t) const;
       
    91   mystring sub(size_t, size_t) const;
       
    92 
       
    93   mystring lstrip() const;
       
    94   mystring rstrip() const;
       
    95   mystring strip() const;
       
    96 
       
    97   unsigned count(char ch) const;
       
    98   
       
    99   void operator+=(const mystring& str) {append(str.rep->buf, str.rep->length);}
       
   100   void operator+=(const char* str) { append(str); }
       
   101   void operator+=(char ch)
       
   102     {
       
   103       char str[2] = { ch, 0 };
       
   104       append(str, 1);
       
   105     }
       
   106 };
       
   107 
       
   108 #ifndef MYSTRING_TRACE
       
   109 inline mystring::~mystring()
       
   110 {
       
   111   rep->detach();
       
   112 }
       
   113 #endif
       
   114 
       
   115 #include "mystring/iter.h"
       
   116 #include "mystring/join.h"
       
   117 
       
   118 class fdobuf;
       
   119 fdobuf& operator<<(fdobuf& out, const mystring& str);
       
   120 
       
   121 //istream& operator>>(istream& in, mystring& str);
       
   122 
       
   123 typedef mystring string;
       
   124 
       
   125 #endif