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
28 lines (25 loc) · 1.01 KB
/
HomogenizeEx.cs
File metadata and controls
28 lines (25 loc) · 1.01 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
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 readonly 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));
}
}
}