-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
227 lines (187 loc) · 7.56 KB
/
Program.cs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using GraphemeSplitter;
using System;
using System.IO;
using System.Linq;
namespace GraphemeBreakPropertyCodeGenerator
{
/// <summary>
/// generates `switch` codes from GraphemeBreakProperty definition in unicode.org
///
/// http://www.unicode.org/Public/9.0.0/ucd/auxiliary/GraphemeBreakProperty.txt
/// http://www.unicode.org/Public/10.0.0/ucd/auxiliary/GraphemeBreakProperty.txt
/// </summary>
class Program
{
static void Main(string[] args)
{
GenerateBenchmarkCode();
Generate(UnicodeVersion.V9);
Generate(UnicodeVersion.V10);
}
const string @namespace = "GraphemeSplitter";
static void Generate(UnicodeVersion version)
{
var items = Loader.LoadProperties(version);
const string pathFormat = "../GraphemeSplitter/Character.{0}.cs";
using (var w = new StreamWriter(string.Format(pathFormat, "GetGraphemeBreakProperty" + version)))
{
w.WriteLine("#if UnicodeVersion" + version);
GenerateBinaryIfCode(items, "Character", "GetGraphemeBreakProperty", w);
w.WriteLine("#endif");
}
}
private static void GenerateBenchmarkCode()
{
var v10Items = Loader.LoadProperties(UnicodeVersion.V10)
//.Take(1000).ToArray()
//.Take(500).ToArray()
//.Take(200).ToArray()
//.Take(100).ToArray()
//.Take(50).ToArray()
//.Take(20).ToArray()
;
const string benchmarkPathFormat = "../GraphemeBreakPropertyCodeGeneratorTest/Benchmark.{0}.cs";
const string benchmarkTypeName = "Benchmark";
using (var w = new StreamWriter(string.Format(benchmarkPathFormat, "switch")))
GenerateSwitchCode(v10Items, benchmarkTypeName, "GetBySwitch", w);
using (var w = new StreamWriter(string.Format(benchmarkPathFormat, "switchwhen")))
GenerateSwitchWhenCode(v10Items, benchmarkTypeName, "GetBySwitchWhen", w);
using (var w = new StreamWriter(string.Format(benchmarkPathFormat, "binif")))
GenerateBinaryIfCode(v10Items, benchmarkTypeName, "GetByBinaryIf", w);
using (var w = new StreamWriter(string.Format(benchmarkPathFormat, "if")))
GenerateLinearIfCode(v10Items, benchmarkTypeName, "GetByIf", w);
using (var w = new StreamWriter(string.Format(benchmarkPathFormat, "items")))
GenerateItemsCode(v10Items, benchmarkTypeName, w);
}
static void GenerateHeader(string typeName, string methodName, StreamWriter w)
{
w.WriteLine($@"//generated by GraphemeBreakPropertyCodeGenerator
using static {@namespace}.GraphemeBreakProperty;
namespace {@namespace}
{{
partial class {typeName}
{{");
if (methodName != null)
w.WriteLine($@" public static GraphemeBreakProperty {methodName}(uint codePoint)
{{");
}
static void GenerateSwitchCode(PropertyItem[] items, string typeName, string methodName, StreamWriter w)
{
GenerateHeader(typeName, methodName, w);
w.WriteLine(@" switch (codePoint)
{");
var groups = items.GroupBy(x => x.Property);
foreach (var g in groups)
{
foreach (var item in g)
{
for (int i = item.Min; i <= item.Max; i++)
{
w.WriteLine($" case {i}:");
}
}
w.WriteLine($" return {g.Key};");
}
w.WriteLine(@" default: return Other;
}
}
}
}");
}
static void GenerateSwitchWhenCode(PropertyItem[] items, string typeName, string methodName, StreamWriter w)
{
GenerateHeader(typeName, methodName, w);
w.WriteLine(@" switch (codePoint)
{");
var groups = items.GroupBy(x => x.Property);
int i = 0;
foreach (var g in groups)
{
foreach (var item in g)
{
if (item.Min == item.Max)
{
w.WriteLine($" case {item.Min}:");
}
else
{
w.WriteLine($" case uint i{i} when {item.Min} <= i{i} && i{i} <= {item.Max}:");
++i;
}
}
w.WriteLine($" return {g.Key};");
}
w.WriteLine(@" default: return Other;
}
}
}
}");
}
static void GenerateBinaryIfCode(PropertyItem[] items, string typeName, string methodName, StreamWriter w)
{
GenerateHeader(typeName, methodName, w);
var sorted = items.OrderBy(x => x.Min).ToArray();
void f(int lower, int upper, int nest)
{
var ws = new string(' ', nest * 4);
if (lower == upper)
{
var r = sorted[lower];
w.WriteLine($"{ws}if ({r.Min} <= codePoint && codePoint <= {r.Max}) return {r.Property};");
w.WriteLine($"{ws}else return Other;");
}
else if (lower + 1 == upper)
{
var r = sorted[lower];
w.WriteLine($"{ws}if ({r.Min} <= codePoint && codePoint <= {r.Max}) return {r.Property};");
r = sorted[upper];
w.WriteLine($"{ws}else if ({r.Min} <= codePoint && codePoint <= {r.Max}) return {r.Property};");
w.WriteLine($"{ws}else return Other;");
}
else
{
var middle = lower + (upper - lower) / 2;
var r = sorted[middle];
w.WriteLine($"{ws}if (codePoint < {r.Min})");
f(lower, middle - 1, nest + 1);
w.WriteLine($"{ws}else if ({r.Max} < codePoint)");
f(middle + 1, upper, nest + 1);
w.WriteLine($"{ws}else return {r.Property};");
}
}
f(0, sorted.Length - 1, 3);
w.WriteLine(@" }
}
}");
}
static void GenerateLinearIfCode(PropertyItem[] items, string typeName, string methodName, StreamWriter w)
{
GenerateHeader(typeName, methodName, w);
foreach (var item in items)
{
if (item.Min == item.Max)
w.WriteLine($" if (codePoint == {item.Max}) return {item.Property};");
else
w.WriteLine($" if ({item.Min} <= codePoint && codePoint <= {item.Max}) return {item.Property};");
}
w.WriteLine(@" return Other;
}
}
}");
}
static void GenerateItemsCode(PropertyItem[] items, string typeName, StreamWriter w)
{
GenerateHeader(typeName, null, w);
w.WriteLine(@" static readonly PropertyItem[] Items = new[]
{
");
foreach (var item in items.OrderBy(x => x.Min))
{
w.WriteLine($" new PropertyItem({item.Min}, {item.Max}, {item.Property}),");
}
w.WriteLine(@" };
}
}");
}
}
}