summaryrefslogtreecommitdiff
path: root/src/mt/stringref.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mt/stringref.h')
-rw-r--r--src/mt/stringref.h239
1 files changed, 239 insertions, 0 deletions
diff --git a/src/mt/stringref.h b/src/mt/stringref.h
new file mode 100644
index 0000000..a66ac21
--- /dev/null
+++ b/src/mt/stringref.h
@@ -0,0 +1,239 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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 this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef SREFH
21#define SREFH
22
23#include <mt/charset.h>
24#include <stdlib.h>
25
26typedef char sref_t[1024];
27
28class sref {
29public:
30 sref() : map((char*)&dim),dim(0) {}
31 explicit sref(char* p) : map(p),dim(strlen(p)) {}
32 explicit sref(const char* p) : map((char*)p),dim(strlen(p)) {}
33 sref(char* p,int n) : map(p),dim(n) {}
34 sref(const char* p,int n) : map((char*)p),dim(n) {}
35
36 int size() const { return dim; }
37 bool empty() const { return dim==0; }
38 bool nempty() const { return dim!=0; }
39
40 char* data() { return map; }
41 const char* data() const { return map; }
42
43 char& operator[](int pos) { return map[pos]; }
44 const char& operator[](int pos) const { return map[pos]; }
45
46 char* begin() { return map; }
47 const char* begin() const { return map; }
48
49 char* end() { return map+dim; }
50 const char* end() const { return map+dim; }
51
52 int operator*() const { return dim?*map:0; }
53 int front() const { return dim?*map:0; }
54 int back() const { return dim?map[dim-1]:0; }
55
56 ////////////////////////////////////////////////////////////////
57
58 sref popf() const { return dim?sref(map+1,dim-1):*this; }
59 sref popb() const { return dim?sref(map,dim-1):*this; }
60
61 sref adv(int n) const { return n>0?(n<dim?sref(map+n,dim-n):sref(map+dim,0)):*this; }
62
63 // NOTE that these functions are LOW LEVEL - no checks!
64
65 sref adv(const sref& s) const { return sref(s.end(),end()-s.end()); }
66 sref operator()(char* p) { return sref(p,map+dim-p); }
67 const sref operator()(const char* p) const { return sref((char*)p,map+dim-p); }
68
69 ////////////////////////////////////////////////////////////////
70
71 char* news() const {char* s=new char[dim+1];s[dim]=0;return (char*)memcpy(s,map,dim); }
72
73 void clear() {dim=0;}
74
75 ////////////////////////////////////////////////////////////////
76
77 void mcopy(const sref& s) {
78 if(s.dim<dim) memmove(map,s.map,dim=s.dim); else memmove(map,s.map,dim);
79 }
80
81 void tolower();
82 void toupper();
83 void tosim(const charmap& s=cset_sim);
84
85 ////////////////////////////////////////////////////////////////
86
87 char* copyto(char* buf,int max) const;
88 char* copyto(sref_t& buf) const { return copyto(buf,sizeof(buf)); }
89
90 int compare(const sref& s) const {
91 int c; if(map==s.map) return dim-s.dim;
92 else return (c=memcmp(map,s.map,dim<s.dim?dim:s.dim))?c:dim-s.dim;
93 }
94 int compare(const sref& s,const charmap& tab) const;
95
96 ////////////////////////////////////////////////////////////////
97
98 sref operator()(int pos,int n=-1) const;
99 sref left(int n) const;
100 sref leftand(int n,int m) const;
101 sref right(int n) const;
102 sref past(int n,int m) const;
103
104 sref past_nl() const { return past(find('\n'),1); }
105
106 ////////////////////////////////////////////////////////////////
107
108 int find(char c) const;
109 sref left_first(char c) const { return left(find(c)); }
110 sref leftand_first(char c) const { return leftand(find(c),1); }
111 sref right_first(char c) const { return right(find(c)); }
112 sref past_first(char c) const { return past(find(c),1); }
113
114 int rfind(char c) const;
115 sref left_last(char c) const { return left(rfind(c)); }
116 sref leftand_last(char c) const { return leftand(rfind(c),1); }
117 sref right_last(char c) const { return right(rfind(c)); }
118 sref past_last(char c) const { return past(rfind(c),1); }
119
120 int find(const sref& s) const;
121 sref left_first(const sref& s) const { return left(find(s)); }
122 sref leftand_first(const sref& s) const { return leftand(find(s),s.size()); }
123 sref right_first(const sref& s) const { return right(find(s)); }
124 sref past_first(const sref& s) const { return past(find(s),s.size()); }
125
126 int rfind(const sref& s) const;
127 sref left_last(const sref& s) const { return left(rfind(s)); }
128 sref leftand_last(const sref& s) const { return leftand(rfind(s),s.size()); }
129 sref right_last(const sref& s) const { return right(rfind(s)); }
130 sref past_last(const sref& s) const { return past(rfind(s),s.size()); }
131
132 int find(const sref& s,const charmap& tab) const;
133 sref left_first(const sref& s,const charmap& tab) const { return left(find(s,tab)); }
134 sref leftand_first(const sref& s,const charmap& tab)const{
135 return leftand(find(s,tab),s.size());
136 }
137 sref right_first(const sref& s,const charmap& tab) const { return right(find(s,tab)); }
138 sref past_first(const sref& s,const charmap& tab) const {
139 return past(find(s,tab),s.size());
140 }
141
142 int rfind(const sref& s,const charmap& tab) const;
143 sref left_last(const sref& s,const charmap& tab) const { return left(rfind(s,tab)); }
144 sref leftand_last(const sref& s,const charmap& tab)const{
145 return leftand(rfind(s,tab),s.size());
146 }
147 sref right_last(const sref& s,const charmap& tab) const { return right(rfind(s,tab)); }
148 sref past_last(const sref& s,const charmap& tab) const {
149 return past(rfind(s,tab),s.size());
150 }
151
152 int find_of(const charset& s) const;
153 sref left_first_of(const charset& s) const { return left(find_of(s)); }
154 sref leftand_first_of(const charset& s) const { return leftand(find_of(s),1); }
155 sref right_first_of(const charset& s) const { return right(find_of(s)); }
156 sref past_first_of(const charset& s) const { return past(find_of(s),1); }
157
158 int rfind_of(const charset& s) const;
159 sref left_last_of(const charset& s) const { return left(rfind_of(s)); }
160 sref leftand_last_of(const charset& s) const { return leftand(rfind_of(s),1); }
161 sref right_last_of(const charset& s) const { return right(rfind_of(s)); }
162 sref past_last_of(const charset& s) const { return past(rfind_of(s),1); }
163
164 int find_not_of(const charset& s) const;
165 sref left_first_not_of(const charset& s) const { return left(find_not_of(s)); }
166 sref leftand_first_not_of(const charset& s) const { return leftand(find_not_of(s),1); }
167 sref right_first_not_of(const charset& s) const { return right(find_not_of(s)); }
168 sref past_first_not_of(const charset& s) const { return past(find_not_of(s),1); }
169
170 int rfind_not_of(const charset& s) const;
171 sref left_last_not_of(const charset& s) const { return left(rfind_not_of(s)); }
172 sref leftand_last_not_of(const charset& s) const { return leftand(rfind_not_of(s),1); }
173 sref right_last_not_of(const charset& s) const { return right(rfind_not_of(s)); }
174 sref past_last_not_of(const charset& s) const { return past(rfind_not_of(s),1); }
175
176 int skip(const bracket& bra) const;
177
178 ////////////////////////////////////////////////////////////////
179
180 int save(const sref& path,int prot=0600) const;
181
182protected:
183 char* map;
184 int dim;
185};
186
187////////////////////////////////////////////////////
188
189inline bool operator<(const sref& a,const sref& b) {
190 return a.compare(b)<0;
191}
192inline bool operator<=(const sref& a,const sref& b) {
193 return a.compare(b)<=0;
194}
195inline bool operator==(const sref& a,const sref& b) {
196 return a.compare(b)==0;
197}
198inline bool operator!=(const sref& a,const sref& b) {
199 return a.compare(b)!=0;
200}
201inline bool operator>=(const sref& a,const sref& b) {
202 return a.compare(b)>=0;
203}
204inline bool operator>(const sref& a,const sref& b) {
205 return a.compare(b)>0;
206}
207
208//////////////////////////////////////////////////////////////////////////
209
210inline int operator-(const sref& a,const sref& b) { return a.data()-b.data(); }
211
212//////////////////////////////////////////////////////////////////////////
213
214inline int ltn(const sref& a,const sref& b) {
215 return a.compare(b,cset_lcase)<0;
216}
217inline int len(const sref& a,const sref& b) {
218 return a.compare(b,cset_lcase)<=0;
219}
220inline int eqn(const sref& a,const sref& b) {
221 return a.compare(b,cset_lcase)==0;
222}
223inline int nen(const sref& a,const sref& b) {
224 return a.compare(b,cset_lcase)!=0;
225}
226inline int gen(const sref& a,const sref& b) {
227 return a.compare(b,cset_lcase)>=0;
228}
229inline int gtn(const sref& a,const sref& b) {
230 return a.compare(b,cset_lcase)>0;
231}
232
233//////////////////////////////////////////////////////////////////////////
234
235inline int atob(const sref& s) { sref_t buf; return atoi(s.copyto(buf))!=0; }
236inline int atoi(const sref& s) { sref_t buf; return atoi(s.copyto(buf)); }
237inline double atof(const sref& s) { sref_t buf; return atof(s.copyto(buf)); }
238
239#endif