OSDN Git Service

reduce regex's.
[hmh/hhml.git] / lib / util_splitter.h
1 #ifndef UTIL_SPLITTER_H
2 #define UTIL_SPLITTER_H
3
4 #include "ustring.h"
5 #include "util_string.h"
6
7 class  Splitter {
8  public:
9     uiterator  b;               // 先頭
10     uiterator  t;               // 区切り文字列先頭
11     uiterator  u;               // 区切り文字列末尾
12     uiterator  e;               // 末尾
13
14     Splitter (uiterator _begin, uiterator _end) {
15         b = t = u = _begin;
16         e = _end;
17     };
18     virtual  ~Splitter () {};
19
20     virtual bool  isEnd () {
21         return b == e;
22     };
23     virtual uiterator  begin () {
24         return b;
25     };
26     virtual uiterator  end () {
27         return t;
28     };
29     virtual ustring  pre () {
30         return ustring (b, t);
31     };
32     virtual size_t  preSize () {
33         return t - b;
34     };
35     virtual uiterator  matchBegin () {
36         return t;
37     };
38     virtual uiterator  matchEnd () {
39         return u;
40     };
41     virtual uiterator  eol () {
42         return e;
43     };
44     virtual void  rewind (int i) {
45         int  n = u - t;
46         if (n > i) {
47             u -= i;
48         } else {
49             u -= n;
50         }
51     };
52     virtual void  shiftCursor () {
53         b = u;
54     };
55     virtual bool  next () = 0;
56     virtual bool  nextSep () = 0;
57 };
58
59 class  SplitterRe: public Splitter {
60  public:
61     uregex*  re;
62     umatch  m;
63
64     SplitterRe (const ustring& text, uregex& _re): Splitter (text.begin (), text.end ()) {
65         re = &_re;
66     };
67     SplitterRe (uiterator _begin, uiterator _end, uregex& _re): Splitter (_begin, _end) {
68         re = &_re;
69     };
70     virtual  ~SplitterRe () {};
71
72     virtual bool  next () {
73         b = u;
74         if (b < e) {
75             if (usearch (b, e, m, *re)) {
76                 t = m[0].first;
77                 u = m[0].second;
78             } else {
79                 t = e;
80                 u = e;
81             }
82             return true;
83         } else {
84             return false;
85         }
86     };
87     virtual bool  nextSep () {
88         b = u;
89         if (b < e) {
90             if (usearch (b, e, m, *re)) {
91                 t = m[0].first;
92                 u = m[0].second;
93                 return true;
94             } else {
95                 t = u = e;
96                 return false;
97             }
98         } else {
99             t = u = e;
100             return false;
101         }
102     };
103     virtual bool  match (int index) {
104         return (t != u && m[index].matched);
105     }
106     virtual uiterator  matchBegin () {
107         return t;
108     };
109     virtual uiterator  matchEnd () {
110         return u;
111     };
112     virtual uiterator  matchBegin (int index) {
113         return m[index].first;
114     };
115     virtual uiterator  matchEnd (int index) {
116         return m[index].second;
117     };
118     virtual bool  nextSearch () {
119         if (u != e) {
120             if (usearch (u, e, m, *re)) {
121                 t = m[0].first;
122                 u = m[0].second;
123                 return true;
124             } else {
125                 t = e;
126                 u = e;
127                 return false;
128             }
129         } else {
130             t = e;
131             u = e;
132             return false;
133         }
134     };
135 };
136
137 class  SplitterCh: public Splitter {
138  public:
139     int  ch;
140
141     SplitterCh (uiterator _begin, uiterator _end, int _ch): Splitter (_begin, _end) {
142         ch = _ch;
143     };
144     SplitterCh (const ustring& text, int _ch): Splitter (text.begin (), text.end ()) {
145         ch = _ch;
146     };
147     virtual  ~SplitterCh () {};
148
149     virtual bool  next () {
150         b = t = u;
151         if (b < e) {
152             if (findChar (t, e, ch)) {
153                 u = t + 1;
154             } else {
155                 u = e;
156             }
157             return true;
158         } else {
159             return false;
160         }
161     };
162     virtual bool  nextSep () {
163         b = t = u;
164         if (b < e) {
165             if (findChar (t, e, ch)) {
166                 u = t + 1;
167                 return true;
168             } else {
169                 u = e;
170                 return false;
171             }
172         } else {
173             t = u = e;
174             return false;
175         }
176     };
177 };
178
179 class  SplitterChars: public Splitter {
180  public:
181     ustring  pattern;
182
183     SplitterChars (uiterator _begin, uiterator _end, const ustring& _pat): Splitter (_begin, _end) {
184         pattern = _pat;
185     };
186     SplitterChars (const ustring& text, const ustring& _pat): Splitter (text.begin (), text.end ()) {
187         pattern = _pat;
188     };
189     virtual  ~SplitterChars () {};
190
191     virtual bool  next () {
192         b = t = u;
193         if (b < e) {
194             if (findChars (t, e, pattern)) {
195                 u = t + 1;
196             } else {
197                 u = e;
198             }
199             return true;
200         } else {
201             return false;
202         }
203     };
204     virtual bool  nextSep () {
205         b = t = u;
206         if (b < e) {
207             if (findChars (t, e, pattern)) {
208                 u = t + 1;
209                 return true;
210             } else {
211                 u = e;
212                 return false;
213             }
214         } else {
215             t = u = e;
216             return false;
217         }
218     };
219 };
220
221 class  SplitterFn: public Splitter {
222  public:
223     bool  (*fn) (uiterator&, uiterator, uiterator&);
224
225     SplitterFn (uiterator _begin, uiterator _end, bool (*_fn)(uiterator&, uiterator, uiterator&)): Splitter (_begin, _end) {
226         fn = _fn;
227     };
228     SplitterFn (const ustring& text, bool (*_fn)(uiterator&, uiterator, uiterator&)): Splitter (text.begin (), text.end ()) {
229         fn = _fn;
230     };
231     virtual  ~SplitterFn () {};
232
233     virtual bool  next () {
234         b = t = u;
235         if (b < e) {
236             fn (t, e, u);
237             return true;
238         } else {
239             return false;
240         }
241     };
242     virtual bool  nextSep () {
243         b = t = u;
244         if (b < e) {
245             return fn (t, e, u);
246         } else {
247             t = u = e;
248             return false;
249         }
250     };
251 };
252
253 class  SplitterNL: public Splitter {
254  public:
255     SplitterNL (uiterator _begin, uiterator _end): Splitter (_begin, _end) {};
256     SplitterNL (const ustring& text): Splitter (text.begin (), text.end ()) {};
257     virtual  ~SplitterNL () {};
258
259     virtual bool  next () {
260         b = t = u;
261         if (b < e) {
262             findNL (t, e, u);
263             return true;
264         } else {
265             return false;
266         }
267     };
268     virtual bool  nextSep () {
269         b = t = u;
270         if (b < e) {
271             return findNL (t, e, u);
272         } else {
273             t = u = e;
274             return false;
275         }
276     };
277 };
278
279 #endif /* UTIL_SPLITTER_H */