summaryrefslogtreecommitdiff
path: root/src/mt/stringref.h
blob: a66ac2105ad3edd96eb6a0a19e6a0aefc1fb2ec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef SREFH
#define SREFH

#include <mt/charset.h>
#include <stdlib.h>

typedef char sref_t[1024];

class sref {
public:
  sref() : map((char*)&dim),dim(0) {}
  explicit sref(char* p) : map(p),dim(strlen(p)) {}
  explicit sref(const char* p) : map((char*)p),dim(strlen(p)) {}
  sref(char* p,int n) : map(p),dim(n) {}
  sref(const char* p,int n) : map((char*)p),dim(n) {}
  
  int size() const { return dim; }
  bool empty() const { return dim==0; }
  bool nempty() const { return dim!=0; }
  
  char* data() { return map; }
  const char* data() const { return map; }

  char& operator[](int pos) { return map[pos]; }
  const char& operator[](int pos) const { return map[pos]; }
  
  char* begin() { return map; }
  const char* begin() const { return map; }

  char* end() { return map+dim; }
  const char* end() const { return map+dim; }
  
  int operator*() const { return dim?*map:0; }
  int front() const { return dim?*map:0; }
  int back() const { return dim?map[dim-1]:0; }

  ////////////////////////////////////////////////////////////////

  sref popf() const { return dim?sref(map+1,dim-1):*this; }
  sref popb() const { return dim?sref(map,dim-1):*this; }
  
  sref adv(int n) const { return n>0?(n<dim?sref(map+n,dim-n):sref(map+dim,0)):*this; }

  // NOTE that these functions are LOW LEVEL - no checks!

  sref adv(const sref& s) const { return sref(s.end(),end()-s.end()); }
  sref operator()(char* p) { return sref(p,map+dim-p); }
  const sref operator()(const char* p) const { return sref((char*)p,map+dim-p); }

  ////////////////////////////////////////////////////////////////

  char* news() const {char* s=new char[dim+1];s[dim]=0;return (char*)memcpy(s,map,dim); }
  
  void clear() {dim=0;}

  ////////////////////////////////////////////////////////////////

  void mcopy(const sref& s) {
    if(s.dim<dim) memmove(map,s.map,dim=s.dim); else memmove(map,s.map,dim);
  }
  
  void tolower();
  void toupper();
  void tosim(const charmap& s=cset_sim);
  
  ////////////////////////////////////////////////////////////////

  char* copyto(char* buf,int max) const;
  char* copyto(sref_t& buf) const { return copyto(buf,sizeof(buf)); }
  
  int compare(const sref& s) const {
    int c; if(map==s.map) return dim-s.dim;
    else return (c=memcmp(map,s.map,dim<s.dim?dim:s.dim))?c:dim-s.dim;
  }
  int compare(const sref& s,const charmap& tab) const;

  ////////////////////////////////////////////////////////////////

  sref operator()(int pos,int n=-1) const;
  sref left(int n) const;
  sref leftand(int n,int m) const;
  sref right(int n) const;
  sref past(int n,int m) const;

  sref past_nl() const { return past(find('\n'),1); }

  ////////////////////////////////////////////////////////////////

  int find(char c) const;
  sref left_first(char c) const { return left(find(c)); }
  sref leftand_first(char c) const { return leftand(find(c),1); }
  sref right_first(char c) const { return right(find(c)); }
  sref past_first(char c) const { return past(find(c),1); }
  
  int rfind(char c) const;
  sref left_last(char c) const { return left(rfind(c)); }
  sref leftand_last(char c) const { return leftand(rfind(c),1); }
  sref right_last(char c) const { return right(rfind(c)); }
  sref past_last(char c) const { return past(rfind(c),1); }
  
  int find(const sref& s) const;
  sref left_first(const sref& s) const { return left(find(s)); }
  sref leftand_first(const sref& s) const { return leftand(find(s),s.size()); }
  sref right_first(const sref& s) const { return right(find(s)); }
  sref past_first(const sref& s) const { return past(find(s),s.size()); }
  
  int rfind(const sref& s) const;
  sref left_last(const sref& s) const { return left(rfind(s)); }
  sref leftand_last(const sref& s) const { return leftand(rfind(s),s.size()); }
  sref right_last(const sref& s) const { return right(rfind(s)); }
  sref past_last(const sref& s) const { return past(rfind(s),s.size()); }
  
