Skip to content

Instantly share code, notes, and snippets.

Created August 10, 2009 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/165308 to your computer and use it in GitHub Desktop.
Save anonymous/165308 to your computer and use it in GitHub Desktop.
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