| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// <copyright file="RegexBoyerMoore.cs" company="Microsoft"> |
| 3 |
// Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 |
// </copyright> |
| 5 |
//------------------------------------------------------------------------------ |
| 6 |
|
| 7 |
// The RegexBoyerMoore object precomputes the Boyer-Moore |
| 8 |
// tables for fast string scanning. These tables allow |
| 9 |
// you to scan for the first occurance of a string within |
| 10 |
// a large body of text without examining every character. |
| 11 |
// The performance of the heuristic depends on the actual |
| 12 |
// string and the text being searched, but usually, the longer |
| 13 |
// the string that is being searched for, the fewer characters |
| 14 |
// need to be examined. |
| 15 |
|
| 16 |
namespace System.Text.RegularExpressions |
| 17 |
{ |
| 18 |
|
| 19 |
using System.Collections; |
| 20 |
using System.Diagnostics; |
| 21 |
using System.Globalization; |
| 22 |
|
| 23 |
internal sealed class RegexBoyerMoore { |
| 24 |
internal int[] _positive; |
| 25 |
internal int[] _negativeASCII; |
| 26 |
internal int[][] _negativeUnicode; |
| 27 |
internal String _pattern; |
| 28 |
internal int _lowASCII; |
| 29 |
internal int _highASCII; |
| 30 |
internal bool _rightToLeft; |
| 31 |
internal bool _caseInsensitive; |
| 32 |
internal CultureInfo _culture; |
| 33 |
|
| 34 |
internal const int infinite = 0x7FFFFFFF; |
| 35 |
|
| 36 |
/* |
| 37 |
* Constructs a Boyer-Moore state machine for searching for the string |
| 38 |
* pattern. The string must not be zero-length. |
| 39 |
*/ |
| 40 |
internal RegexBoyerMoore(String pattern, bool caseInsensitive, bool rightToLeft, CultureInfo culture) { |
| 41 |
/* |
| 42 |
* Sorry, you just can't use Boyer-Moore to find an empty pattern. |
| 43 |
* We're doing this for your own protection. (Really, for speed.) |
| 44 |
*/ |
| 45 |
Debug.Assert(pattern.Length != 0, "RegexBoyerMoore called with an empty string. This is bad for perf"); |
| 46 |
|
| 47 |
int beforefirst; |
| 48 |
int last; |
| 49 |
int bump; |
| 50 |
int examine; |
| 51 |
int scan; |
| 52 |
int match; |
| 53 |
char ch; |
| 54 |
|
| 55 |
// We do the ToLower character by character for consistency. With surrogate chars, doing |
| 56 |
// a ToLower on the entire string could actually change the surrogate pair. This is more correct |
| 57 |
// linguistically, but since Regex doesn't support surrogates, it's more important to be |
| 58 |
// consistent. |
| 59 |
if (caseInsensitive) { |
| 60 |
StringBuilder sb = new StringBuilder(pattern.Length); |
| 61 |
for (int i=0; i<pattern.Length; i++) |
| 62 |
sb.Append(Char.ToLower(pattern[i], culture)); |
| 63 |
pattern = sb.ToString(); |
| 64 |
} |
| 65 |
|
| 66 |
_pattern = pattern; |
| 67 |
_rightToLeft = rightToLeft; |
| 68 |
_caseInsensitive = caseInsensitive; |
| 69 |
_culture = culture; |
| 70 |
|
| 71 |
if (!rightToLeft) { |
| 72 |
beforefirst = -1; |
| 73 |
last = pattern.Length - 1; |
| 74 |
bump = 1; |
| 75 |
} |
| 76 |
else { |
| 77 |
beforefirst = pattern.Length; |
| 78 |
last = 0; |
| 79 |
bump = -1; |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* PART I - the good-suffix shift table |
| 84 |
* |
| 85 |
* compute the positive requirement: |
| 86 |
* if char "i" is the first one from the right that doesn't match, |
| 87 |
* then we know the matcher can advance by _positive[i]. |
| 88 |
* |
| 89 |
* < |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
*/ |
| 97 |
_positive = new int[pattern.Length]; |
| 98 |
|
| 99 |
examine = last; |
| 100 |
ch = pattern[examine]; |
| 101 |
_positive[examine] = bump; |
| 102 |
examine -= bump; |
| 103 |
|
| 104 |
for (;;) { |
| 105 |
// find an internal char (examine) that matches the tail |
| 106 |
|
| 107 |
for (;;) { |
| 108 |
if (examine == beforefirst) |
| 109 |
goto OuterloopBreak; |
| 110 |
if (pattern[examine] == ch) |
| 111 |
break; |
| 112 |
examine -= bump; |
| 113 |
} |
| 114 |
|
| 115 |
match = last; |
| 116 |
scan = examine; |
| 117 |
|
| 118 |
// find the length of the match |
| 119 |
|
| 120 |
for (;;) { |
| 121 |
if (scan == beforefirst || pattern[match] != pattern[scan]) { |
| 122 |
// at the end of the match, note the difference in _positive |
| 123 |
// this is not the length of the match, but the distance from the internal match |
| 124 |
// to the tail suffix. |
| 125 |
if (_positive[match] == 0) |
| 126 |
_positive[match] = match - scan; |
| 127 |
|
| 128 |
// System.Diagnostics.Debug.WriteLine("Set positive[" + match + "] to " + (match - scan)); |
| 129 |
|
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
scan -= bump; |
| 134 |
match -= bump; |
| 135 |
} |
| 136 |
|
| 137 |
examine -= bump; |
| 138 |
} |
| 139 |
|
| 140 |
OuterloopBreak: |
| 141 |
|
| 142 |
match = last - bump; |
| 143 |
|
| 144 |
// scan for the chars for which there are no shifts that yield a different candidate |
| 145 |
|
| 146 |
/* < |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
*/ |
| 155 |
while (match != beforefirst) { |
| 156 |
if (_positive[match] == 0) |
| 157 |
_positive[match] = bump; |
| 158 |
|
| 159 |
match -= bump; |
| 160 |
} |
| 161 |
|
| 162 |
//System.Diagnostics.Debug.WriteLine("good suffix shift table:"); |
| 163 |
//for (int i=0; i<_positive.Length; i++) |
| 164 |
// System.Diagnostics.Debug.WriteLine("\t_positive[" + i + "] = " + _positive[i]); |
| 165 |
|
| 166 |
|
| 167 |
/* |
| 168 |
* PART II - the bad-character shift table |
| 169 |
* |
| 170 |
* compute the negative requirement: |
| 171 |
* if char "ch" is the reject character when testing position "i", |
| 172 |
* we can slide up by _negative[ch]; |
| 173 |
* (_negative[ch] = str.Length - 1 - str.LastIndexOf(ch)) |
| 174 |
* |
| 175 |
* the lookup table is divided into ASCII and Unicode portions; |
| 176 |
* only those parts of the Unicode 16-bit code set that actually |
| 177 |
* appear in the string are in the table. (Maximum size with |
| 178 |
* Unicode is 65K; ASCII only case is 512 bytes.) |
| 179 |
*/ |
| 180 |
|
| 181 |
_negativeASCII = new int[128]; |
| 182 |
|
| 183 |
for (int i = 0; i < 128; i++) |
| 184 |
_negativeASCII[i] = last - beforefirst; |
| 185 |
|
| 186 |
_lowASCII = 127; |
| 187 |
_highASCII = 0; |
| 188 |
|
| 189 |
for (examine = last; examine != beforefirst; examine -= bump) { |
| 190 |
ch = pattern[examine]; |
| 191 |
|
| 192 |
if (ch < 128) { |
| 193 |
if (_lowASCII > ch) |
| 194 |
_lowASCII = ch; |
| 195 |
|
| 196 |
if (_highASCII < ch) |
| 197 |
_highASCII = ch; |
| 198 |
|
| 199 |
if (_negativeASCII[ch] == last - beforefirst) |
| 200 |
_negativeASCII[ch] = last - examine; |
| 201 |
} |
| 202 |
else { |
| 203 |
int i = ch >> 8; |
| 204 |
int j = ch & 0xFF; |
| 205 |
|
| 206 |
if (_negativeUnicode == null) { |
| 207 |
_negativeUnicode = new int[256][]; |
| 208 |
} |
| 209 |
|
| 210 |
if (_negativeUnicode[i] == null) { |
| 211 |
int[] newarray = new int[256]; |
| 212 |
|
| 213 |
for (int k = 0; k < 256; k++) |
| 214 |
newarray[k] = last - beforefirst; |
| 215 |
|
| 216 |
if (i == 0) { |
| 217 |
System.Array.Copy(_negativeASCII, newarray, 128); |
| 218 |
_negativeASCII = newarray; |
| 219 |
} |
| 220 |
|
| 221 |
_negativeUnicode[i] = newarray; |
| 222 |
} |
| 223 |
|
| 224 |
if (_negativeUnicode[i][j] == last - beforefirst) |
| 225 |
_negativeUnicode[i][j] = last - examine; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
private bool MatchPattern(string text, int index) { |
| 231 |
if (_caseInsensitive) { |
| 232 |
if( text.Length - index < _pattern.Length) { |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
TextInfo textinfo = _culture.TextInfo; |
| 237 |
for( int i = 0; i < _pattern.Length; i++) { |
| 238 |
Debug.Assert(textinfo.ToLower(_pattern[i]) == _pattern[i], "pattern should be converted to lower case in constructor!"); |
| 239 |
if( textinfo.ToLower(text[index + i]) != _pattern[i]) { |
| 240 |
return false; |
| 241 |
} |
| 242 |
} |
| 243 |
return true; |
| 244 |
} |
| 245 |
else { |
| 246 |
return(0 == String.CompareOrdinal(_pattern, 0, text, index, _pattern.Length)); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
/* |
| 251 |
* When a regex is anchored, we can do a quick IsMatch test instead of a Scan |
| 252 |
*/ |
| 253 |
internal bool IsMatch(String text, int index, int beglimit, int endlimit) { |
| 254 |
|
| 255 |
if (!_rightToLeft) { |
| 256 |
if (index < beglimit || endlimit - index < _pattern.Length) |
| 257 |
return false; |
| 258 |
|
| 259 |
return MatchPattern(text, index); |
| 260 |
} |
| 261 |
else { |
| 262 |
if (index > endlimit || index - beglimit < _pattern.Length) |
| 263 |
return false; |
| 264 |
|
| 265 |
return MatchPattern(text, index - _pattern.Length); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
/* |
| 271 |
* Scan uses the Boyer-Moore algorithm to find the first occurrance |
| 272 |
* of the specified string within text, beginning at index, and |
| 273 |
* constrained within beglimit and endlimit. |
| 274 |
* |
| 275 |
* The direction and case-sensitivity of the match is determined |
| 276 |
* by the arguments to the RegexBoyerMoore constructor. |
| 277 |
*/ |
| 278 |
internal int Scan(String text, int index, int beglimit, int endlimit) { |
| 279 |
int test; |
| 280 |
int test2; |
| 281 |
int match; |
| 282 |
int startmatch; |
| 283 |
int endmatch; |
| 284 |
int advance; |
| 285 |
int defadv; |
| 286 |
int bump; |
| 287 |
char chMatch; |
| 288 |
char chTest; |
| 289 |
int[] unicodeLookup; |
| 290 |
|
| 291 |
if (!_rightToLeft) { |
| 292 |
defadv = _pattern.Length; |
| 293 |
startmatch = _pattern.Length - 1; |
| 294 |
endmatch = 0; |
| 295 |
test = index + defadv - 1; |
| 296 |
bump = 1; |
| 297 |
} |
| 298 |
else { |
| 299 |
defadv = -_pattern.Length; |
| 300 |
startmatch = 0; |
| 301 |
endmatch = -defadv - 1; |
| 302 |
test = index + defadv; |
| 303 |
bump = -1; |
| 304 |
} |
| 305 |
|
| 306 |
chMatch = _pattern[startmatch]; |
| 307 |
|
| 308 |
for (;;) { |
| 309 |
if (test >= endlimit || test < beglimit) |
| 310 |
return -1; |
| 311 |
|
| 312 |
chTest = text[test]; |
| 313 |
|
| 314 |
if (_caseInsensitive) |
| 315 |
chTest = Char.ToLower(chTest, _culture); |
| 316 |
|
| 317 |
if (chTest != chMatch) { |
| 318 |
if (chTest < 128) |
| 319 |
advance = _negativeASCII[chTest]; |
| 320 |
else if (null != _negativeUnicode && (null != (unicodeLookup = _negativeUnicode[chTest >> 8]))) |
| 321 |
advance = unicodeLookup[chTest & 0xFF]; |
| 322 |
else |
| 323 |
advance = defadv; |
| 324 |
|
| 325 |
test += advance; |
| 326 |
} |
| 327 |
else { // if (chTest == chMatch) |
| 328 |
test2 = test; |
| 329 |
match = startmatch; |
| 330 |
|
| 331 |
for (;;) { |
| 332 |
if (match == endmatch) |
| 333 |
return(_rightToLeft ? test2 + 1 : test2); |
| 334 |
|
| 335 |
match -= bump; |
| 336 |
test2 -= bump; |
| 337 |
|
| 338 |
chTest = text[test2]; |
| 339 |
|
| 340 |
if (_caseInsensitive) |
| 341 |
chTest = Char.ToLower(chTest, _culture); |
| 342 |
|
| 343 |
if (chTest != _pattern[match]) { |
| 344 |
advance = _positive[match]; |
| 345 |
if ((chTest & 0xFF80) == 0) |
| 346 |
test2 = (match - startmatch) + _negativeASCII[chTest]; |
| 347 |
else if (null != _negativeUnicode && (null != (unicodeLookup = _negativeUnicode[chTest >> 8]))) |
| 348 |
test2 = (match - startmatch) + unicodeLookup[chTest & 0xFF]; |
| 349 |
else { |
| 350 |
test += advance; |
| 351 |
break; |
| 352 |
} |
| 353 |
|
| 354 |
if (_rightToLeft ? test2 < advance : test2 > advance) |
| 355 |
advance = test2; |
| 356 |
|
| 357 |
test += advance; |
| 358 |
break; |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
/* |
| 366 |
* Used when dumping for debugging. |
| 367 |
*/ |
| 368 |
public override String ToString() { |
| 369 |
return _pattern; |
| 370 |
} |
| 371 |
|
| 372 |
#if DBG |
| 373 |
public String Dump(String indent) { |
| 374 |
StringBuilder sb = new StringBuilder(); |
| 375 |
|
| 376 |
sb.Append(indent + "BM Pattern: " + _pattern + "\n"); |
| 377 |
sb.Append(indent + "Positive: "); |
| 378 |
for (int i = 0; i < _positive.Length; i++) { |
| 379 |
sb.Append(_positive[i].ToString(CultureInfo.InvariantCulture) + " "); |
| 380 |
} |
| 381 |
sb.Append("\n"); |
| 382 |
|
| 383 |
if (_negativeASCII != null) { |
| 384 |
sb.Append(indent + "Negative table\n"); |
| 385 |
for (int i = 0; i < _negativeASCII.Length; i++) { |
| 386 |
if (_negativeASCII[i] != _pattern.Length) { |
| 387 |
sb.Append(indent + " " + Regex.Escape(Convert.ToString((char)i, CultureInfo.InvariantCulture)) + " " + _negativeASCII[i].ToString(CultureInfo.InvariantCulture) + "\n"); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
return sb.ToString(); |
| 393 |
} |
| 394 |
#endif |
| 395 |
} |
| 396 |
} |