Created
August 10, 2009 11:11
-
-
Save paulbatum/165134 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 NUnit.Framework; | |
namespace BadChars.Tests | |
{ | |
[TestFixture] | |
public class BadCharsTests | |
{ | |
[Test] | |
public void Input_with_bad_characters_should_be_replaced_with_approprate_character() | |
{ | |
var badChars = new[] {(char) 96, (char) 8216, (char) 8217, (char) 8242, (char) 769, (char) 768, | |
(char) 8220, (char) 8221, (char) 8243, (char) 12291,(char)8208, (char)8211, (char)8212, | |
(char)8722, (char)173, (char)8209, (char)8259,(char)160, (char)8195, (char)8194}; | |
var badString = new string(badChars); | |
var cleanedString = CleanInput(badString); | |
Assert.That(cleanedString, Is.EqualTo("''''''\"\"\"\"------- ")); | |
} | |
private static readonly CharReplacementMap _replacements = new CharReplacementMap | |
{ | |
{45, new[] {8208, 8211, 8212, 8722, 173, 8209, 8259}}, | |
{39, new[] {96, 8216, 8217, 8242, 769, 768}}, | |
{34, new[] {8220, 8221, 8243, 12291}}, | |
{32, new[] {160, 8195, 8194}} | |
}; | |
public string CleanInput(string input) | |
{ | |
IEnumerable<char> characters = input.Select(c => _replacements.Replace(c)); | |
return new string(characters.ToArray()); | |
} | |
private class CharReplacementMap : Dictionary<char, char> | |
{ | |
public void Add(int goodCharCode, IEnumerable<int> badCharCodes) | |
{ | |
foreach (char c in badCharCodes) | |
Add(c, (char)goodCharCode); | |
} | |
public char Replace(char character) | |
{ | |
TryGetValue(character, out character); | |
return character; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment