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