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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
public static class Hebrew { static Dictionary<char, char> mapper = CombineMappers( BuildMapper("שנבגקכעיןחלךצמםפ/רדאוה'סטז", "abcdefghijklmnopqrstuvqxyz"), BuildMapper("tcdsvuzjyhflknobixgp;m.era,", "אבגדהוזחטיכךלמםנןסעפףצץקרשת")); public class CharEqualityIgnoreCase : IEqualityComparer<char> { public static readonly CharEqualityIgnoreCase Default = new CharEqualityIgnoreCase(); static int dist = Math.Abs((int)'A' - (int)'a'); public bool Equals(char x, char y) { return x == y || (char.IsLetter(x) && char.IsLetter(y) && Math.Abs((int)x - (int)y) == dist); } public int GetHashCode(char obj) { return char.IsLetter(obj) ? char.ToLower(obj).GetHashCode() : obj.GetHashCode(); } } static Dictionary<char, char> CombineMappers(Dictionary<char, char> mapper1, Dictionary<char, char> mapper2) { var result = new Dictionary<char, char>(CharEqualityIgnoreCase.Default); foreach (var pair in mapper1) { result.Add(pair.Key, pair.Value); } foreach (var pair in mapper2) { result.Add(pair.Key, pair.Value); } return result; } static Dictionary<char, char> BuildMapper(string src, string dest) { var result = new Dictionary<char, char>(); for (int i = 0; i < src.Length; i++) { result[src[i]] = dest[i]; } return result; } public static string Toggle(string text) { var result = new StringBuilder(text.Length); for (int i = 0; i < text.Length; i++) { var ch = text[i]; if (mapper.TryGetValue(ch, out ch) == false) { ch = text[i]; } result.Append(ch); } return result.ToString(); } }
Refactorings
No refactoring yet !
Many times when having a Hebrew/English keyboard you find yourself typing the right chars but in the wrong language.
This simple class toggles the chars so that for example if you intend to write something in Hebrew but you were on the English mode you will get the text in Hebrew.
This is very useful when processing user input, the use might have enter the "right" data but in the wrong keyboard mode, you can just toggle the data and return the "right" results.