-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathPolyfillExtensions.cs
More file actions
90 lines (69 loc) · 2.96 KB
/
PolyfillExtensions.cs
File metadata and controls
90 lines (69 loc) · 2.96 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Polyfill extension methods for the Adapter projects which target
// net462 and need APIs not available on .NET Framework.
#if !NET5_0_OR_GREATER
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
internal static class AdapterPolyfillExtensions
{
public static bool Contains(this string s, char c) => s.IndexOf(c) >= 0;
public static bool StartsWith(this string s, char c) => s.Length > 0 && s[0] == c;
public static bool EndsWith(this string s, char c) => s.Length > 0 && s[s.Length - 1] == c;
public static void Deconstruct<TKey, TValue>(this System.Collections.Generic.KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
{
key = pair.Key;
value = pair.Value;
}
public static bool IsAssignableTo(this System.Type type, System.Type? targetType)
=> targetType?.IsAssignableFrom(type) ?? false;
public static bool TryAdd<TKey, TValue>(this System.Collections.Generic.Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
where TKey : notnull
{
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, value);
return true;
}
return false;
}
public static System.Text.StringBuilder AppendJoin(this System.Text.StringBuilder sb, string separator, System.Collections.Generic.IEnumerable<string> values)
{
bool first = true;
foreach (string value in values)
{
if (!first)
{
sb.Append(separator);
}
sb.Append(value);
first = false;
}
return sb;
}
public static System.Text.StringBuilder AppendJoin(this System.Text.StringBuilder sb, char separator, System.Collections.Generic.IEnumerable<string> values) =>
sb.AppendJoin(separator.ToString(), values);
public static TValue GetOrAdd<TKey, TValue, TArg>(this System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> dictionary, TKey key, System.Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument)
where TKey : notnull
=> dictionary.GetOrAdd(key, k => valueFactory(k, factoryArgument));
public static string[] Split(this string s, char separator, System.StringSplitOptions options) =>
s.Split([separator], options);
}
#endif
#if !NET8_0_OR_GREATER
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
internal static class AdapterCancellationTokenSourcePolyfill
{
public static System.Threading.Tasks.Task CancelAsync(this System.Threading.CancellationTokenSource cts)
{
if (cts.IsCancellationRequested)
{
return System.Threading.Tasks.Task.CompletedTask;
}
var task = System.Threading.Tasks.Task.Run(cts.Cancel);
while (!cts.IsCancellationRequested)
{
}
return task;
}
}
#endif