-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExtensions.cs
More file actions
384 lines (322 loc) · 11.7 KB
/
Copy pathExtensions.cs
File metadata and controls
384 lines (322 loc) · 11.7 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
// SOME BASE CODE PROVIDED UNDER THIS LICENSE
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
using System.Reflection;
namespace ControlLibrary.Extensions
{
internal static partial class Extensions
{
public static bool Contains(this string s, string value, StringComparison comparison)
{
return s.IndexOf(value, comparison) >= 0;
}
internal static IDictionary<string, object> ParseUrlQueryString(string query)
{
var result = new Dictionary<string, object>();
// if string is null, empty or whitespace
if (string.IsNullOrEmpty(query) || query.Trim().Length == 0)
{
return result;
}
string decoded = query;
int decodedLength = decoded.Length;
int namePos = 0;
bool first = true;
while (namePos <= decodedLength)
{
int valuePos = -1, valueEnd = -1;
for (int q = namePos; q < decodedLength; q++)
{
if (valuePos == -1 && decoded[q] == '=')
{
valuePos = q + 1;
}
else if (decoded[q] == '&' || decoded[q] == '#')
{
valueEnd = q;
break;
}
}
if (first)
{
first = false;
if (decoded[namePos] == '#')
{
namePos++;
}
}
string name, value;
if (valuePos == -1)
{
name = null;
valuePos = namePos;
}
else
{
//name = UrlDecode(decoded.Substring(namePos, valuePos - namePos - 1));
name = decoded.Substring(namePos, valuePos - namePos - 1);
}
if (valueEnd < 0)
{
namePos = -1;
valueEnd = decoded.Length;
}
else
{
namePos = valueEnd + 1;
}
//value = UrlDecode(decoded.Substring(valuePos, valueEnd - valuePos));
value = decoded.Substring(valuePos, valueEnd - valuePos);
if (!string.IsNullOrEmpty(name))
{
result[name] = value;
}
if (namePos == -1)
{
break;
}
}
return result;
}
public static string QueryString(this string query, string param)
{
var result = new Dictionary<string, object>();
string decoded = query;
int decodedLength = decoded.Length;
int namePos = 0;
bool first = true;
while (namePos <= decodedLength)
{
int valuePos = -1, valueEnd = -1;
for (int q = namePos; q < decodedLength; q++)
{
if (valuePos == -1 && decoded[q] == '=')
{
valuePos = q + 1;
}
else if (decoded[q] == '&' || decoded[q] == '#')
{
valueEnd = q;
break;
}
}
if (first)
{
first = false;
if (decoded[namePos] == '#')
{
namePos++;
}
}
string name, value;
if (valuePos == -1)
{
name = null;
valuePos = namePos;
}
else
{
//name = UrlDecode(decoded.Substring(namePos, valuePos - namePos - 1));
name = decoded.Substring(namePos, valuePos - namePos - 1);
}
if (valueEnd < 0)
{
namePos = -1;
valueEnd = decoded.Length;
}
else
{
namePos = valueEnd + 1;
}
//value = UrlDecode(decoded.Substring(valuePos, valueEnd - valuePos));
value = decoded.Substring(valuePos, valueEnd - valuePos);
if (!string.IsNullOrEmpty(name))
{
result[name] = value;
}
if (namePos == -1)
{
break;
}
}
return result[param].ToString();
}
public static string Then(this string input, string value)
{
return String.Concat(input, value);
}
public static string ToLower(this Enum type)
{
return type.ToString().ToLower();
}
public static string ToUpper(this Enum type)
{
return type.ToString().ToUpper();
}
public static DateTime FromNow(this TimeSpan value)
{
return new DateTime((DateTime.Now + value).Ticks);
}
public static DateTime FromUnixTime(this long seconds)
{
var time = new DateTime(1970, 1, 1);
time = time.AddSeconds(seconds);
return time.ToLocalTime();
}
public static long ToUnixTime(this DateTime dateTime)
{
var timeSpan = (dateTime - new DateTime(1970, 1, 1));
var timestamp = (long)timeSpan.TotalSeconds;
return timestamp;
}
public static byte[] GetBytes(this string input)
{
return Encoding.UTF8.GetBytes(input);
}
public static string PercentEncode(this string s)
{
var bytes = s.GetBytes();
var sb = new StringBuilder();
foreach (var b in bytes)
{
// [DC]: Support proper encoding of special characters (\n\r\t\b)
if ((b > 7 && b < 11) || b == 13)
{
sb.Append(string.Format("%0{0:X}", b));
}
else
{
sb.Append(string.Format("%{0:X}", b));
}
}
return sb.ToString();
}
public static string FormatWith(this string format, params object[] args)
{
return String.Format(format, args);
}
public static string FormatWithInvariantCulture(this string format, params object[] args)
{
return String.Format(CultureInfo.InvariantCulture, format, args);
}
public static bool IsNullOrBlank(this string value)
{
return String.IsNullOrEmpty(value) ||
(!String.IsNullOrEmpty(value) && value.Trim() == String.Empty);
}
public static bool EqualsIgnoreCase(this string left, string right)
{
return String.Compare(left, right, StringComparison.OrdinalIgnoreCase) == 0;
}
public static bool EqualsAny(this string input, params string[] args)
{
return args.Aggregate(false, (current, arg) => current | input.Equals(arg));
}
public static IEnumerable<T> AsEnumerable<T>(this T item)
{
return new[] { item };
}
public static IEnumerable<T> And<T>(this T item, T other)
{
return new[] { item, other };
}
public static IEnumerable<T> And<T>(this IEnumerable<T> items, T item)
{
foreach (var i in items)
{
yield return i;
}
yield return item;
}
public static K TryWithKey<T, K>(this IDictionary<T, K> dictionary, T key)
{
return dictionary.ContainsKey(key) ? dictionary[key] : default(K);
}
public static IEnumerable<T> ToEnumerable<T>(this object[] items) where T : class
{
foreach (var item in items)
{
var record = item as T;
yield return record;
}
}
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (var item in items)
{
action(item);
}
}
public static string UrlEncode(this string value)
{
return Uri.EscapeDataString(value);
}
public static string UrlDecode(this string value)
{
return Uri.UnescapeDataString(value);
}
public static Uri AsUri(this string value)
{
return new Uri(value);
}
public static string HashWith(this string input, MacAlgorithmProvider hashProvider, string key)
{
IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
CryptographicKey cryptoKey = hashProvider.CreateKey(keyMaterial);
IBuffer hash = CryptographicEngine.Sign(cryptoKey, CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8));
return CryptographicBuffer.EncodeToBase64String(hash);
}
public static Windows.UI.Color FromName(string colorName)
{
var colorProperty = typeof(Windows.UI.Colors).GetRuntimeProperty(colorName.UpperFirst());
if (colorProperty == null) throw new ArgumentException("This is not a known color name. Use a proper hex color number.");
return (Windows.UI.Color)colorProperty.GetValue(null);
}
public static string UpperFirst(this string nameValue)
{
if (string.IsNullOrEmpty(nameValue)) return string.Empty;
char[] chars = nameValue.ToCharArray();
chars[0] = char.ToUpperInvariant(chars[0]);
return new string(chars);
}
public static Windows.UI.Color ToColor(this string hexValue)
{
if (!hexValue.Contains("#"))
{ // may be a named color, attempt that
var foundColor = FromName(hexValue);
if (foundColor != null) return foundColor;
}
hexValue = hexValue.Replace("#", string.Empty);
// some loose validation (not bullet-proof)
if (hexValue.Length < 6)
{
throw new ArgumentException("This does not appear to be a proper hex color number");
}
byte a = 255;
byte r = 255;
byte g = 255;
byte b = 255;
int startPosition = 0;
// the case where alpha is provided
if (hexValue.Length == 8)
{
a = byte.Parse(hexValue.Substring(0, 2), NumberStyles.HexNumber);
startPosition = 2;
}
r = byte.Parse(hexValue.Substring(startPosition, 2), NumberStyles.HexNumber);
g = byte.Parse(hexValue.Substring(startPosition + 2, 2), NumberStyles.HexNumber);
b = byte.Parse(hexValue.Substring(startPosition + 4, 2), NumberStyles.HexNumber);
return Windows.UI.Color.FromArgb(a, r, g, b);
}
}
}