-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStr.cs
555 lines (482 loc) · 7.91 KB
/
Str.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
public class Str
{
static Encoding asciiEncoding = Encoding.ASCII;
public static Encoding AsciiEncoding
{
get { return Str.asciiEncoding; }
}
static Encoding utf8Encoding = Encoding.UTF8;
public static Encoding Utf8Encoding
{
get { return Str.utf8Encoding; }
}
static Encoding uniEncoding = Encoding.Unicode;
public static Encoding UniEncoding
{
get { return Str.uniEncoding; }
}
public static string ByteToHex3(byte[] data)
{
if (data.Length == 0)
{
return "";
}
return BitConverter.ToString(data) + "-";
}
public static string ByteToHex(byte[] data)
{
StringBuilder ret = new StringBuilder();
foreach (byte b in data)
{
string s = b.ToString("X");
if (s.Length == 1)
{
s = "0" + s;
}
ret.Append(s);
}
return ret.ToString();
}
public static byte[] HexToByte(string str)
{
try
{
List<byte> o = new List<byte>();
string tmp = "";
int i, len;
str = str.ToUpper().Trim();
len = str.Length;
for (i = 0; i < len; i++)
{
char c = str[i];
if (('0' <= c && c <= '9') || ('A' <= c && c <= 'F'))
{
tmp += c;
if (tmp.Length == 2)
{
byte b = Convert.ToByte(tmp, 16);
o.Add(b);
tmp = "";
}
}
else if (c == ' ' || c == ',' || c == '-' || c == ';')
{
}
else
{
break;
}
}
return o.ToArray();
}
catch
{
return new byte[0];
}
}
public static string ByteToStr(byte[] data, Encoding enc)
{
try
{
return enc.GetString(data);
}
catch
{
return "";
}
}
public static byte[] StrToByte(string str, Encoding enc)
{
try
{
return enc.GetBytes(str);
}
catch
{
return new byte[0];
}
}
public static string[] GetLines(string str)
{
List<string> a = new List<string>();
StringReader sr = new StringReader(str);
while (true)
{
string s = sr.ReadLine();
if (s == null)
{
break;
}
a.Add(s);
}
return a.ToArray();
}
public static string LinesToStr(string[] lines)
{
StringWriter sw = new StringWriter();
foreach (string s in lines)
{
sw.WriteLine(s);
}
return sw.ToString();
}
public static bool IsEmptyStr(string str)
{
if (str == null || str.Trim().Length == 0)
{
return true;
}
else
{
return false;
}
}
public static bool IsSplitChar(char c, string splitStr)
{
if (splitStr == null)
{
splitStr = StrToken.DefaultSplitStr;
}
foreach (char t in splitStr)
{
string a = "" + t;
string b = "" + c;
if (Util.StrCmpi(a, b))
{
return true;
}
}
return false;
}
public static bool GetKeyAndValue(string str, out string key, out string value)
{
return GetKeyAndValue(str, out key, out value, null);
}
public static bool GetKeyAndValue(string str, out string key, out string value, string splitStr)
{
uint mode = 0;
string keystr = "", valuestr = "";
if (splitStr == null)
{
splitStr = StrToken.DefaultSplitStr;
}
foreach (char c in str)
{
switch (mode)
{
case 0:
if (IsSplitChar(c, splitStr) == false)
{
mode = 1;
keystr += c;
}
break;
case 1:
if (IsSplitChar(c, splitStr) == false)
{
keystr += c;
}
else
{
mode = 2;
}
break;
case 2:
if (IsSplitChar(c, splitStr) == false)
{
mode = 3;
valuestr += c;
}
break;
case 3:
valuestr += c;
break;
}
}
if (mode == 0)
{
value = "";
key = "";
return false;
}
else
{
value = valuestr;
key = keystr;
return true;
}
}
public static int StrCmpRetInt(string s1, string s2)
{
return s1.CompareTo(s2);
}
public static bool StrCmp(string s1, string s2)
{
return StrCmpRetInt(s1, s2) == 0 ? true : false;
}
public static int StrCmpiRetInt(string s1, string s2)
{
s1 = s1.ToUpper();
s2 = s2.ToUpper();
return StrCmpRetInt(s1, s2);
}
public static bool StrCmpi(string s1, string s2)
{
return StrCmpiRetInt(s1, s2) == 0 ? true : false;
}
public static bool IsStrInList(string str, params string[] args)
{
return IsStrInList(str, true, args);
}
public static bool IsStrInList(string str, bool ignoreCase, params string[] args)
{
foreach (string s in args)
{
if (ignoreCase)
{
if (StrCmpi(str, s))
{
return true;
}
}
else
{
if (StrCmp(str, s))
{
return true;
}
}
}
return false;
}
public static bool IsNumber(string str)
{
str = str.Trim();
foreach (char c in str)
{
if (c >= '0' && c <= '9')
{
}
else
{
return false;
}
}
return true;
}
public static string DateToString(DateTime dt)
{
if (dt.Ticks != 0)
{
return dt.ToString("yyyyMMdd HHmmss").Substring(2);
}
else
{
return "000000_000000";
}
}
public static DateTime StringToDate(string str)
{
str = str.Replace("(JST)", "").Trim();
try
{
return DateTime.Parse(str);
}
catch
{
return new DateTime(0);
}
}
public static string ToSafeString(string str)
{
char[] chars =
{
';', '?', '=', '<', '>', ':', '@', '%', '$', '\\', '/', '|', '\"', '\r', '\n',
};
string ret = str;
foreach (char c in chars)
{
ret = ret.Replace(c, '_');
}
string ret2 = "";
foreach (char c in ret)
{
bool b = false;
if (c >= 0x00 && c <= 0x1f)
{
b = true;
}
if (c >= 0x7f && c <= 0xff)
{
b = true;
}
if (b == false)
{
ret2 += c;
}
else
{
ret2 += "_";
}
}
return ret2;
}
public static byte[] Base64Decode(string src)
{
try
{
return System.Convert.FromBase64String(src);
}
catch
{
return null;
}
}
public static string DecodeMime(string src)
{
string[] s = src.Split('?');
byte[] b;
if (s[2] == "B")
{
b = System.Convert.FromBase64String(s[3]);
}
else
{
throw new Exception("Bad Encode.");
}
string ret = System.Text.Encoding.GetEncoding(s[1]).GetString(b);
if (s.Length >= 4)
{
string tmp = s[s.Length - 1];
if (tmp.StartsWith("="))
{
ret += tmp.Substring(1);
}
}
return ret;
}
public static bool HasNullChar(string str)
{
if (str.IndexOf('\0') != -1)
{
return true;
}
return false;
}
public static bool IsMailHeaderStr(string str)
{
if (str == null)
{
return false;
}
if (HasNullChar(str))
{
return false;
}
string[] sep = { ": " };
string[] tokens = str.Split(sep, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length >= 2)
{
return true;
}
return false;
}
public static bool IsStrForBase64(string str)
{
string b64str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/+=";
foreach (char c in str)
{
bool b = false;
foreach (char c2 in b64str)
{
if (c == c2)
{
b = true;
break;
}
}
if (b == false)
{
return false;
}
}
return true;
}
}
public class StrToken
{
string[] tokens;
public string[] Tokens
{
get { return tokens; }
}
public string this[uint index]
{
get { return tokens[index]; }
}
public uint NumTokens
{
get
{
return (uint)Tokens.Length;
}
}
const string defaultSplitStr = " ,\t\r\n";
public static string DefaultSplitStr
{
get { return defaultSplitStr; }
}
public StrToken(string str)
: this(str, null)
{
}
public StrToken(string str, string splitStr)
{
if (splitStr == null)
{
splitStr = defaultSplitStr;
}
int i, len;
len = splitStr.Length;
char[] chars = new char[len];
for (i = 0; i < len; i++)
{
chars[i] = splitStr[i];
}
tokens = str.Split(chars, StringSplitOptions.RemoveEmptyEntries);
}
}
public class StrData
{
string strValue;
public string StrValue
{
get { return strValue; }
}
public uint IntValue
{
get
{
return Util.StrToUInt(strValue);
}
}
public ulong Int64Value
{
get
{
return Util.StrToULong(strValue);
}
}
public StrData(string str)
{
if (str == null)
{
str = "";
}
strValue = str;
}
}