-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextUtils.cs
399 lines (335 loc) · 13.3 KB
/
TextUtils.cs
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace LumiSoft.Net
{
/// <summary>
/// This class provides usefull text methods.
/// </summary>
public class TextUtils
{
#region static method QuoteString
/// <summary>
/// Qoutes string and escapes fishy('\',"') chars.
/// </summary>
/// <param name="text">Text to quote.</param>
/// <returns></returns>
public static string QuoteString(string text)
{
// String is already quoted-string.
if(text != null && text.StartsWith("\"") && text.EndsWith("\"")){
return text;
}
StringBuilder retVal = new StringBuilder();
for(int i=0;i<text.Length;i++){
char c = text[i];
if(c == '\\'){
retVal.Append("\\\\");
}
else if(c == '\"'){
retVal.Append("\\\"");
}
else{
retVal.Append(c);
}
}
return "\"" + retVal.ToString() + "\"";
}
#endregion
#region static method UnQuoteString
/// <summary>
/// Unquotes and unescapes escaped chars specified text. For example "xxx" will become to 'xxx', "escaped quote \"", will become to escaped 'quote "'.
/// </summary>
/// <param name="text">Text to unquote.</param>
/// <returns></returns>
public static string UnQuoteString(string text)
{
int startPosInText = 0;
int endPosInText = text.Length;
//--- Trim. We can't use standard string.Trim(), it's slow. ----//
for(int i=0;i<endPosInText;i++){
char c = text[i];
if(c == ' ' || c == '\t'){
startPosInText++;
}
else{
break;
}
}
for(int i=endPosInText-1;i>0;i--){
char c = text[i];
if(c == ' ' || c == '\t'){
endPosInText--;
}
else{
break;
}
}
//--------------------------------------------------------------//
// All text trimmed
if((endPosInText - startPosInText) <= 0){
return "";
}
// Remove starting and ending quotes.
if(text[startPosInText] == '\"'){
startPosInText++;
}
if(text[endPosInText - 1] == '\"'){
endPosInText--;
}
// Just '"'
if(endPosInText == startPosInText - 1){
return "";
}
char[] chars = new char[endPosInText - startPosInText];
int posInChars = 0;
bool charIsEscaped = false;
for(int i=startPosInText;i<endPosInText;i++){
char c = text[i];
// Escaping char
if(!charIsEscaped && c == '\\'){
charIsEscaped = true;
}
// Escaped char
else if(charIsEscaped){
// TODO: replace \n,\r,\t,\v ???
chars[posInChars] = c;
posInChars++;
charIsEscaped = false;
}
// Normal char
else{
chars[posInChars] = c;
posInChars++;
charIsEscaped = false;
}
}
return new string(chars,0,posInChars);
}
#endregion
#region static method EscapeString
/// <summary>
/// Escapes specified chars in the specified string.
/// </summary>
/// <param name="text">Text to escape.</param>
/// <param name="charsToEscape">Chars to escape.</param>
public static string EscapeString(string text,char[] charsToEscape)
{
// Create worst scenario buffer, assume all chars must be escaped
char[] buffer = new char[text.Length * 2];
int nChars = 0;
foreach(char c in text){
foreach(char escapeChar in charsToEscape){
if(c == escapeChar){
buffer[nChars] = '\\';
nChars++;
break;
}
}
buffer[nChars] = c;
nChars++;
}
return new string(buffer,0,nChars);
}
#endregion
#region static method UnEscapeString
/// <summary>
/// Unescapes all escaped chars.
/// </summary>
/// <param name="text">Text to unescape.</param>
/// <returns></returns>
public static string UnEscapeString(string text)
{
// Create worst scenarion buffer, non of the chars escaped.
char[] buffer = new char[text.Length];
int nChars = 0;
bool escapedCahr = false;
foreach(char c in text){
if(!escapedCahr && c == '\\'){
escapedCahr = true;
}
else{
buffer[nChars] = c;
nChars++;
escapedCahr = false;
}
}
return new string(buffer,0,nChars);
}
#endregion
#region static method SplitQuotedString
/// <summary>
/// Splits string into string arrays. This split method won't split qouted strings, but only text outside of qouted string.
/// For example: '"text1, text2",text3' will be 2 parts: "text1, text2" and text3.
/// </summary>
/// <param name="text">Text to split.</param>
/// <param name="splitChar">Char that splits text.</param>
/// <returns></returns>
public static string[] SplitQuotedString(string text,char splitChar)
{
return SplitQuotedString(text,splitChar,false);
}
/// <summary>
/// Splits string into string arrays. This split method won't split qouted strings, but only text outside of qouted string.
/// For example: '"text1, text2",text3' will be 2 parts: "text1, text2" and text3.
/// </summary>
/// <param name="text">Text to split.</param>
/// <param name="splitChar">Char that splits text.</param>
/// <param name="unquote">If true, splitted parst will be unqouted if they are qouted.</param>
/// <returns></returns>
public static string[] SplitQuotedString(string text,char splitChar,bool unquote)
{
return SplitQuotedString(text,splitChar,unquote,int.MaxValue);
}
/// <summary>
/// Splits string into string arrays. This split method won't split qouted strings, but only text outside of qouted string.
/// For example: '"text1, text2",text3' will be 2 parts: "text1, text2" and text3.
/// </summary>
/// <param name="text">Text to split.</param>
/// <param name="splitChar">Char that splits text.</param>
/// <param name="unquote">If true, splitted parst will be unqouted if they are qouted.</param>
/// <param name="count">Maximum number of substrings to return.</param>
/// <returns>Returns splitted string.</returns>
/// <exception cref="ArgumentNullException">Is raised when <b>text</b> is null reference.</exception>
public static string[] SplitQuotedString(string text,char splitChar,bool unquote,int count)
{
if(text == null){
throw new ArgumentNullException("text");
}
List<string> splitParts = new List<string>(); // Holds splitted parts
int startPos = 0;
bool inQuotedString = false; // Holds flag if position is quoted string or not
char lastChar = '0';
for(int i=0;i<text.Length;i++){
char c = text[i];
// We have exceeded maximum allowed splitted parts.
if((splitParts.Count + 1) >= count){
break;
}
// We have quoted string start/end.
if(lastChar != '\\' && c == '\"'){
inQuotedString = !inQuotedString;
}
// We have escaped or normal char.
//else{
// We igonre split char in quoted-string.
if(!inQuotedString){
// We have split char, do split.
if(c == splitChar){
if(unquote){
splitParts.Add(UnQuoteString(text.Substring(startPos,i - startPos)));
}
else{
splitParts.Add(text.Substring(startPos,i - startPos));
}
// Store new split part start position.
startPos = i + 1;
}
}
//else{
lastChar = c;
}
// Add last split part to splitted parts list
if(unquote){
splitParts.Add(UnQuoteString(text.Substring(startPos,text.Length - startPos)));
}
else{
splitParts.Add(text.Substring(startPos,text.Length - startPos));
}
return splitParts.ToArray();
}
#endregion
#region method QuotedIndexOf
/// <summary>
/// Gets first index of specified char. The specified char in quoted string is skipped.
/// Returns -1 if specified char doesn't exist.
/// </summary>
/// <param name="text">Text in what to check.</param>
/// <param name="indexChar">Char what index to get.</param>
/// <returns></returns>
public static int QuotedIndexOf(string text,char indexChar)
{
int retVal = -1;
bool inQuotedString = false; // Holds flag if position is quoted string or not
for(int i=0;i<text.Length;i++){
char c = text[i];
if(c == '\"'){
// Start/end quoted string area
inQuotedString = !inQuotedString;
}
// Current char is what index we want and it isn't in quoted string, return it's index
if(!inQuotedString && c == indexChar){
return i;
}
}
return retVal;
}
#endregion
#region static method SplitString
/// <summary>
/// Splits string into string arrays.
/// </summary>
/// <param name="text">Text to split.</param>
/// <param name="splitChar">Char Char that splits text.</param>
/// <returns></returns>
public static string[] SplitString(string text,char splitChar)
{
ArrayList splitParts = new ArrayList(); // Holds splitted parts
int lastSplitPoint = 0;
int textLength = text.Length;
for(int i=0;i<textLength;i++){
if(text[i] == splitChar){
// Add current currentSplitBuffer value to splitted parts list
splitParts.Add(text.Substring(lastSplitPoint,i - lastSplitPoint));
lastSplitPoint = i + 1;
}
}
// Add last split part to splitted parts list
if(lastSplitPoint <= textLength){
splitParts.Add(text.Substring(lastSplitPoint));
}
string[] retVal = new string[splitParts.Count];
splitParts.CopyTo(retVal,0);
return retVal;
}
#endregion
#region static method IsToken
/// <summary>
/// Gets if specified string is valid "token" value.
/// </summary>
/// <param name="value">String value to check.</param>
/// <returns>Returns true if specified string value is valid "token" value.</returns>
/// <exception cref="ArgumentNullException">Is raised if <b>value</b> is null.</exception>
public static bool IsToken(string value)
{
if(value == null){
throw new ArgumentNullException(value);
}
/* This syntax is taken from rfc 3261, but token must be universal so ... .
token = 1*(alphanum / "-" / "." / "!" / "%" / "*" / "_" / "+" / "`" / "'" / "~" )
alphanum = ALPHA / DIGIT
ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
DIGIT = %x30-39 ; 0-9
*/
char[] tokenChars = new char[]{'-','.','!','%','*','_','+','`','\'','~'};
foreach(char c in value){
// We don't have letter or digit, so we only may have token char.
if(!((c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A) || (c >= 0x30 && c <= 0x39))){
bool validTokenChar = false;
foreach(char tokenChar in tokenChars){
if(c == tokenChar){
validTokenChar = true;
break;
}
}
if(!validTokenChar){
return false;
}
}
}
return true;
}
#endregion
}
}