  int find(const sref& s,const charmap& tab) const;
  sref left_first(const sref& s,const charmap& tab) const { return left(find(s,tab)); }
  sref leftand_first(const sref& s,const charmap& tab)const{
    return leftand(find(s,tab),s.size());
  }
  sref right_first(const sref& s,const charmap& tab) const { return right(find(s,tab)); }
  sref past_first(const sref& s,const charmap& tab) const {
    return past(find(s,tab),s.size());
  }
  
  int rfind(const sref& s,const charmap& tab) const;
  sref left_last(const sref& s,const charmap& tab) const { return left(rfind(s,tab)); }
  sref leftand_last(const sref& s,const charmap& tab)const{
    return leftand(rfind(s,tab),s.size());
  }
  sref right_last(const sref& s,const charmap& tab) const { return right(rfind(s,tab)); }
  sref past_last(const sref& s,const charmap& tab) const {
    return past(rfind(s,tab),s.size());
  }
  
  int find_of(const charset& s) const;
  sref left_first_of(const charset& s) const { return left(find_of(s)); }
  sref leftand_first_of(const charset& s) const { return leftand(find_of(s),1); }
  sref right_first_of(const charset& s) const { return right(find_of(s)); }
  sref past_first_of(const charset& s) const { return past(find_of(s),1); }

  int rfind_of(const charset& s) const;
  sref left_last_of(const charset& s) const { return left(rfind_of(s)); }
  sref leftand_last_of(const charset& s) const { return leftand(rfind_of(s),1); }
  sref right_last_of(const charset& s) const { return right(rfind_of(s)); }
  sref past_last_of(const charset& s) const { return past(rfind_of(s),1); }
  
  int find_not_of(const charset& s) const;
  sref left_first_not_of(const charset& s) const { return left(find_not_of(s)); }
  sref leftand_first_not_of(const charset& s) const { return leftand(find_not_of(s),1); }
  sref right_first_not_of(const charset& s) const { return right(find_not_of(s)); }
  sref past_first_not_of(const charset& s) const { return past(find_not_of(s),1); }

  int rfind_not_of(const charset& s) const;
  sref left_last_not_of(const charset& s) const { return left(rfind_not_of(s)); }
  sref leftand_last_not_of(const charset& s) const { return leftand(rfind_not_of(s),1); }
  sref right_last_not_of(const charset& s) const { return right(rfind_not_of(s)); }
  sref past_last_not_of(const charset& s) const { return past(rfind_not_of(s),1); }
  
  int skip(const bracket& bra) const;
  
  ////////////////////////////////////////////////////////////////

  int save(const sref& path,int prot=0600) const;

protected:
  char* map;
  int dim;
};
  
////////////////////////////////////////////////////

inline bool operator<(const sref& a,const sref& b) {
  return a.compare(b)<0;
}
inline bool operator<=(const sref& a,const sref& b) {
  return a.compare(b)<=0;
}
inline bool operator==(const sref& a,const sref& b) {
  return a.compare(b)==0;
}
inline bool operator!=(const sref& a,const sref& b) {
  return a.compare(b)!=0;
}
inline bool operator>=(const sref& a,const sref& b) {
  return a.compare(b)>=0;
}
inline bool operator>(const sref& a,const sref& b) {
  return a.compare(b)>0;
}

//////////////////////////////////////////////////////////////////////////

inline int operator-(const sref& a,const sref& b) { return a.data()-b.data(); }

//////////////////////////////////////////////////////////////////////////

inline int ltn(const sref& a,const sref& b) {
  return a.compare(b,cset_lcase)<0;
}
inline int len(const sref& a,const sref& b) {
  return a.compare(b,cset_lcase)<=0;
}
inline int eqn(const sref& a,const sref& b) {
  return a.compare(b,cset_lcase)==0;
}
inline int nen(const sref& a,const sref& b) {
  return a.compare(b,cset_lcase)!=0;
}
inline int gen(const sref& a,const sref& b) {
  return a.compare(b,cset_lcase)>=0;
}
inline int gtn(const sref& a,const sref& b) {
  return a.compare(b,cset_lcase)>0;
}

//////////////////////////////////////////////////////////////////////////

inline int atob(const sref& s) { sref_t buf; return atoi(s.copyto(buf))!=0; }
inline int atoi(const sref& s) { sref_t buf; return atoi(s.copyto(buf)); }
inline double atof(const sref& s) { sref_t buf; return atof(s.copyto(buf)); }

#endif