-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cs
293 lines (258 loc) · 8.43 KB
/
Utils.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace HunspellSharp
{
static class Utils
{
public const int DEFAULTFLAGS = 65510;
public const int FORBIDDENWORD = 65510;
public const int ONLYUPCASEFLAG = 65511;
static char[] seps = new char[] { ' ', '\t' };
public static string[] SplitIn2(string line)
{
return line.Split(seps, 2, StringSplitOptions.RemoveEmptyEntries);
}
public static int atoi(string a)
{
return int.TryParse(a, out var i) ? i : 0;
}
public static int atoi(Bytes a)
{
try
{
return int.Parse(a.String(Encoding.ASCII));
}
catch
{
return 0;
}
}
/* get type of capitalization */
public static CapType get_captype(string word, TextInfo textinfo)
{
int n = word.Length;
if (n == 0) return CapType.NOCAP;
// now determine the capitalization type of the first nl letters
bool hasLower = false, hasNoCaps = true;
char c;
for (int i = n - 1; i > 0; --i) {
if (char.IsUpper(c = word[i]))
{
hasNoCaps = false;
if (hasLower) break;
}
else if (!hasLower && ('a' <= c && c <= 'z' || char.IsLower(c) && textinfo.ToUpper(c) != c))
hasLower = true;
}
// now finally set the captype
return char.IsUpper(c = word[0]) ? (hasNoCaps ? CapType.INITCAP : hasLower ? CapType.HUHINITCAP : CapType.ALLCAP) :
(hasNoCaps ? CapType.NOCAP : (hasLower || 'a' <= c && c <= 'z' || char.IsLower(c) && textinfo.ToUpper(c) != c) ? CapType.HUHCAP : CapType.ALLCAP);
}
public static void GetLanguageAndTextInfo(string lang, out LANG l, ref TextInfo textinfo)
{
int lcid;
switch (lang)
{
case "ar": lcid = 0x401; l = LANG.ar; break;
case "az_AZ": // for back-compatibility
case "az": lcid = 0x42C; l = LANG.az; break;
case "bg": lcid = 0x402; l = LANG.bg; break;
case "ca": lcid = 0x403; l = LANG.ca; break;
case "crh": lcid = -1; l = LANG.crh; break;
case "cs": lcid = 0x405; l = LANG.cs; break;
case "da": lcid = 0x406; l = LANG.da; break;
case "de": lcid = 0x407; l = LANG.de; break;
case "el": lcid = 0x408; l = LANG.el; break;
case "en": lcid = 0x409; l = LANG.en; break;
case "es": lcid = 0x40A; l = LANG.es; break;
case "eu": lcid = 0x42D; l = LANG.eu; break;
case "gl": lcid = 0x456; l = LANG.gl; break;
case "fr": lcid = 0x40C; l = LANG.fr; break;
case "hr": lcid = 0x41A; l = LANG.hr; break;
case "hu_HU": // for back-compatibility
case "hu": lcid = 0x40E; l = LANG.hu; break;
case "it": lcid = 0x410; l = LANG.it; break;
case "la": lcid = 0x476; l = LANG.la; break;
case "lv": lcid = 0x426; l = LANG.lv; break;
case "nl": lcid = 0x413; l = LANG.nl; break;
case "pl": lcid = 0x415; l = LANG.pl; break;
case "pt": lcid = 0x816; l = LANG.pt; break;
case "sv": lcid = 0x424; l = LANG.sv; break;
case "tr_TR": // for back-compatibility
case "tr": lcid = 0x41F; l = LANG.tr; break;
case "ru": lcid = 0x419; l = LANG.ru; break;
case "uk": lcid = 0x422; l = LANG.uk; break;
default: lcid = -1; l = LANG.xx; break;
}
try
{
var ci = lcid > 0 ? CultureInfo.GetCultureInfo(lcid) : CultureInfo.GetCultureInfo(lang.Replace('_', '-'));
if (ci != CultureInfo.InvariantCulture) textinfo = ci.TextInfo;
}
catch
{
}
}
// uniq line in place
public static string line_uniq(string text)
{
var lines = text.Split(MSEP.REC_as_array, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length == 0) return string.Empty;
int n = 1;
for (int i = 1; i < lines.Length; ++i)
{
if (Array.IndexOf(lines, lines[i], 0, n) < 0)
{
if (n < i) lines[n] = lines[i];
++n;
}
}
return n < lines.Length ? string.Join(MSEP.REC_as_string, lines, 0, n) : text;
}
// uniq and boundary for compound analysis: "1\n\2\n\1" -> " ( \1 | \2 ) "
public static string line_uniq_app(string text)
{
if (text.IndexOf(MSEP.REC) < 0) return text;
var lines = text.Split(MSEP.REC_as_array, StringSplitOptions.RemoveEmptyEntries);
switch (lines.Length)
{
case 0:
return string.Empty;
case 1:
return lines[0];
default:
return " ( " + string.Join(" | ", lines) + " ) ";
}
}
static char[] copy_field_stop_chars = new char[] { ' ', '\t', '\n' };
public static int fieldlen(string r, int start)
{
int i = r.IndexOfAny(copy_field_stop_chars, start);
return (i >= 0 ? i : r.Length) - start;
}
public static string copy_field(string morph, int pos, string var)
{
if (string.IsNullOrEmpty(morph) ||
(pos = morph.IndexOf(var, pos)) < 0)
{
return null;
}
int end = morph.IndexOfAny(copy_field_stop_chars, pos += MORPH.TAG_LEN);
if (end < 0) end = morph.Length;
return morph.Substring(pos, end - pos);
}
public static void copy_field(StringBuilder dest, string morph, int pos, string var)
{
if (string.IsNullOrEmpty(morph) ||
(pos = morph.IndexOf(var, pos)) < 0)
{
return;
}
int end = morph.IndexOfAny(copy_field_stop_chars, pos += MORPH.TAG_LEN);
if (end < 0) end = morph.Length;
dest.Append(morph, pos, end - pos);
}
public static List<string> uniqlist(List<string> list)
{
int n = list.Count;
if (n < 2)
return list;
int j = 1;
for (int i = 1; i < n; ++i)
{
if (list.IndexOf(list[i], 0, j) < 0)
{
if (j < i) list[j] = list[i];
++j;
}
}
if (j < n) list.RemoveRange(j, n - j);
return list;
}
public static void SortRemoveDuplicates(ref ushort[] a)
{
int n = a.Length;
if (n > 1)
{
Array.Sort(a);
var prev = a[0];
int j;
for (int i = j = 1; i < n; ++i)
{
var v = a[i];
if (v != prev)
{
if (j < i) a[j] = v;
++j;
prev = v;
}
}
if (j < n) Array.Resize(ref a, j);
}
}
public static bool TESTAFF(ushort[] a, ushort b)
{
if (a == null) return false;
if (a.Length < 5)
{
for (int i = a.Length - 1; i >= 0; --i)
if (a[i] == b) return true; else if (a[i] < b) break;
return false;
}
int lo = 0, hi = a.Length;
while (lo < hi)
{
int i = (lo + hi) / 2;
if (a[i] == b) return true;
if (a[i] < b)
lo = i + 1;
else
hi = i;
}
return false;
}
public static void Heapify<V>(int[] keys, V[] values)
{
int key = keys[0], child = keys[2] < keys[1] ? 2 : 1;
if (keys[child] >= key) return;
int i = 0, n = keys.Length;
var value = values[0];
do
{
keys[i] = keys[child];
values[i] = values[child];
i = child;
child = 2 * i + 1;
if (child >= n) break;
if (child + 1 < n && keys[child + 1] < keys[child]) ++child;
} while (keys[child] < key);
keys[i] = key;
values[i] = value;
}
public static readonly char[] EmptyCharArray = new char[0];
public static readonly char[] Dot = new char[] { '.' };
public static IHunspellWarningHandler warningHandler;
static string FormatMessage(string message, object[] args)
{
int n = args.Length;
if (n > 0 && args[n - 1] is FileMgr file)
--n;
else
file = null;
if (n > 0) message = string.Format(message, args);
if (file == null) return message;
var name = file.Name;
if (name != null) return $"{name}({file.getlinenum()}): {message}";
return string.Format(Properties.Resources.MessageWithLineNumber, file.getlinenum(), message);
}
public static void HUNSPELL_WARNING(bool error, string format, params object[] args)
{
var message = FormatMessage(format, args);
if (error && Hunspell.StrictFormat) throw new HunspellException(message);
if (warningHandler == null || !warningHandler.HandleWarning(message))
System.Diagnostics.Debug.WriteLine(message);
}
}
}