Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
RegexBoyerMoore.cs
Go to the documentation of this file.
2
4
5internal sealed class RegexBoyerMoore
6{
7 public readonly int[] Positive;
8
9 public readonly int[] NegativeASCII;
10
11 public readonly int[][] NegativeUnicode;
12
13 public readonly string Pattern;
14
15 public readonly int LowASCII;
16
17 public readonly int HighASCII;
18
19 public readonly bool RightToLeft;
20
21 public readonly bool CaseInsensitive;
22
23 private readonly CultureInfo _culture;
24
25 public RegexBoyerMoore(string pattern, bool caseInsensitive, bool rightToLeft, CultureInfo culture)
26 {
28 RightToLeft = rightToLeft;
29 CaseInsensitive = caseInsensitive;
31 int num;
32 int num2;
33 int num3;
34 if (!rightToLeft)
35 {
36 num = -1;
37 num2 = pattern.Length - 1;
38 num3 = 1;
39 }
40 else
41 {
42 num = pattern.Length;
43 num2 = 0;
44 num3 = -1;
45 }
46 Positive = new int[pattern.Length];
47 int num4 = num2;
48 char c = pattern[num4];
49 Positive[num4] = num3;
50 num4 -= num3;
51 while (num4 != num)
52 {
53 if (pattern[num4] != c)
54 {
55 num4 -= num3;
56 continue;
57 }
58 int num5 = num2;
59 int num6 = num4;
60 while (num6 != num && pattern[num5] == pattern[num6])
61 {
62 num6 -= num3;
63 num5 -= num3;
64 }
65 if (Positive[num5] == 0)
66 {
67 Positive[num5] = num5 - num6;
68 }
69 num4 -= num3;
70 }
71 for (int num5 = num2 - num3; num5 != num; num5 -= num3)
72 {
73 if (Positive[num5] == 0)
74 {
75 Positive[num5] = num3;
76 }
77 }
78 NegativeASCII = new int[128];
79 for (int i = 0; i < 128; i++)
80 {
81 NegativeASCII[i] = num2 - num;
82 }
83 LowASCII = 127;
84 HighASCII = 0;
85 for (num4 = num2; num4 != num; num4 -= num3)
86 {
87 c = pattern[num4];
88 if (c < '\u0080')
89 {
90 if (LowASCII > c)
91 {
92 LowASCII = c;
93 }
94 if (HighASCII < c)
95 {
96 HighASCII = c;
97 }
98 if (NegativeASCII[(uint)c] == num2 - num)
99 {
100 NegativeASCII[(uint)c] = num2 - num4;
101 }
102 }
103 else
104 {
105 int num7 = (int)c >> 8;
106 int num8 = c & 0xFF;
107 if (NegativeUnicode == null)
108 {
109 NegativeUnicode = new int[256][];
110 }
111 if (NegativeUnicode[num7] == null)
112 {
113 int[] array = new int[256];
114 for (int j = 0; j < array.Length; j++)
115 {
116 array[j] = num2 - num;
117 }
118 if (num7 == 0)
119 {
122 }
123 NegativeUnicode[num7] = array;
124 }
125 if (NegativeUnicode[num7][num8] == num2 - num)
126 {
127 NegativeUnicode[num7][num8] = num2 - num4;
128 }
129 }
130 }
131 }
132
133 public bool IsMatch(string text, int index, int beglimit, int endlimit)
134 {
135 if (!RightToLeft)
136 {
137 if (index < beglimit || endlimit - index < Pattern.Length)
138 {
139 return false;
140 }
141 }
142 else
143 {
144 if (index > endlimit || index - beglimit < Pattern.Length)
145 {
146 return false;
147 }
148 index -= Pattern.Length;
149 }
150 if (CaseInsensitive)
151 {
152 TextInfo textInfo = _culture.TextInfo;
153 for (int i = 0; i < Pattern.Length; i++)
154 {
155 if (Pattern[i] != textInfo.ToLower(text[index + i]))
156 {
157 return false;
158 }
159 }
160 return true;
161 }
162 return Pattern.AsSpan().SequenceEqual(text.AsSpan(index, Pattern.Length));
163 }
164
165 public int Scan(string text, int index, int beglimit, int endlimit)
166 {
167 int num;
168 int num2;
169 int num3;
170 int num4;
171 int num5;
172 if (!RightToLeft)
173 {
174 num = Pattern.Length;
175 num2 = Pattern.Length - 1;
176 num3 = 0;
177 num4 = index + num - 1;
178 num5 = 1;
179 }
180 else
181 {
182 num = -Pattern.Length;
183 num2 = 0;
184 num3 = -num - 1;
185 num4 = index + num;
186 num5 = -1;
187 }
188 char c = Pattern[num2];
189 while (num4 < endlimit && num4 >= beglimit)
190 {
191 char c2 = text[num4];
192 if (CaseInsensitive)
193 {
194 c2 = _culture.TextInfo.ToLower(c2);
195 }
196 int num6;
197 if (c2 != c)
198 {
199 int[] array;
200 num6 = ((c2 < '\u0080') ? NegativeASCII[(uint)c2] : ((NegativeUnicode == null || (array = NegativeUnicode[(int)c2 >> 8]) == null) ? num : array[c2 & 0xFF]));
201 num4 += num6;
202 continue;
203 }
204 int num7 = num4;
205 int num8 = num2;
206 do
207 {
208 if (num8 == num3)
209 {
210 if (!RightToLeft)
211 {
212 return num7;
213 }
214 return num7 + 1;
215 }
216 num8 -= num5;
217 num7 -= num5;
218 c2 = text[num7];
219 if (CaseInsensitive)
220 {
221 c2 = _culture.TextInfo.ToLower(c2);
222 }
223 }
224 while (c2 == Pattern[num8]);
225 num6 = Positive[num8];
226 if ((c2 & 0xFF80) == 0)
227 {
228 num7 = num8 - num2 + NegativeASCII[(uint)c2];
229 }
230 else
231 {
232 int[] array;
233 if (NegativeUnicode == null || (array = NegativeUnicode[(int)c2 >> 8]) == null)
234 {
235 num4 += num6;
236 continue;
237 }
238 num7 = num8 - num2 + array[c2 & 0xFF];
239 }
240 if (RightToLeft ? (num7 < num6) : (num7 > num6))
241 {
242 num6 = num7;
243 }
244 num4 += num6;
245 }
246 return -1;
247 }
248}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
int Scan(string text, int index, int beglimit, int endlimit)
RegexBoyerMoore(string pattern, bool caseInsensitive, bool rightToLeft, CultureInfo culture)
bool IsMatch(string text, int index, int beglimit, int endlimit)