|
| 1 | +using System.Linq; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +namespace Unidecode.NET |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// ASCII transliterations of Unicode text |
| 8 | + /// </summary> |
| 9 | + public static partial class Unidecoder |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Transliterate Unicode string to ASCII string. |
| 13 | + /// </summary> |
| 14 | + /// <param name="input">String you want to transliterate into ASCII</param> |
| 15 | + /// <param name="tempStringBuilderCapacity"> |
| 16 | + /// If you know the length of the result, |
| 17 | + /// pass the value for StringBuilder capacity. |
| 18 | + /// InputString.Length*2 is used by default. |
| 19 | + /// </param> |
| 20 | + /// <returns> |
| 21 | + /// ASCII string. There are [?] (3 characters) in places of some unknown(?) unicode characters. |
| 22 | + /// It is this way in Python code as well. |
| 23 | + /// </returns> |
| 24 | + public static string Unidecode(this string input, int? tempStringBuilderCapacity = null) |
| 25 | + { |
| 26 | + if (string.IsNullOrEmpty(input)) |
| 27 | + { |
| 28 | + return ""; |
| 29 | + } |
| 30 | + |
| 31 | + if (input.All(x => x < 0x80)) |
| 32 | + { |
| 33 | + return input; |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + // Unidecode result often can be at least two times longer than input string. |
| 38 | + var sb = new StringBuilder(tempStringBuilderCapacity ?? input.Length * 2); |
| 39 | + foreach (char c in input) |
| 40 | + { |
| 41 | + // Copypaste is bad, but sb.Append(c.Unidecode()); would be a bit slower. |
| 42 | + if (c < 0x80) |
| 43 | + { |
| 44 | + sb.Append(c); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + int high = c >> 8; |
| 49 | + int low = c & 0xff; |
| 50 | + string[] transliterations; |
| 51 | + if (characters.TryGetValue(high, out transliterations)) |
| 52 | + { |
| 53 | + sb.Append(transliterations[low]); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return sb.ToString(); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Transliterate Unicode character to ASCII string. |
| 63 | + /// </summary> |
| 64 | + /// <param name="c">Character you want to transliterate into ASCII</param> |
| 65 | + /// <returns> |
| 66 | + /// ASCII string. Unknown(?) unicode characters will return [?] (3 characters). |
| 67 | + /// It is this way in Python code as well. |
| 68 | + /// </returns> |
| 69 | + public static string Unidecode(this char c) |
| 70 | + { |
| 71 | + string result; |
| 72 | + if (c < 0x80) |
| 73 | + { |
| 74 | + result = new string(c, 1); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + int high = c >> 8; |
| 79 | + int low = c & 0xff; |
| 80 | + string[] transliterations; |
| 81 | + if (characters.TryGetValue(high, out transliterations)) |
| 82 | + { |
| 83 | + result = transliterations[low]; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + result = ""; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return result; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments