Repository navigation
Expand file tree
/
Copy pathStringEx.cs
More file actions
314 lines (262 loc) · 8.21 KB
/
Copy pathStringEx.cs
File metadata and controls
314 lines (262 loc) · 8.21 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
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
#if EXPOSE_EVERYTHING || EXPOSE_STRINGEX
public
#endif
static partial class StringEx
{
#pragma warning disable 1591
public static StringComparison GlobalDefaultComparison { get; set; } = StringComparison.Ordinal;
[ThreadStatic]
private static StringComparison? _DefaultComparison;
public static StringComparison DefaultComparison
{
get { return _DefaultComparison ?? GlobalDefaultComparison; }
set { _DefaultComparison = value; }
}
#region basic String methods
public static bool IsNullOrEmpty(this string value)
=> string.IsNullOrEmpty(value);
public static bool IsNullOrWhiteSpace(this string value)
=> string.IsNullOrWhiteSpace(value);
public static bool IsWhiteSpace(this string value)
{
foreach (var c in value)
{
if (char.IsWhiteSpace(c)) continue;
return false;
}
return true;
}
#if !PCL
public static string IsInterned(this string value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
return string.IsInterned(value);
}
public static string Intern(this string value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
return string.Intern(value);
}
#endif
#if UNSAFE
public static unsafe string ToLowerForASCII(this string value)
{
if (value.IsNullOrWhiteSpace())
return value;
value = string.Copy(value);
fixed (char* low = value)
{
var end = low + value.Length;
for (var p = low; p < end; p++)
{
var c = *p;
if (c < 'A' || c > 'Z')
continue;
*p = (char)(c + 0x20);
}
}
return value;
}
public static unsafe string ToUpperForASCII(this string value)
{
if (value.IsNullOrWhiteSpace())
return value;
value = string.Copy(value);
fixed (char* low = value)
{
var end = low + value.Length;
for (var p = low; p < end; p++)
{
var c = *p;
if (c < 'a' || c > 'z')
continue;
*p = (char)(c - 0x20);
}
}
return value;
}
#else
public static string ToLowerForASCII(this string value)
{
if (value.IsNullOrWhiteSpace())
return value;
var sb = new StringBuilder(value.Length);
foreach (var c in value)
{
if (c < 'A' || c > 'Z')
sb.Append(c);
else
sb.Append((char)(c + 0x20));
}
return sb.ToString();
}
public static string ToUpperForASCII(this string value)
{
if (value.IsNullOrWhiteSpace())
return value;
var sb = new StringBuilder(value.Length);
foreach (var c in value)
{
if (c < 'a' || c > 'z')
sb.Append(c);
else
sb.Append((char)(c - 0x20));
}
return sb.ToString();
}
#endif
#endregion
#region comparing
#region Is
public static bool Is(this string a, string b)
=> string.Equals(a, b, DefaultComparison);
public static bool Is(this string a, string b, StringComparison comparisonType)
=> string.Equals(a, b, comparisonType);
#endregion
#region BeginWith
public static bool BeginWith(this string s, char c)
{
if (s.IsNullOrEmpty()) return false;
return s[0] == c;
}
public static bool BeginWithAny(this string s, IEnumerable<char> chars)
{
if (s.IsNullOrEmpty()) return false;
return chars.Contains(s[0]);
}
public static bool BeginWithAny(this string s, params char[] chars)
=> s.BeginWithAny(chars.AsEnumerable());
public static bool BeginWith(this string a, string b)
{
if (a == null || b == null) return false;
return a.StartsWith(b, DefaultComparison);
}
public static bool BeginWith(this string a, string b, StringComparison comparisonType)
{
if (a == null || b == null) return false;
return a.StartsWith(b, comparisonType);
}
#if !PCL
public static bool BeginWith(this string a, string b, bool ignoreCase, CultureInfo culture)
{
if (a == null || b == null) return false;
return a.StartsWith(b, ignoreCase, culture);
}
#endif
#endregion
#region FinishWith
public static bool FinishWith(this string s, char c)
{
if (s.IsNullOrEmpty()) return false;
return s.Last() == c;
}
public static bool FinishWithAny(this string s, IEnumerable<char> chars)
{
if (s.IsNullOrEmpty()) return false;
return chars.Contains(s.Last());
}
public static bool FinishWithAny(this string s, params char[] chars)
=> s.FinishWithAny(chars.AsEnumerable());
public static bool FinishWith(this string a, string b)
{
if (a == null || b == null) return false;
return a.EndsWith(b, DefaultComparison);
}
public static bool FinishWith(this string a, string b, StringComparison comparisonType)
{
if (a == null || b == null) return false;
return a.EndsWith(b, comparisonType);
}
#if !PCL
public static bool FinishWith(this string a, string b, bool ignoreCase, CultureInfo culture)
{
if (a == null || b == null) return false;
return a.EndsWith(b, ignoreCase, culture);
}
#endif
#endregion
#endregion
#region ToLines
public static IEnumerable<string> ToLines(this TextReader reader)
{
string line;
while ((line = reader.ReadLine()) != null)
yield return line;
}
public static IEnumerable<string> NonEmptyLines(this TextReader reader)
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line == "") continue;
yield return line;
}
}
public static IEnumerable<string> NonWhiteSpaceLines(this TextReader reader)
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.IsWhiteSpace()) continue;
yield return line;
}
}
#endregion
#region others
private static readonly char[][] Quotes = new[]
{
"\"\"",
"''",
"“”",
"‘’",
"『』",
"「」",
"〖〗",
"【】",
}.Select(s => s.ToCharArray()).ToArray();
public static string Enquote(this string value)
{
if (value == null)
return "(null)";
foreach (var pair in Quotes)
{
if (value.IndexOfAny(pair) < 0)
return pair[0] + value + pair[1];
}
return '"' + value.Replace("\\", @"\\").Replace("\"", @"\""") + '"';
}
public static string Replace(this string value, string find, string rep, StringComparison comparsionType)
{
if (find.IsNullOrEmpty())
throw new ArgumentException(null, nameof(find));
if (rep == null)
rep = "";
if (value.IsNullOrEmpty())
return value;
var sb = new StringBuilder(value.Length);
var last = 0;
var len = find.Length;
var idx = value.IndexOf(find, DefaultComparison);
while (idx != -1)
{
sb.Append(value.Substring(last, idx - last));
sb.Append(rep);
idx += len;
last = idx;
idx = value.IndexOf(find, idx, comparsionType);
}
sb.Append(value.Substring(last));
return sb.ToString();
}
public static string ReplaceEx(this string value, string find, string rep)
=> value.Replace(find, rep, DefaultComparison);
#endregion
}