Repository navigation
Expand file tree
/
Copy pathStringUtils.cs
More file actions
394 lines (337 loc) · 10.3 KB
/
Copy pathStringUtils.cs
File metadata and controls
394 lines (337 loc) · 10.3 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
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Sylphe.Utils
{
public static class StringUtils
{
/// <summary>
/// Remove leading and trailing white space from <paramref name="s"/>.
/// If <paramref name="s"/> is <c>null</c>, return <c>null</c>.
/// <seealso cref="TrimCanonical"/>
/// </summary>
/// <param name="s">The string to trim; can be <c>null</c></param>
public static string Trim(this string s)
{
return s?.Trim();
}
/// <summary>
/// Remove leading and trailing white space from <paramref name="s"/>.
/// Return the resulting strimg, or <c>null</c> if it is null or empty.
/// </summary>
/// <remarks>
/// Useful for bringing user input text into a canonical form.
/// </remarks>
/// <param name="s">The string to trim; can be <c>null</c></param>
public static string TrimCanonical(string s)
{
if (s == null) return null;
s = s.Trim();
return s.Length > 0 ? s : null;
}
/// <summary>
/// Remove leading and trailing white space (or the given
/// <paramref name="trimChars"/>) from <paramref name="sb"/>.
/// If <paramref name="sb"/> is <c>null</c>, return <c>null</c>.
/// </summary>
/// <param name="sb">The <see cref="StringBuilder"/> to trim; can be <c>null</c></param>
/// <param name="trimChars">The caracters to trim; defaults to white space if <c>null</c></param>
public static StringBuilder Trim(this StringBuilder sb, string trimChars = null)
{
if (sb == null)
{
return null;
}
const string whiteSpace = " \t\n\v\f\r\u0085\u00A0"; // SP HT LF VT FF CR NextLine NoBreakSpace
trimChars = trimChars ?? whiteSpace;
int length = sb.Length;
int start, end;
for (start = 0; start < length; start++)
{
if (trimChars.IndexOf(sb[start]) < 0)
break;
}
for (end = length - 1; end >= start; end--)
{
if (trimChars.IndexOf(sb[end]) < 0)
break;
}
if (end < length - 1)
{
sb.Remove(end + 1, length - end - 1);
}
if (start > 0)
{
sb.Remove(0, start);
}
return sb;
}
/// <summary>
/// Like <see cref="String.EndsWith(string)"/> but for StringBuilder
/// </summary>
public static bool EndsWith(this StringBuilder sb, string suffix,
StringComparison comparisonType = StringComparison.Ordinal)
{
if (string.IsNullOrEmpty(suffix)) return true;
if (sb == null) return false;
if (sb.Length < suffix.Length) return false;
int start = sb.Length - suffix.Length;
return string.Equals(sb.ToString(start, suffix.Length), suffix, comparisonType);
}
public static StringBuilder Reverse(this StringBuilder sb, int start, int length)
{
if (sb == null) return null;
if (start < 0)
throw new ArgumentOutOfRangeException(nameof(start));
if (start > sb.Length)
throw new ArgumentOutOfRangeException(nameof(start));
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length));
if (start + length > sb.Length)
throw new ArgumentOutOfRangeException(nameof(length));
for (int lo = start, hi = start + length - 1; lo < hi; lo++, hi--)
{
(sb[lo], sb[hi]) = (sb[hi], sb[lo]);
}
return sb;
}
/// <summary>
/// Format and join all <paramref name="items"/> into a string.
/// Separate items by the given <paramref name="separator"/>.
/// </summary>
/// <param name="separator">The separator string; can be null or empty</param>
/// <param name="items">The items to join; can be null or empty</param>
public static string Join<T>(string separator, IEnumerable<T> items)
{
if (items == null)
{
return string.Empty;
}
if (separator == null)
{
separator = string.Empty;
}
var sb = new StringBuilder();
foreach (var item in items)
{
if (sb.Length > 0)
{
sb.Append(separator);
}
sb.Append(item);
}
return sb.ToString();
}
/// <summary>
/// Remove diacritical marks from the given <paramref name="text"/>
/// and return the result. French accents and German umlauts work well,
/// but surprises exist; e.g. "łŁ" remains unchanged.
/// </summary>
/// <remarks>
/// About removing diacritical marks, see
/// https://docs.microsoft.com/en-us/dotnet/api/system.string.normalize and
/// http://stackoverflow.com/questions/3769457/how-can-i-remove-accents-on-a-string
/// </remarks>
public static string RemoveDiacritics(this string text, StringBuilder buffer = null)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
text = text.Normalize(NormalizationForm.FormD);
if (buffer == null)
{
buffer = new StringBuilder();
}
else
{
buffer.Clear();
}
foreach (char c in text)
{
var category = char.GetUnicodeCategory(c);
if (category != UnicodeCategory.NonSpacingMark)
{
buffer.Append(c);
}
}
return buffer.ToString();
}
/// <summary>
/// Get the length of the common prefix of the two given strings.
/// If <paramref name="maxLength"/> is non-negative, compare at
/// most that many characters.
/// </summary>
public static int CommonPrefixLength(string a, string b, int maxLength = -1)
{
if (a == null || b == null) return 0;
int limit = Math.Min(a.Length, b.Length);
if (maxLength >= 0) limit = Math.Min(maxLength, limit);
int index;
for (index = 0; index < limit; index++)
{
if (a[index] != b[index]) break;
}
return index; // index of first differing char is length of common prefix
}
/// <summary>
/// Get the next larger string such that the given <paramref name="key"/>
/// is not a prefix of the result! Use ordinal comparison. If there is
/// no such string, return <c>null</c>.
/// </summary>
/// <example>
/// For example, the next larger key after "abc" is "abd" and the next
/// larger key after "az" is "b" (here 'z' stands for char.MaxValue).
/// <para/>
/// Note that the next larger string after "z" is "za" and therefore "z"
/// is a prefix of all strings larger than "z" (here again 'z' stands for
/// char.MaxValue, and 'a' stands for char.MinValue).
/// </example>
public static string NextKeyOrdinal(string key)
{
if (string.IsNullOrEmpty(key))
{
return null; // empty string is prefix to all strings: can't get next
}
char[] chars = key.ToCharArray();
int index = chars.Length - 1;
for (; index >= 0; index--)
{
if (chars[index] < char.MaxValue)
{
chars[index]++;
break;
}
}
return index >= 0 ? new string(chars, 0, index + 1) : null;
}
/// <summary>
/// The same as <see cref="string.CompareOrdinal(string,string)"/>,
/// but compare only up to the first occurrence of the given stop char.
/// For example, if stop = '#', "foo" and "foo#bar" compare equal.
/// </summary>
public static int CompareOrdinal(string x, string y, char stop)
{
int xlen = x.Length;
int ylen = y.Length;
int min = xlen < ylen ? xlen : ylen;
for (int i = 0; i < min; i++)
{
char xi = x[i];
char yi = y[i];
if (xi == stop)
{
return yi == stop ? 0 : -1;
}
if (yi == stop)
{
return xi == stop ? 0 : +1;
}
if (xi < yi) return -1; // x < y
if (xi > yi) return +1; // x > y
// xi == yi, check chars at next position
}
// the first min chars are the same
if (xlen < ylen)
{
// x is exhausted, y has more chars:
// Let x="foo", then there are two cases for y:
// 1. y="foo#" => consider x==y
// 2. y="fooX" => consider x < y
return y[min] == stop ? 0 : -1;
}
if (xlen > ylen)
{
// y is exhausted, x has more chars:
return x[min] == stop ? 0 : +1;
}
return 0; // x == y
}
/// <summary>
/// Compare two strings "logically", that is, runs of digits
/// as numbers, all other characters lexicographically. This
/// is about how Windows Explorer sorts files names.
/// </summary>
public static int CompareLogical(string x, string y, StringComparison comparison = StringComparison.Ordinal)
{
// By .NET conventions, null sorts before anything,
// even before the empty string:
if (x == null && y == null) return 0; // both null
if (x == null) return -1;
if (y == null) return +1;
if (x.Length == 0 && y.Length == 0) return 0; // both empty
if (x.Length == 0) return -1;
if (y.Length == 0) return +1;
int ix = 0, iy = 0;
while (ix < x.Length && iy < y.Length)
{
int nx = ScanRange(x, ix, out var xx);
int ny = ScanRange(y, iy, out var yy);
if (xx.HasValue && yy.HasValue) // both numeric
{
if (xx < yy) return -1;
if (xx > yy) return +1;
// Difference in leading zeros?
if (nx < ny) return -1;
if (nx > ny) return +1;
// Truly the same numbers
ix += nx;
iy += ny;
continue;
}
if (xx.HasValue) // x numeric, y text
{
return -1;
}
if (yy.HasValue) // x text, y numeric
{
return +1;
}
// x and y non-numeric
int len = Math.Min(nx, ny);
int r = string.Compare(x, ix, y, iy, len, comparison);
if (r != 0) return r;
ix += nx;
iy += ny;
}
if (x.Length < y.Length) return -1;
if (x.Length > y.Length) return +1;
return 0;
}
private static int ScanRange(string text, int index, out double? value)
{
int anchor = index;
if (char.IsDigit(text, index))
{
value = 0;
while (index < text.Length && char.IsDigit(text, index))
{
value *= 10;
value += char.GetNumericValue(text, index);
index += 1;
}
}
else
{
while (index < text.Length && !char.IsDigit(text, index)) ++index;
value = null;
}
return index - anchor;
}
public class LogicalStringComparer : Comparer<string>
{
private readonly StringComparison _comparison;
public LogicalStringComparer()
: this(StringComparison.OrdinalIgnoreCase) { }
public LogicalStringComparer(StringComparison comparison)
{
_comparison = comparison;
}
public override int Compare(string x, string y)
{
return CompareLogical(x, y, _comparison);
}
}
}
}