Created
August 10, 2009 17:47
-
-
Save anonymous/165308 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
public static class CharSanitizer | |
{ | |
public static string CleanInput(string input) | |
{ | |
return InputExpression.Replace(input, badChar => Replacements[badChar.Value]); | |
} | |
static readonly Dictionary<string, int[]> Map = new Dictionary<string, int[]> | |
{ | |
{"'", new[]{96, 8216, 8217, 8242, 769, 768}}, | |
{"\"", new[]{8220, 8221, 8243, 12291}}, | |
{"-", new[]{8208, 8211, 8212, 8722, 173, 8209, 8259}}, | |
{" ", new[]{160, 8195, 8194}} | |
}; | |
static readonly IDictionary<string, string> Replacements = Map | |
.SelectMany(kvp => kvp.Value | |
.Select(ch => new { BadCharString = ((char)ch).ToString(), CorrectString = kvp.Key })) | |
.ToDictionary(p => p.BadCharString, p => p.CorrectString); | |
static readonly Regex InputExpression = | |
new Regex("[" + string.Join("", Replacements.Keys.ToArray()) + "]", | |
RegexOptions.Compiled); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment