-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCStringTokenizer.cs
More file actions
174 lines (158 loc) · 4.13 KB
/
CStringTokenizer.cs
File metadata and controls
174 lines (158 loc) · 4.13 KB
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
using System;
using System.Text;
namespace Arad.Net.Core.Informix;
internal sealed class CStringTokenizer
{
private readonly StringBuilder _token;
private readonly string _sqlstatement;
private readonly char _quote;
private readonly char _escape;
private int _len;
private int _idx;
internal int CurrentPosition => _idx;
internal CStringTokenizer(string text, char quote, char escape)
{
_token = new StringBuilder();
_quote = quote;
_escape = escape;
_sqlstatement = text;
if (text != null)
{
int num = text.IndexOf('\0');
_len = 0 > num ? text.Length : num;
}
else
{
_len = 0;
}
}
internal string NextToken()
{
if (_token.Length != 0)
{
_idx += _token.Length;
_token.Remove(0, _token.Length);
}
while (_idx < _len && char.IsWhiteSpace(_sqlstatement[_idx]))
{
_idx++;
}
if (_idx == _len)
{
return string.Empty;
}
int i = _idx;
bool flag = false;
while (!flag && i < _len)
{
if (IsValidNameChar(_sqlstatement[i]))
{
for (; i < _len && IsValidNameChar(_sqlstatement[i]); i++)
{
_token.Append(_sqlstatement[i]);
}
continue;
}
char c = _sqlstatement[i];
if (c == '[')
{
i = GetTokenFromBracket(i);
continue;
}
if (' ' != _quote && c == _quote)
{
i = GetTokenFromQuote(i);
continue;
}
if (!char.IsWhiteSpace(c))
{
if (c == ',')
{
if (i == _idx)
{
_token.Append(c);
}
}
else
{
_token.Append(c);
}
}
flag = true;
break;
}
if (_token.Length <= 0)
{
return string.Empty;
}
return _token.ToString();
}
private int GetTokenFromBracket(int curidx)
{
while (curidx < _len)
{
_token.Append(_sqlstatement[curidx]);
curidx++;
if (_sqlstatement[curidx - 1] == ']')
{
break;
}
}
return curidx;
}
private int GetTokenFromQuote(int curidx)
{
int i;
for (i = curidx; i < _len; i++)
{
_token.Append(_sqlstatement[i]);
if (_sqlstatement[i] == _quote && i > curidx && _sqlstatement[i - 1] != _escape && i + 1 < _len && _sqlstatement[i + 1] != _quote)
{
return i + 1;
}
}
return i;
}
private bool IsValidNameChar(char ch)
{
if (!char.IsLetterOrDigit(ch) && ch != '_' && ch != '-' && ch != '.' && ch != '$' && ch != '#' && ch != '@' && ch != '~' && ch != '`' && ch != '%' && ch != '^' && ch != '&')
{
return ch == '|';
}
return true;
}
internal int FindTokenIndex(string tokenString)
{
while (true)
{
string text = NextToken();
if (_idx == _len || string.IsNullOrEmpty(text))
{
break;
}
if (string.Equals(tokenString, text, StringComparison.OrdinalIgnoreCase))
{
return _idx;
}
}
return -1;
}
internal bool StartsWith(string tokenString)
{
int i;
for (i = 0; i < _len && char.IsWhiteSpace(_sqlstatement[i]); i++)
{
}
if (_len - i < tokenString.Length)
{
return false;
}
if (string.Compare(_sqlstatement, i, tokenString, 0, tokenString.Length, StringComparison.OrdinalIgnoreCase) == 0)
{
_idx = 0;
NextToken();
return true;
}
return false;
}
}