-
-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathExtensions.cs
More file actions
43 lines (35 loc) · 1.22 KB
/
Copy pathExtensions.cs
File metadata and controls
43 lines (35 loc) · 1.22 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace StableNameDotNet;
public static class Extensions
{
public static bool ContainsAnyInvalidSourceCodeChars(this string s, bool allowCompilerGenerated = false)
{
foreach (var c in s)
switch (c)
{
case >= 'a' and <= 'z':
case >= 'A' and <= 'Z':
case >= '0' and <= '9':
case '_' or '`':
case '.' or '<' or '>' when allowCompilerGenerated:
continue;
default:
return true;
}
return false;
}
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, Func<TValue> defaultGetter) where TKey : notnull
{
if (dict.TryGetValue(key, out var value))
return value;
value = defaultGetter();
dict.Add(key, value);
return value;
}
public static string Join(this IEnumerable<string> strings, string separator = "")
=> string.Join(separator, strings);
public static ulong StableHash(this string str)
=> str.Aggregate<char, ulong>(0, (current, c) => current * 37 + c);
}