1 /* $Id: mystring.h 635 2005-11-02 17:37:50Z bruce $ */
2 // Copyright (C) 2018 Bruce Guenter <bruce@untroubled.org>
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 assign(const char*);
37 void assign(const char*, size_t);
38 public:
39 static const mystring NUL;
40
mystring()41 mystring() { dupnil(); }
mystring(const char * s)42 mystring(const char* s) { dup(s); }
mystring(const mystring & s)43 mystring(const mystring& s) { dup(s.rep->buf, s.rep->length); }
mystring(const char * str,size_t len)44 mystring(const char* str, size_t len) { dup(str, len); }
45 mystring(const mystringjoin&);
46 ~mystring();
47
c_str()48 const char* c_str() const { return rep->buf; }
49
50 bool operator!() const { return empty(); }
51
52 char operator[](size_t i) const { return rep->buf[i]; }
53
length()54 size_t length() const { return rep->length; }
55
empty()56 bool empty() const { return rep->length == 0; }
57
58 int operator!=(const char* in) const;
59 int operator!=(const mystring& in) const;
60 bool operator==(const char* in) const
61 {
62 return !operator!=(in);
63 }
64 bool operator==(const mystring& in) const
65 {
66 return !operator!=(in);
67 }
68
69 void operator=(const char* in) { assign(in); }
70 void operator=(const mystring& in) { assign(in.rep->buf, in.rep->length); }
71 void operator=(const mystringjoin& in);
72
73 mystring subst(char from, char to) const;
74
75 mystring lower() const;
76 mystring upper() const;
77
78 bool starts_with(const mystring&) const;
79 bool starts_with(const char*) const;
80 bool starts_with(const char*, size_t) const;
81
82 int find_first(char, size_t = 0) const;
83 int find_first_of(const mystring&, size_t = 0) const;
84 int find_first_of(const char*, size_t = 0) const;
85 int find_first_of(const char*, size_t, size_t) const;
86
87 int find_last(char, size_t = (size_t)-1) const;
88 int find_last_of(const mystring&, size_t = (size_t)-1) const;
89 int find_last_of(const char*, size_t = 0) const;
90 int find_last_of(const char*, size_t, size_t) const;
91
92 mystring left(size_t) const;
93 mystring right(size_t) const;
94 mystring sub(size_t, size_t) const;
95
96 mystring lstrip() const;
97 mystring rstrip() const;
98 mystring strip() const;
99
100 unsigned count(char ch) const;
101
102 void append(const char*);
103 void append(const char*, size_t);
104
105 void operator+=(const mystring& str) {append(str.rep->buf, str.rep->length);}
106 void operator+=(const char* str) { append(str); }
107 void operator+=(char ch)
108 {
109 char str[2] = { ch, 0 };
110 append(str, 1);
111 }
112 };
113
114 #ifndef MYSTRING_TRACE
~mystring()115 inline mystring::~mystring()
116 {
117 rep->detach();
118 }
119 #endif
120
121 #include "mystring/iter.h"
122 #include "mystring/join.h"
123
124 class fdobuf;
125 fdobuf& operator<<(fdobuf& out, const mystring& str);
126
127 //istream& operator>>(istream& in, mystring& str);
128
129 typedef mystring string;
130
131 #endif
132