forked from thecoderok/Unidecode.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnidecoder.cs
More file actions
94 lines (87 loc) · 2.94 KB
/
Unidecoder.cs
File metadata and controls
94 lines (87 loc) · 2.94 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Linq;
using System.Text;
namespace Unidecode.NET
{
/// <summary>
/// ASCII transliterations of Unicode text
/// </summary>
public static partial class Unidecoder
{
/// <summary>
/// Transliterate Unicode string to ASCII string.
/// </summary>
/// <param name="input">String you want to transliterate into ASCII</param>
/// <param name="tempStringBuilderCapacity">
/// If you know the length of the result,
/// pass the value for StringBuilder capacity.
/// InputString.Length*2 is used by default.
/// </param>
/// <returns>
/// ASCII string. There are [?] (3 characters) in places of some unknown(?) unicode characters.
/// It is this way in Python code as well.
/// </returns>
public static string Unidecode(this string input, int? tempStringBuilderCapacity = null)
{
if (string.IsNullOrEmpty(input))
{
return "";
}
if (input.All(x => x < 0x80))
{
return input;
}
// Unidecode result often can be at least two times longer than input string.
var sb = new StringBuilder(tempStringBuilderCapacity ?? input.Length * 2);
foreach (char c in input)
{
// Copypaste is bad, but sb.Append(c.Unidecode()); would be a bit slower.
if (c < 0x80)
{
sb.Append(c);
}
else
{
int high = c >> 8;
int low = c & 0xff;
string[] transliterations;
if (characters.TryGetValue(high, out transliterations))
{
sb.Append(transliterations[low]);
}
}
}
return sb.ToString();
}
/// <summary>
/// Transliterate Unicode character to ASCII string.
/// </summary>
/// <param name="c">Character you want to transliterate into ASCII</param>
/// <returns>
/// ASCII string. Unknown(?) unicode characters will return [?] (3 characters).
/// It is this way in Python code as well.
/// </returns>
public static string Unidecode(this char c)
{
string result;
if (c < 0x80)
{
result = new string(c, 1);
}
else
{
int high = c >> 8;
int low = c & 0xff;
string[] transliterations;
if (characters.TryGetValue(high, out transliterations))
{
result = transliterations[low];
}
else
{
result = "";
}
}
return result;
}
}
}