forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomogenizeEx.cs
More file actions
38 lines (34 loc) · 1.47 KB
/
HomogenizeEx.cs
File metadata and controls
38 lines (34 loc) · 1.47 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
using System.Collections.Concurrent;
using System.Text.RegularExpressions;
using System;
namespace Simple.Data.Extensions
{
public static class HomogenizeEx
{
private static readonly ConcurrentDictionary<string, string> Cache
= new ConcurrentDictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
private static Regex _homogenizeRegex = new Regex("[^a-z0-9]");
/// <summary>
/// Downshift a string and remove all non-alphanumeric characters.
/// </summary>
/// <param name="source">The original string.</param>
/// <returns>The modified string.</returns>
public static string Homogenize(this string source)
{
return source == null ? null : Cache.GetOrAdd(source, HomogenizeImpl);
}
private static string HomogenizeImpl(string source)
{
return string.Intern(_homogenizeRegex.Replace(source.ToLowerInvariant(), string.Empty));
}
/// <summary>
/// Sets the regular expression to be used for homogenizing object names.
/// </summary>
/// <param name="regex">A regular expression matching all non-comparing characters. The default is "[^a-z0-9]".</param>
/// <remarks>Homogenized strings are always forced to lower-case.</remarks>
public static void SetRegularExpression(Regex regex)
{
_homogenizeRegex = regex;
}
}
}