1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
using System;
|
28
|
|
using System.Collections;
|
29
|
|
using System.Text;
|
30
|
|
|
31
|
|
namespace IBatisNet.Common.Utilities
|
32
|
|
{
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
public class StringTokenizer : IEnumerable
|
38
|
|
{
|
39
|
|
|
40
|
|
private static readonly string _defaultDelim=" \t\n\r\f";
|
41
|
|
string _origin = string.Empty;
|
42
|
|
string _delimiters = string.Empty;
|
43
|
|
bool _returnDelimiters = false;
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
0
|
public StringTokenizer(string str)
|
51
|
|
{
|
52
|
|
_origin = str;
|
53
|
|
_delimiters = _defaultDelim;
|
54
|
|
_returnDelimiters = false;
|
55
|
|
}
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
|
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
525
|
public StringTokenizer(string str, string delimiters)
|
65
|
|
{
|
66
|
525
|
_origin = str;
|
67
|
525
|
_delimiters = delimiters;
|
68
|
525
|
_returnDelimiters = false;
|
69
|
|
}
|
70
|
|
|
71
|
|
|
72
|
|
|
73
|
|
|
74
|
|
|
75
|
|
|
76
|
|
|
77
|
|
|
78
|
|
|
79
|
69952
|
public StringTokenizer(string str, string delimiters, bool returnDelimiters)
|
80
|
|
{
|
81
|
69952
|
_origin = str;
|
82
|
69952
|
_delimiters = delimiters;
|
83
|
69952
|
_returnDelimiters = returnDelimiters;
|
84
|
|
}
|
85
|
|
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
70477
|
public IEnumerator GetEnumerator()
|
92
|
|
{
|
93
|
70477
|
return new StringTokenizerEnumerator(this);
|
94
|
|
}
|
95
|
|
|
96
|
|
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
|
|
105
|
|
public int TokenNumber
|
106
|
|
{
|
107
|
3910
|
get
|
108
|
|
{
|
109
|
3910
|
int count = 0;
|
110
|
3910
|
int currpos = 0;
|
111
|
3910
|
int maxPosition = _origin.Length;
|
112
|
|
|
113
|
17000
|
while (currpos < maxPosition)
|
114
|
|
{
|
115
|
13090
|
while (!_returnDelimiters &&
|
116
|
|
(currpos < maxPosition) &&
|
117
|
|
(_delimiters.IndexOf(_origin[currpos]) >= 0))
|
118
|
|
{
|
119
|
0
|
currpos++;
|
120
|
|
}
|
121
|
|
|
122
|
13090
|
if (currpos >= maxPosition)
|
123
|
|
{
|
124
|
0
|
break;
|
125
|
|
}
|
126
|
|
|
127
|
13090
|
int start = currpos;
|
128
|
96390
|
while ((currpos < maxPosition) &&
|
129
|
|
(_delimiters.IndexOf(_origin[currpos]) < 0))
|
130
|
|
{
|
131
|
83300
|
currpos++;
|
132
|
|
}
|
133
|
13090
|
if (_returnDelimiters && (start == currpos) &&
|
134
|
|
(_delimiters.IndexOf(_origin[currpos]) >= 0))
|
135
|
|
{
|
136
|
4590
|
currpos++;
|
137
|
|
}
|
138
|
13090
|
count++;
|
139
|
|
}
|
140
|
3910
|
return count;
|
141
|
|
|
142
|
|
}
|
143
|
|
|
144
|
|
}
|
145
|
|
|
146
|
|
|
147
|
|
private class StringTokenizerEnumerator : IEnumerator
|
148
|
|
{
|
149
|
|
private StringTokenizer _stokenizer;
|
150
|
|
private int _cursor = 0;
|
151
|
|
private string _next = null;
|
152
|
|
|
153
|
70477
|
public StringTokenizerEnumerator(StringTokenizer stok)
|
154
|
|
{
|
155
|
70477
|
_stokenizer = stok;
|
156
|
|
}
|
157
|
|
|
158
|
373267
|
public bool MoveNext()
|
159
|
|
{
|
160
|
373267
|
_next = GetNext();
|
161
|
373267
|
return _next != null;
|
162
|
|
}
|
163
|
|
|
164
|
0
|
public void Reset()
|
165
|
|
{
|
166
|
|
_cursor = 0;
|
167
|
|
}
|
168
|
|
|
169
|
|
public object Current
|
170
|
|
{
|
171
|
301430
|
get
|
172
|
|
{
|
173
|
301430
|
return _next;
|
174
|
|
}
|
175
|
|
}
|
176
|
|
|
177
|
373793
|
private string GetNext()
|
178
|
|
{
|
179
|
373793
|
char c;
|
180
|
373793
|
bool isDelim;
|
181
|
|
|
182
|
373793
|
if( _cursor >= _stokenizer._origin.Length )
|
183
|
67247
|
return null;
|
184
|
|
|
185
|
306546
|
c = _stokenizer._origin[_cursor];
|
186
|
306546
|
isDelim = (_stokenizer._delimiters.IndexOf(c) != -1);
|
187
|
|
|
188
|
306546
|
if ( isDelim )
|
189
|
|
{
|
190
|
132069
|
_cursor++;
|
191
|
132069
|
if ( _stokenizer._returnDelimiters )
|
192
|
|
{
|
193
|
131543
|
return c.ToString();
|
194
|
|
}
|
195
|
526
|
return GetNext();
|
196
|
|
}
|
197
|
|
|
198
|
174477
|
int nextDelimPos = _stokenizer._origin.IndexOfAny(_stokenizer._delimiters.ToCharArray(), _cursor);
|
199
|
174477
|
if (nextDelimPos == -1)
|
200
|
|
{
|
201
|
43428
|
nextDelimPos = _stokenizer._origin.Length;
|
202
|
|
}
|
203
|
|
|
204
|
174477
|
string nextToken = _stokenizer._origin.Substring(_cursor, nextDelimPos - _cursor);
|
205
|
174477
|
_cursor = nextDelimPos;
|
206
|
174477
|
return nextToken;
|
207
|
|
}
|
208
|
|
|
209
|
|
}
|
210
|
|
}
|
211
|
|
|
212
|
|
}
|
213
|
|
|