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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
|
{
*****************************************************************************
This file is part of LazUtils.
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Implement TMask and related classes.
It is fast and optimized, and fully supports Unicode. Also supports
DOS/Windows compatible mask which behaves differently from standard mask.
Author: José Mejuto
Changes and improvements by Juha Manninen and Bart Broersma
}
unit Masks;
{$T-} // TODO: Fix compilation with -Sy.
{$mode objfpc}{$H+}
{.$define debug_maskcompiled}
{.$define debug_anycharornone}
interface
uses
Classes, SysUtils, Contnrs,
// LazUtils
LazUtilsStrConsts, LazUTF8;
type
{ EMaskError }
EMaskError=class(EConvertError)
public
type
TMaskExceptionCode=(mecInternalError,
mecInvalidCharMask,
mecMissingClose,
mecIncompleteMask,
mecInvalidEscapeChar,
mecInvalidUTF8Sequence
);
protected
FCode: TMaskExceptionCode;
public
constructor Create(const msg: string; aCode: TMaskExceptionCode);
constructor CreateFmt(const msg: string; args: array of const; aCode: TMaskExceptionCode);
property Code: TMaskExceptionCode read FCode;
end;
TMaskOpCode=(mocAnyChar, //treat ? as a wildcard to match exactly one char
mocAnyCharOrNone, //treat [?] to match any char or the absence of a char
mocAnyText, //treat * as a wildcard to mach zero or any mumber of chars
mocRange, //treat [a-c] to match either 'a', 'b' or 'c'. '-' is always treated as a range indicator.
//to have a literal '-' in a range, you must escape it with EscapeChar (defaults to '\'): [+-\-] matches '+', ',', or '-'.
mocSet, //treat [a-c] to match either 'a', '-' or 'c'
mocNegateGroup, //treat [!a-c] to not match 'a', 'b', or 'c', but match any other char. Requires mocRange and/or mocSet
mocEscapeChar //treat EscapeChar (defaults to '\') to take the next char as a literal, so '\*' is treated as a literal '*'.
);
TMaskOpCodes=set of TMaskOpCode;
const
AllMaskOpCodes=[mocAnyChar,
mocAnyCharOrNone,
mocAnyText,
mocRange,
mocSet,
mocNegateGroup,
mocEscapeChar];
// Match [x] literally, not as a range.
// Leave out mocAnyCharOrNone, mocRange and mocSet.
MaskOpCodesDisableRange=[mocAnyChar,
mocAnyText,
mocNegateGroup,
mocEscapeChar];
// Interpret [?] as literal question mark instead of 0..1 chars wildcard.
// Disable backslash escaping characters like "\?".
// Leave out mocAnyCharOrNone and mocEscapeChar
MaskOpCodesNoEscape=[mocAnyChar,
mocAnyText,
mocRange,
mocSet,
mocNegateGroup];
DefaultMaskOpCodes=MaskOpCodesNoEscape;
(*
Windows mask works in a different mode than regular mask, it has too many
quirks and corner cases inherited from CP/M, then adapted to DOS (8.3) file
names and adapted again for long file names.
Anyth?ng.abc = "?" matches exactly 1 char
Anyth*ng.abc = "*" matches 0 or more of chars
------- Quirks -------
*)
type
TWindowsQuirk=(wqAnyExtension, // Anything*.* : ".*" is removed. Also makes "foo.*" match "foo"
wqFilenameEnd, // Anything??.abc : "?" matches 1 or 0 chars (except '.')
// (Not the same as "Anything*.abc", but the same
// as regex "Anything.{0,2}\.abc")
// Internally converted to "Anything[??].abc"
wqExtension3More, // Anything.abc : Matches "Anything.abc" but also "Anything.abc*" (3 char extension)
// Anything.ab : Matches "Anything.ab" and never "anything.abcd"
// *.pas : Matches "Unit1.pas.bak". Not good.
wqEmptyIsAny, // "" : Empty string matches anything "*"
wqAllByExtension, // .abc : Runs as "*.abc" (Not in use anymore)
wqNoExtension); // Anything*. : Matches "Anything*" without extension
TWindowsQuirks=set of TWindowsQuirk;
const
AllWindowsQuirks=[wqAnyExtension,
wqFilenameEnd,
wqExtension3More,
wqEmptyIsAny,
wqAllByExtension,
wqNoExtension];
// Leave out wqExtension3More and wqAllByExtension
DefaultWindowsQuirks=[wqAnyExtension,
wqEmptyIsAny,
wqNoExtension];
type
// Backwards compatible options.
TMaskOption = (moCaseSensitive, moDisableSets);
TMaskOptions = set of TMaskOption;
{ TMaskBase }
TMaskBase = class
private
procedure SetAutoReverseRange(AValue: Boolean);
procedure SetCaseSensitive(AValue: Boolean);
procedure SetMaskEscapeChar(AValue: Char);
procedure SetMaskOpCodesAllowed(AValue: TMaskOpCodes);
protected
// Literal = It must match
// Range = Match any char in the range
// Negate = Negate match in a group
// AnyChar = It matches any char, but one must match
// AnyCharOrNone = Matches one or none char (only in a group)
// AnyCharToNext = Matches any chars amount, if fail, restart in the
// next position up to finish the mask or the matched string
// OptionalChar = Optional char
// CharsGroupBegin = Begin optional chars or ranges "["
// CharsGroupEnd = End optional chars or ranges "]"
type
TMaskParsedCode = (
Literal=0,
Range=1,
Negate=2,
AnyChar=3,
AnyCharOrNone=4,
AnyCharToNext=5,
OptionalChar=6,
CharsGroupBegin=10,
CharsGroupEnd=11
);
TMaskFailCause = (
mfcSuccess,
mfcMatchStringExhausted,
mfcMaskExhausted,
mfcMaskNotMatch,
mfcUnexpectedEnd
);
const
GROW_BY=100;
DefaultSpecialChars=['*', '?', '['];
procedure Add(aLength: integer; aData: PBYTE);
procedure Add(aValue: integer);
procedure Add(aValue: TMaskParsedCode);
procedure IncrementLastCounterBy(aOpcode: TMaskParsedCode; aValue: integer);
protected
fCaseSensitive: Boolean;
fAutoReverseRange: Boolean; // If enabled, range [z-a] is interpreted as [a-z]
fMaskIsCompiled: Boolean;
fMaskCompiled: TBytes;
fMaskCompiledIndex: integer;
fMaskCompiledAllocated: integer;
fMaskCompiledLimit: integer;
fMaskLimit: integer;
fMatchStringLimit: integer;
fMatchMinimumLiteralBytes: SizeInt;
fMatchMaximumLiteralBytes: SizeInt;
fMaskOpcodesAllowed: TMaskOpCodes;
// EscapeChar forces next char to be a literal one, not a wildcard.
fMaskEscapeChar: Char;
procedure PrepareCompile;
class procedure Exception_InvalidCharMask(const aMaskChar: string; aOffset: integer=-1); static;
class procedure Exception_MissingCloseChar(const aMaskChar: string; aOffset: integer=-1); static;
class procedure Exception_IncompleteMask(); static;
class procedure Exception_InvalidEscapeChar(); static;
procedure Exception_InternalError();
//function intfMatches(aMatchOffset: integer; aMaskIndex: integer): TMaskFailCause; virtual; abstract;
public
constructor Create(aCaseSensitive: Boolean=False;
aOpcodesAllowed: TMaskOpCodes=DefaultMaskOpCodes);
constructor Create(aOptions: TMaskOptions);
public
property CaseSensitive: Boolean read fCaseSensitive write SetCaseSensitive;
property AutoReverseRange: Boolean read fAutoReverseRange write SetAutoReverseRange;
property EscapeChar: Char read fMaskEscapeChar write SetMaskEscapeChar; //Must be lower ASCII (#0-#127)
property MaskOpCodes: TMaskOpCodes read fMaskOpcodesAllowed write SetMaskOpCodesAllowed;
end;
{ TMaskUTF8 }
TMaskUTF8 = class (TMaskBase)
private
fMatchString: String;
// Used by Compile.
fMaskInd: Integer;
fCPLength: integer; // Size of codepoint.
fLastOC: TMaskParsedCode; // Last OpCode.
fMask: String;
procedure AddAnyChar;
procedure AddLiteral;
procedure AddRange(lFirstRange, lSecondRange: Integer);
procedure AddRangeReverse(lFirstRange, lSecondRange: Integer);
procedure CompileRange;
procedure CompileEscapeCharPlusLiteral;
procedure CompileSpecialChars;
procedure CompileAnyCharOrNone(QChar: Char; BracketsRequired: Boolean);
function GetMask: String; virtual;
procedure SetMask(AValue: String); virtual;
protected
fOriginalMask: String;
function IsSpecialChar({%H-}AChar: Char): Boolean; virtual;
procedure CompileOtherSpecialChars; virtual;
class function CompareUTF8Sequences(const P1,P2: PChar): integer; static;
function intfMatches(aMatchOffset: integer; aMaskIndex: integer): TMaskFailCause; //override;
public
{$IFDEF debug_maskcompiled}
procedure DumpMaskCompiled;
{$ENDIF}
constructor Create(const aMask: String); overload;
constructor Create(const aMask: String; aCaseSensitive: Boolean); overload;
constructor Create(const aMask: String; aCaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes); virtual; overload;
constructor Create(const aMask: String; aOptions: TMaskOptions); overload;
deprecated 'Use Create with TMaskOpCodes params.'; // in Lazarus 2.3, remove in 2.5.
procedure Compile; virtual;
function Matches(const aStringToMatch: String): Boolean; virtual;
function MatchesWindowsMask(const AFileName: String): Boolean;
deprecated 'Create with TMaskWindows.Create, then call Matches.'; // in Lazarus 2.3, remove in 2.5.
public
property Mask: String read GetMask write SetMask;
end;
TMask = class(TMaskUTF8);
{ TWindowsMaskUTF8 }
TWindowsMaskUTF8=class(TMask)
private
procedure SetMask(AValue: String); override;
function GetMask: String; override;
procedure SetWindowsQuirkAllowed(AValue: TWindowsQuirks);
protected
fWindowsQuirkAllowed: TWindowsQuirks;
fWindowsQuirkInUse: TWindowsQuirks;
fWindowsMask: String;
procedure CompileOtherSpecialChars; override;
function IsSpecialChar(AChar: Char): Boolean; override;
class procedure SplitFileNameExtension(const aSourceFileName: String;
out aFileName: String; out aExtension: String; aIsMask: Boolean=False); static;
public
constructor Create(const aMask: String; aCaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes); override;
constructor Create(const aMask: String; aCaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes; aWindowsQuirksAllowed: TWindowsQuirks);
procedure Compile; override;
function Matches(const aFileName: String): Boolean; override;
public
property Quirks: TWindowsQuirks read fWindowsQuirkAllowed write SetWindowsQuirkAllowed;
end;
TWindowsMask = class(TWindowsMaskUTF8);
TMaskClass = class of TMaskUtf8;
{ TParseStringList }
TParseStringList = class(TStringList)
public
constructor Create(const AText, ASeparators: String);
end;
{ TMaskList }
TMaskList = class
private
fAutoReverseRange: Boolean;
fMasks: TObjectList;
FMaskClass: TMaskClass;
fMask: String;
fSeparator: Char;
fCaseSensitive: Boolean;
fMaskOpcodes: TMaskOpcodes;
function GetCount: Integer;
function GetItem(Index: Integer): TMask;
procedure SetAutoReverseRange(AValue: Boolean);
procedure SetCaseSensitive(AValue: Boolean);
procedure SetMask(AValue: String); virtual;
procedure SetMaskOpCodes(AValue: TMaskOpCodes);
protected
function GetMaskClass: TMaskClass; virtual;
procedure AddMasksToList(const aValue: String; aSeparator: Char; CaseSensitive: Boolean;
aOpcodesAllowed: TMaskOpCodes); virtual;
public
constructor Create(const aValue: String; aSeparator: Char=';'; CaseSensitive: Boolean=False;
aOpcodesAllowed: TMaskOpCodes=DefaultMaskOpCodes); virtual;
//Remove in 2.5
constructor Create(const aValue: String; aSeparator: Char; aOptions: TMaskOptions); virtual;
deprecated 'Use Create with TMaskOpcodes paramater';
destructor Destroy; override;
function Matches(const AFileName: String): Boolean;
// Deprecated in Lazarus 2.3, October 2021. Remove in 2.5.
function MatchesWindowsMask(const AFileName: String): Boolean;
deprecated 'Use a TWindowsMaskList instead.';
property Count: Integer read GetCount;
property Items[Index: Integer]: TMask read GetItem;
property Mask: String read fMask write SetMask;
property MaskOpCodes: TMaskOpCodes read fMaskOpCodes write SetMaskOpCodes;
property AutoReverseRange: Boolean read fAutoReverseRange write SetAutoReverseRange;
property CaseSensitive: Boolean read fCaseSensitive write SetCaseSensitive;
end;
{ TWindowsMaskList }
TWindowsMaskList = class(TMaskList)
private
fWindowsQuirks: TWindowsQuirks;
procedure SetQuirks(AValue: TWindowsQuirks);
protected
function GetMaskClass: TMaskClass; override;
procedure AddMasksToList(const aValue: String; aSeparator: Char; aCaseSensitive: Boolean;
aOpcodesAllowed: TMaskOpCodes); override;
public
constructor Create(const aValue: String; aSeparator: Char=';'; aCaseSensitive: Boolean=False;
aOpcodesAllowed: TMaskOpCodes=DefaultMaskOpCodes); override;
constructor Create(const aValue: String; aSeparator: Char{=';'}; aCaseSensitive: Boolean{=False};
aOpcodesAllowed: TMaskOpCodes{=DefaultMaskOpCodes};
aWindowsQuirksAllowed: TWindowsQuirks{=DefaultWindowsQuirks}); overload; //reintroduce;
//Remove in 2.5
constructor Create(const aValue: String; aSeparator: Char; aOptions: TMaskOptions); override;
deprecated 'Use Create with TMaskOpcodes paramater';
property Quirks: TWindowsQuirks read fWindowsQuirks write SetQuirks;
end;
TMaskListClass = class of TMaskList;
function MatchesMask(const FileName, Mask: String; CaseSensitive: Boolean=False;
aOpcodesAllowed: TMaskOpCodes=DefaultMaskOpCodes): Boolean;
function MatchesMask(const FileName, Mask: String; Options: TMaskOptions): Boolean;
deprecated 'Use MatchesMask with TMaskOpCodes params.'; // in Lazarus 2.3, remove in 2.5.
function MatchesWindowsMask(const FileName, Mask: String; CaseSensitive: Boolean=False;
aOpcodesAllowed: TMaskOpCodes=DefaultMaskOpCodes;
aWindowsQuirksAllowed: TWindowsQuirks=DefaultWindowsQuirks): Boolean;
function MatchesWindowsMask(const FileName, Mask: String; Options: TMaskOptions): Boolean;
function MatchesMaskList(const FileName, Mask: String; Separator: Char=';';
CaseSensitive: Boolean=False;
aOpcodesAllowed: TMaskOpCodes=DefaultMaskOpCodes): Boolean;
function MatchesMaskList(const FileName, Mask: String; Separator: Char;
Options: TMaskOptions): Boolean;
deprecated 'Use MatchesMaskList with TMaskOpCodes params.'; // in Lazarus 2.3, remove in 2.5.
function MatchesWindowsMaskList(const FileName, Mask: String; Separator: Char=';';
CaseSensitive: Boolean=False;
aOpcodesAllowed: TMaskOpCodes=DefaultMaskOpCodes;
aWindowsQuirksAllowed: TWindowsQuirks=DefaultWindowsQuirks): Boolean;
function MatchesWindowsMaskList(const FileName, Mask: String; Separator: Char;
Options: TMaskOptions): Boolean;
deprecated 'Use MatchesWindowsMaskList with TMaskOpCodes params.'; // in Lazarus 2.3, remove in 2.5.
function DbgS(O: TMaskOpCodes): String ; overload;
function DbgS(Q: TWindowsQuirks): String ; overload;
implementation
function DbgS(O: TMaskOpCodes): String ;
var
S: String;
Op: TMaskOpcode;
begin
Result := '[';
for Op in O do
begin
WriteStr(S, Op);
Result := Result + S + ',';
end;
if (Result[Length(Result)] = ',') then
System.Delete(Result, Length(Result), 1);
Result := Result + ']';
end;
function DbgS(Q: TWindowsQuirks): String ;
var
S: String;
Quirk: TWindowsQuirk;
begin
Result := '[';
for Quirk in Q do
begin
WriteStr(S, Quirk);
Result := Result + S + ',';
end;
if (Result[Length(Result)] = ',') then
System.Delete(Result, Length(Result), 1);
Result := Result + ']';
end;
function EncodeDisableRange(Options: TMaskOptions): TMaskOpCodes;
// Encode the Disable Range option from legacy TMaskOptions.
begin
if moDisableSets in Options then
Result:=MaskOpCodesDisableRange
else // Disable '\' escaping for legacy code.
Result:=MaskOpCodesNoEscape; //DefaultMaskOpCodes;
end;
function MatchesMask(const FileName, Mask: String; CaseSensitive: Boolean;
aOpcodesAllowed: TMaskOpCodes): Boolean;
var
AMask: TMask;
begin
AMask := TMask.Create(Mask, CaseSensitive, aOpcodesAllowed);
try
Result := AMask.Matches(FileName);
finally
AMask.Free;
end;
end;
function MatchesMask(const FileName, Mask: String; Options: TMaskOptions): Boolean;
begin
Result := MatchesMask(FileName, Mask, moCaseSensitive in Options,
EncodeDisableRange(Options));
end;
function MatchesWindowsMask(const FileName, Mask: String; CaseSensitive: Boolean;
aOpcodesAllowed: TMaskOpCodes; aWindowsQuirksAllowed: TWindowsQuirks): Boolean;
var
AMask: TWindowsMask;
begin
AMask := TWindowsMask.Create(Mask, CaseSensitive, aOpcodesAllowed, aWindowsQuirksAllowed);
try
Result := AMask.Matches(FileName);
finally
AMask.Free;
end;
end;
function MatchesWindowsMask(const FileName, Mask: String; Options: TMaskOptions): Boolean;
begin
Result := MatchesWindowsMask(FileName, Mask, moCaseSensitive in Options,
EncodeDisableRange(Options));
end;
function MatchesMaskList(const FileName, Mask: String; Separator: Char;
CaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes): Boolean;
var
AMaskList: TMaskList;
begin
AMaskList := TMaskList.Create(Mask, Separator, CaseSensitive, aOpcodesAllowed);
try
Result := AMaskList.Matches(FileName);
finally
AMaskList.Free;
end;
end;
function MatchesMaskList(const FileName, Mask: String; Separator: Char;
Options: TMaskOptions): Boolean;
begin
Result := MatchesMaskList(FileName, Mask, Separator, moCaseSensitive in Options,
EncodeDisableRange(Options));
end;
function MatchesWindowsMaskList(const FileName, Mask: String; Separator: Char;
CaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes; aWindowsQuirksAllowed: TWindowsQuirks): Boolean;
var
AMaskList: TWindowsMaskList;
begin
AMaskList := TWindowsMaskList.Create(Mask, Separator, CaseSensitive, aOpcodesAllowed, aWindowsQuirksAllowed);
try
Result := AMaskList.Matches(FileName);
finally
AMaskList.Free;
end;
end;
function MatchesWindowsMaskList(const FileName, Mask: String; Separator: Char;
Options: TMaskOptions): Boolean;
begin
Result := MatchesWindowsMaskList(FileName, Mask, Separator, moCaseSensitive in Options,
EncodeDisableRange(Options));
end;
{ TWindowsMaskList }
procedure TWindowsMaskList.SetQuirks(AValue: TWindowsQuirks);
var
i: Integer;
begin
if fWindowsQuirks = AValue then Exit;
fWindowsQuirks := AValue;
//if (wqFilenameEnd in fWindowsQuirks) then
// Include(fMaskOpcodes, mocAnyCharOrNone);
for i := 0 to fMasks.Count - 1 do
begin
TWindowsMask(fMasks.Items[i]).Quirks := FWindowsQuirks;
TWindowsMask(fMasks.Items[i]).MaskOpCodes := fMaskOpcodes;
//TWindowsMask(fMasks.Items[i]).Compile; //to apply Quirks
end;
//fMasks.Clear;
//AddMasksToList(fMask, fSeparator, fCaseSensitive, fMaskOpCodes);
end;
function TWindowsMaskList.GetMaskClass: TMaskClass;
begin
Result := TWindowsMask;
end;
procedure TWindowsMaskList.AddMasksToList(const aValue: String;
aSeparator: Char; aCaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes);
var
i: Integer;
begin
inherited AddMasksToList(aValue, aSeparator, aCaseSensitive, aOpcodesAllowed);
if (FWindowsQuirks <> DefaultWindowsQuirks) then //inherited did not pass Quirks to the constructor
begin
for i := 0 to fMasks.Count - 1 do
begin
TWindowsMask(fMasks.Items[i]).fWindowsQuirkAllowed := FWindowsQuirks;
//TWindowsMask(fMasks.Items[i]).Compile; //to apply Quirks
end;
end;
end;
constructor TWindowsMaskList.Create(const aValue: String; aSeparator: Char;
aCaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes);
begin
Create(aValue, aSeparator, aCaseSensitive, aOpcodesAllowed, DefaultWindowsQuirks);
end;
constructor TWindowsMaskList.Create(const aValue: String; aSeparator: Char;
aCaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes;
aWindowsQuirksAllowed: TWindowsQuirks);
begin
fWindowsQuirks := aWindowsQuirksAllowed;
//if (wqFilenameEnd in aWindowsQuirksAllowed) then
// Include(aOpcodesAllowed, mocAnyCharOrNone);
inherited Create(aValue, aSeparator, aCaseSensitive, aOpcodesAllowed);
end;
constructor TWindowsMaskList.Create(const aValue: String; aSeparator: Char;
aOptions: TMaskOptions);
begin
Create(aValue, aSeparator, (moCaseSensitive in aOptions), DefaultMaskOpCodes, DefaultWindowsQuirks);
end;
{ EMaskError }
constructor EMaskError.Create(const msg: string; aCode: TMaskExceptionCode);
begin
CreateFmt(msg,[],aCode);
end;
constructor EMaskError.CreateFmt(const msg: string; args: array of const;
aCode: TMaskExceptionCode);
begin
FCode:=aCode;
Inherited CreateFmt(msg,args);
end;
{ TMaskBase }
procedure TMaskBase.SetAutoReverseRange(AValue: Boolean);
begin
if fAutoReverseRange = AValue then Exit;
fAutoReverseRange := AValue;
fMaskIsCompiled := False;
end;
procedure TMaskBase.SetCaseSensitive(AValue: Boolean);
begin
if fCaseSensitive = AValue then Exit;
fCaseSensitive := AValue;
fMaskIsCompiled := False;
end;
procedure TMaskBase.SetMaskEscapeChar(AValue: Char);
begin
if fMaskEscapeChar=AValue then Exit;
if fMaskEscapeChar>#127 then
Exception_InvalidEscapeChar();
fMaskEscapeChar:=AValue;
fMaskIsCompiled:=False;
end;
procedure TMaskBase.SetMaskOpCodesAllowed(AValue: TMaskOpCodes);
begin
if fMaskOpcodesAllowed = AValue then Exit;
fMaskOpcodesAllowed := AValue;
fMaskIsCompiled := False;
end;
procedure TMaskBase.Add(aLength: integer; aData: PBYTE);
var
lCounter: integer;
begin
if fMaskCompiledIndex+aLength>=fMaskCompiledAllocated then begin
fMaskCompiledAllocated:=fMaskCompiledAllocated+aLength+GROW_BY;
SetLength(fMaskCompiled,fMaskCompiledAllocated);
end;
for lCounter := 0 to Pred(aLength) do begin
fMaskCompiled[fMaskCompiledIndex]:=(aData+lCounter)^;
inc(fMaskCompiledIndex);
end;
end;
procedure TMaskBase.Add(aValue: integer);
begin
Add(sizeof(aValue),@aValue);
end;
procedure TMaskBase.Add(aValue: TMaskParsedCode);
var
v: BYTE;
begin
//writeln('Add: ',aValue);
v:=BYTE(aValue);
Add(1,@v);
end;
procedure TMaskBase.IncrementLastCounterBy(aOpcode: TMaskParsedCode; aValue: integer);
var
p: PInteger;
begin
//writeln('TMaskBase.IncrementLastCounterBy: aOPcode=',aOpcode,', aValue=',aValue);
fMaskCompiledIndex:=fMaskCompiledIndex-sizeof(aValue);
if TMaskParsedCode(fMaskCompiled[fMaskCompiledIndex-1])<>aOpcode then
Exception_InternalError();
P:=@fMaskCompiled[fMaskCompiledIndex];
Add(P^+aValue);
end;
procedure TMaskBase.PrepareCompile;
begin
fMaskCompiled := nil;
fMaskCompiledAllocated := 0;
fMaskCompiledIndex := 0;
fMaskIsCompiled:=False;
end;
class procedure TMaskBase.Exception_InvalidCharMask(const aMaskChar: string;
aOffset: integer);
begin
if aOffset>=0 then
raise EMaskError.CreateFmt(rsInvalidCharMaskAt, [aMaskChar, aOffset], mecInvalidCharMask)
else
raise EMaskError.CreateFmt(rsInvalidCharMask, [aMaskChar], mecInvalidCharMask);
end;
class procedure TMaskBase.Exception_MissingCloseChar(const aMaskChar: string;
aOffset: integer);
begin
if aOffset>=0 then
raise EMaskError.CreateFmt(rsMissingCloseCharMaskAt, [aMaskChar, aOffset], mecMissingClose)
else
raise EMaskError.CreateFmt(rsMissingCloseCharMask, [aMaskChar], mecMissingClose);
end;
class procedure TMaskBase.Exception_IncompleteMask();
begin
raise EMaskError.CreateFmt(rsIncompleteMask, [], mecIncompleteMask);
end;
class procedure TMaskBase.Exception_InvalidEscapeChar();
begin
raise EMaskError.Create(rsInvalidEscapeChar, mecInvalidEscapeChar);
end;
procedure TMaskBase.Exception_InternalError();
begin
raise EMaskError.CreateFmt(rsInternalError, [self.ClassName], mecInternalError);
end;
constructor TMaskBase.Create(aCaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes);
begin
fCaseSensitive:=aCaseSensitive;
fMaskOpcodesAllowed:=aOpcodesAllowed;
fAutoReverseRange:=True;
fMaskEscapeChar:='\';
end;
constructor TMaskBase.Create(aOptions: TMaskOptions);
begin
Create(moCaseSensitive in aOptions, EncodeDisableRange(aOptions));
end;
{ TMask }
procedure TMaskUTF8.AddAnyChar;
begin
Add(TMaskParsedCode.AnyChar);
inc(fMatchMinimumLiteralBytes,1);
if fMatchMaximumLiteralBytes<High(fMatchMaximumLiteralBytes) then
inc(fMatchMaximumLiteralBytes,4);
fLastOC:=TMaskParsedCode.AnyChar;
end;
procedure TMaskUTF8.AddLiteral;
begin
Add(TMaskParsedCode.Literal);
Add(fCPLength,@fMask[fMaskInd]);
inc(fMatchMinimumLiteralBytes,fCPLength);
if fMatchMaximumLiteralBytes<High(fMatchMaximumLiteralBytes) then
inc(fMatchMaximumLiteralBytes,fCPLength);
fLastOC:=TMaskParsedCode.Literal;
end;
procedure TMaskUTF8.AddRange(lFirstRange, lSecondRange: Integer);
begin
fCPLength:=UTF8CodepointSizeFast(@fMask[lFirstRange]);
Add(fCPLength,@fMask[lFirstRange]);
fCPLength:=UTF8CodepointSizeFast(@fMask[lSecondRange]);
Add(fCPLength,@fMask[lSecondRange]);
fMaskInd:=lSecondRange;
end;
procedure TMaskUTF8.AddRangeReverse(lFirstRange, lSecondRange: Integer);
begin
fCPLength:=UTF8CodepointSizeFast(@fMask[lSecondRange]);
Add(fCPLength,@fMask[lSecondRange]);
Add(UTF8CodepointSizeFast(@fMask[lFirstRange]),@fMask[lFirstRange]);
fMaskInd:=lSecondRange;
end;
procedure TMaskUTF8.SetMask(AValue: String);
begin
if fOriginalMask = AValue then Exit;
fOriginalMask := AValue;
fMaskIsCompiled := False;
end;
function TMaskUTF8.IsSpecialChar(AChar: Char): Boolean;
begin
Result := False
end;
procedure TMaskUTF8.CompileOtherSpecialChars;
begin
//Nothing to do
end;
procedure TMaskUTF8.CompileRange;
function IsARange(aPosition: integer; out aFirstSequence: integer; out aSecondSequence: integer): Boolean;// {$IFDEF USE_INLINE}inline;{$ENDIF}
var
llCPL: integer;
begin
Result:=false;
aFirstSequence:=0;
aSecondSequence:=0;
if (mocEscapeChar in FMaskOpcodesAllowed) and (fMask[aPosition]=FMaskEscapeChar) then begin
llCPL:=UTF8CodepointSizeFast(@fMask[aPosition]);
if aPosition+llCPL>fMaskLimit then exit; // ==>
inc(aPosition,llCPL);
aFirstSequence:=aPosition;
end else begin
aFirstSequence:=aPosition;
end;
llCPL:=UTF8CodepointSizeFast(@fMask[aPosition]);
inc(aPosition,llCPL);
if aPosition+llCPL>fMaskLimit then exit; // ==>
// It is not a range, return false
if fMask[aPosition]<>'-' then exit;
llCPL:=UTF8CodepointSizeFast(@fMask[aPosition]);
inc(aPosition,llCPL);
if (mocEscapeChar in FMaskOpcodesAllowed) and (fMask[aPosition]=FMaskEscapeChar) then begin
llCPL:=UTF8CodepointSizeFast(@fMask[aPosition]);
if aPosition+llCPL>fMaskLimit then exit; // ==>
inc(aPosition,llCPL);
aSecondSequence:=aPosition;
end else begin
aSecondSequence:=aPosition;
end;
Result:=true;
end;
var
lCharsGroupInsertSize, lFirstRange, lSecondRange: integer;
begin
//writeln('CompileRange: fMask[fMaskInd]=',fMask[fMaskInd]);
if ([mocSet,mocRange,mocAnyCharOrNone]*fMaskOpCodesAllowed = [mocAnyCharOrNone]) or
((mocAnyCharOrNone in fMaskOpcodesAllowed) and (fMaskInd<fMaskLimit) and (fMask[fMaskInd+1]='?'))
then begin
CompileAnyCharOrNone('?', True);
end else begin//not AnyCharOrNone
fLastOC:=TMaskParsedCode.CharsGroupBegin;
Add(TMaskParsedCode.CharsGroupBegin);
inc(fMatchMinimumLiteralBytes,1);
if fMatchMaximumLiteralBytes<High(fMatchMaximumLiteralBytes) then
begin inc(fMatchMaximumLiteralBytes,4); end;
lCharsGroupInsertSize:=fMaskCompiledIndex;
Add(0);
inc(fMaskInd); // CP length is 1 because it is "["
if fMaskInd<fMaskLimit then begin
if (fMask[fMaskInd]='!') and (mocNegateGroup in fMaskOpcodesAllowed) then begin
Add(TMaskParsedCode.Negate);
inc(fMaskInd); // CP length is 1 because it is "!"
fLastOC:=TMaskParsedCode.Negate;
end;
end;
while fMaskInd<=fMaskLimit do begin //while
fCPLength:=UTF8CodepointSizeFast(@fMask[fMaskInd]);
if (mocRange in fMaskOpcodesAllowed) and IsARange(fMaskInd,lFirstRange,lSecondRange) then begin //is a range
Add(TMaskParsedCode.Range);
// Check if reverse range is needed
if (not fAutoReverseRange)
or (CompareUTF8Sequences(@fMask[lFirstRange],@fMask[lSecondRange])<0) then
AddRange(lFirstRange, lSecondRange)
else
AddRangeReverse(lFirstRange, lSecondRange);
fLastOC:=TMaskParsedCode.Range;
end //is a range
else if fMask[fMaskInd]=']' then begin //end of range or set
if (fLastOC=TMaskParsedCode.CharsGroupBegin) or (fLastOC=TMaskParsedCode.Negate) then
begin //empty set or range
//writeln('CompileRange: empty match');
Exception_InvalidCharMask(fMask[fMaskInd],fMaskInd); //Error empty match
end; //empty set or range
// Insert the new offset in case of a positive match in CharsGroup
PInteger(@fMaskCompiled[lCharsGroupInsertSize])^:=fMaskCompiledIndex;
Add(TMaskParsedCode.CharsGroupEnd);
fLastOC:=TMaskParsedCode.CharsGroupEnd;
break;
end // end of range or set
else begin //not a range, not AnyCharOrNone, must be a set
//Note: why not raise an exception right here if mocSet is NOT enabled?
// handle escaping if mocSet is enabled, but mocRange not
if (fMask[fMaskInd]=FMaskEscapeChar) and (mocEscapeChar in fMaskOpcodesAllowed) then begin //escaped literal in set
// next is optional char in set or literal, consume the EscapeChar
inc(fMaskInd,fCPLength);
if fMaskInd<=fMaskLimit then begin
fCPLength:=UTF8CodepointSizeFast(@fMask[fMaskInd]);
end else begin
//writeln('CompileRange: incomplete mask');
Exception_IncompleteMask();
end;
end;//escaped literal in set
if (mocSet in fMaskOpcodesAllowed) then begin //add to set
Add(TMaskParsedCode.OptionalChar);
Add(fCPLength,@fMask[fMaskInd]);
fLastOC:=TMaskParsedCode.OptionalChar;
end //add to set
else begin // mocRange enabled but IsRange=False, mocSet disabled
Exception_InvalidCharMask(fMask[fMaskInd],fMaskInd);
end;
end; //not a range, not AnyCharOrNone, must be a set
inc(fMaskInd,fCPLength);
end;//while
if fMaskInd>fMaskLimit then
Exception_MissingCloseChar(']',fMaskLimit);
end;//not AnyCharOrNone
//writeln('CompileRange end: fMask[fMaskInd]=',fMask[fMaskInd]);
end;
function TMaskUTF8.GetMask: String;
begin
Result := fOriginalMask;
end;
procedure TMaskUtf8.CompileEscapeCharPlusLiteral;
begin
inc(fMaskInd,fCPLength);
if fMaskInd<=fMaskLimit then begin
fCPLength:=UTF8CodepointSizeFast(@fMask[fMaskInd]);
AddLiteral;
inc(fMaskInd,fCPLength);
end
else
Exception_IncompleteMask();
end;
procedure TMaskUtf8.CompileSpecialChars;
begin
case fMask[fMaskInd] of
'*':
begin
if mocAnyText in fMaskOpcodesAllowed then begin
if fLastOC<>TMaskParsedCode.AnyCharToNext then begin
Add(TMaskParsedCode.AnyCharToNext);
fLastOC:=TMaskParsedCode.AnyCharToNext;
// * = No limit
fMatchMaximumLiteralBytes:=High(fMatchMaximumLiteralBytes);
end;
end
else
AddLiteral;
end;
'?':
begin
if mocAnyChar in fMaskOpcodesAllowed then
AddAnyChar
else
AddLiteral;
end;
'[':
begin
if ([mocSet,mocRange,
mocAnyCharOrNone] * fMaskOpcodesAllowed <> [])
//in this case the '[' always mus be the start of a Range, a Set or AnyCharOrNone
then
begin //it's just a bit easier later on if we handle this before we call CompileRange
//if ([mocSet,mocRange]*fMaskOpCodesAllowed = []) {only mocAnyCharOrNone enabled}
// and (fMaskInd<fMaskLimit) and (fMask[fMaskInd+1]<>'?') {next char is not '?'} then
// Exception_InvalidCharMask(fMask[fMaskInd+1], fMaskInd+1);
CompileRange;
end
else begin //mocSet,MocRange and mocAnyCharOrNone are all disabled
{
if (fMask[fMaskInd]=FMaskEscapeChar) and (mocEscapeChar in FMaskOpcodesAllowed) then begin //DEAD CODE?
//This codepath could only be chosen if, at this point '[' is the escapechar
//but, if that is the case, it should already have been handled in Compile
//and CompileRange should not have been called.
//Maybe I'm wrong, so I just comment this section out instead of
//completely removing it
// next is Literal
inc(fMaskInd,fCPLength);
if fMaskInd<=fMaskLimit then begin
fCPLength:=UTF8CodepointSizeFast(@fMask[fMaskInd]);
end else begin
Exception_IncompleteMask();
end;
end; //DEAD CODE??
}
AddLiteral;
end;
end;
otherwise
begin
CompileOtherSpecialChars;
end;
end;
end;
procedure TMaskUTF8.CompileAnyCharOrNone(QChar: Char; BracketsRequired: Boolean);
var
QCount, lCharsGroupInsertSize: Integer;
begin
//if any of the 2 conditions is true, this procedure should not have been called.
{$IFDEF debug_anycharornone}
writeln('CompileAnyCharOrNone: QChar=#',Ord(QChar),', BracketsRequired=',BracketsRequired,', fMask[fMaskInd]=',fMask[fMaskInd]);
if (BracketsRequired and (fMask[fMaskInd]<>'[')) or ((not BracketsRequired) and (fMask[fMaskInd]<>QChar)) then
Exception_InternalError();
{$ENDIF}
if BracketsRequired then
begin
Inc(fMaskInd); //consume the '['
//the following happens when mocSet amd mocRange are disabled and the first char after the bracket is not a '?'
if fMask[fMaskInd]<>QChar then
Exception_InvalidCharMask(fMask[fMaskInd], fMaskInd);
end;
QCount:=1;
while (fMaskInd+QCount<=fMaskLimit) and (fMask[fMaskInd+QCount]=QChar) do Inc(QCount);
{$IFDEF debug_anycharornone}
writeln('CompileAnyCharOrNone: Nr of AnyCharOrNone-tokens: ',QCount);
if (fMaskInd+QCount>fMaskLimit) then writeln('(fMaskInd+QCount>fMaskLimit): ',fMaskInd+QCount,'>',fMaskLimit);
{$ENDIF}
//is Last found QChar also last character of the mask, while we expect a closing ']'?
if BracketsRequired and (fMaskInd+QCount>fMaskLimit) then
Exception_MissingCloseChar(']',fMaskInd+QCount-1);
{$IFDEF debug_anycharornone}
if not (fMask[fMaskInd+QCount]=']') then writeln('fMask[fMaskInd+QCount]: expected ], found: ',fMask[fMaskInd+QCount]);
{$ENDIF}
if BracketsRequired and not (fMask[fMaskInd+QCount]=']') then
Exception_InvalidCharMask(fMask[fMaskInd+QCount],fMaskInd+QCount);
Add(TMaskParsedCode.CharsGroupBegin);
lCharsGroupInsertSize:=fMaskCompiledIndex;
Add(0);
Add(TMaskParsedCode.AnyCharOrNone);
Add(1);
if QCount>1 then
IncrementLastCounterBy(TMaskParsedCode.AnyCharOrNone,QCount-1);
PInteger(@fMaskCompiled[lCharsGroupInsertSize])^:=fMaskCompiledIndex;
Add(TMaskParsedCode.CharsGroupEnd);
fLastOC:=TMaskParsedCode.CharsGroupEnd;
Inc(fMatchMaximumLiteralBytes,QCount*4);
if BracketsRequired then
Inc(fMaskInd,QCount) //go to ending ']'
else
Inc(fMaskInd,QCount-1); //go to last found QChar
{$IFDEF debug_anycharornone}
write('fMaskInd=',fMaskInd,', fMaskLimit=',fMaskLimit,' fMask[fMaskInd]=');if fMaskInd<=fMaskLimit then writeln('#',Ord(fMask[fMaskInd]),': ',fMask[fMaskInd])else writeln('>>');
writeln('CompileAnyCharOrNone end.');
{$ENDIF}
end;
procedure TMaskUTF8.Compile;
begin
PrepareCompile;
if fCaseSensitive then
fMask:=fOriginalMask
else
fMask:=UTF8LowerCase(fOriginalMask);
fMaskLimit:=Length(fMask);
fLastOC:=TMaskParsedCode.Literal;
SetLength(fMaskCompiled,0);
fMaskInd:=1;
while fMaskInd<=fMaskLimit do begin //while
fCPLength:=UTF8CodepointSizeFast(@fMask[fMaskInd]);
if (mocEscapeChar in fMaskOpcodesAllowed) and (fMask[fMaskInd]=fMaskEscapeChar) then
CompileEscapeCharPlusLiteral
else begin // not an escaped literal
//writeln('Compile while: fMask[',fMaskInd,']=',fMask[fMaskInd]);
if (fMask[fMaskInd] in DefaultSpecialChars) or IsSpecialChar(fMask[fMaskInd]) then begin // handle special chars
CompileSpecialChars;
//writeln('After CompileSpecialChar');
//write('fMaskInd=',fMaskInd,', fMaskLimit=',fMaskLimit,' fMask[fMaskInd]=');if fMaskInd<=fMaskLimit then writeln('#',Ord(fMask[fMaskInd]),': ',fMask[fMaskInd])else writeln('>>');
end //handle special chars
else
begin
//writeln('Compile: AddLiteral: fMask[',fMaskInd,']=',fMask[fMaskInd]);
AddLiteral;
end;
inc(fMaskInd,fCPLength);
end; //not an escaped literal
end; //while
SetLength(fMaskCompiled,fMaskCompiledIndex);
fMaskCompiledLimit:=fMaskCompiledIndex-1;
{
fMaskIsCompiled Indicates that Compile was succesfull
If you override this method make sure that it is only set to True if
the overridden method also succeeds!
}
fMaskIsCompiled:=True;
//writeln('Compile end.');
{$IFDEF debug_maskcompiled}
writeln('fMaskInd=',fMaskInd,', fMaskLimit=',fMaskLimit);
writeln('fMaskCompiled:');
writeln('fMaskCompiledLimit=',fMaskCompiledLimit);
writeln('fMaskCompiledIndex=',fMaskCompiledIndex);
DumpMaskCompiled;
{$ENDIF}
end;
class function TMaskUTF8.CompareUTF8Sequences(const P1, P2: PChar): integer;
var
l1,l2: integer;
l: integer;
begin
l1:=UTF8CodepointSizeFast(p1);
l2:=UTF8CodepointSizeFast(p2);
Result:=0;
l:=0;
while (l<l1) and (l<l2) do begin
Result:=Integer((P1+l)^)-integer((P2+l)^);
if Result<>0 then exit;
inc(l);
end;
Result:=l1-l2;
end;
function TMaskUTF8.intfMatches(aMatchOffset: integer; aMaskIndex: integer): TMaskFailCause;
var
c1,c2: PChar;
lFailCause: TMaskFailCause;
lNegateCharGroup: Boolean;
lSkipOnSuccessGroup: integer;
t1: Boolean;
j: integer;
lTryCounter: integer;
begin
lSkipOnSuccessGroup:=0;
Result:=mfcUnexpectedEnd;
lNegateCharGroup:=false;
while aMaskIndex<=fMaskCompiledLimit do begin
case TMaskParsedCode(fMaskCompiled[aMaskIndex]) of
TMaskParsedCode.Literal:
begin
if aMatchOffset>fMatchStringLimit then
exit(TMaskFailCause.mfcMatchStringExhausted); // Error, no char to match.
inc(aMaskIndex);
if CompareUTF8Sequences(@fMaskCompiled[aMaskIndex],@fMatchString[aMatchOffset])<>0 then
exit(TMaskFailCause.mfcMaskNotMatch);
inc(aMaskIndex,UTF8CodepointSizeFast(@fMaskCompiled[aMaskIndex]));
inc(aMatchOffset,UTF8CodepointSizeFast(@fMatchString[aMatchOffset]));
end;
TMaskParsedCode.AnyChar:
begin
inc(aMaskIndex);
if aMatchOffset>fMatchStringLimit then
exit(TMaskFailCause.mfcMatchStringExhausted); // Error, no char to match.
inc(aMatchOffset,UTF8CodepointSizeFast(@fMatchString[aMatchOffset]));
end;
TMaskParsedCode.Negate:
begin
lNegateCharGroup:=true;
inc(aMaskIndex);
end;
TMaskParsedCode.CharsGroupBegin:
begin
lNegateCharGroup:=false;
inc(aMaskIndex);
lSkipOnSuccessGroup:=PInteger(@fMaskCompiled[aMaskIndex])^;
inc(aMaskIndex,sizeof(integer));
end;
TMaskParsedCode.CharsGroupEnd:
begin
if lNegateCharGroup then begin
aMaskIndex:=lSkipOnSuccessGroup+1;
inc(aMatchOffset,UTF8CodepointSizeFast(@fMatchString[aMatchOffset]));
end
else
exit(TMaskFailCause.mfcMaskNotMatch);
end;
TMaskParsedCode.OptionalChar:
begin
inc(aMaskIndex);
if aMatchOffset>fMatchStringLimit then
exit(TMaskFailCause.mfcMatchStringExhausted); // Error, no char to match.
if CompareUTF8Sequences(@fMaskCompiled[aMaskIndex],@fMatchString[aMatchOffset])=0 then begin
if lNegateCharGroup then
exit(TMaskFailCause.mfcMaskNotMatch);
aMaskIndex:=lSkipOnSuccessGroup+1;
inc(aMatchOffset,UTF8CodepointSizeFast(@fMatchString[aMatchOffset]));
end
else
inc(aMaskIndex,UTF8CodepointSizeFast(@fMaskCompiled[aMaskIndex]));
end;
TMaskParsedCode.Range:
begin
if aMatchOffset>fMatchStringLimit then
exit(TMaskFailCause.mfcMatchStringExhausted); // Error, no char to match.
inc(aMaskIndex);
c1:=@fMaskCompiled[aMaskIndex];
inc(aMaskIndex,UTF8CodepointSizeFast(C1));
c2:=@fMaskCompiled[aMaskIndex];
inc(aMaskIndex,UTF8CodepointSizeFast(C2));
t1:=(CompareUTF8Sequences(@fMatchString[aMatchOffset],c1)>=0) and
(CompareUTF8Sequences(@fMatchString[aMatchOffset],c2)<=0);
if t1 then begin
if not lNegateCharGroup then begin
//Jump to CharsGroupEnd+1 because if CharsGroupEnd is reached
//it means that all optional chars and ranges have not matched the string.
aMaskIndex:=lSkipOnSuccessGroup+1;
inc(aMatchOffset,UTF8CodepointSizeFast(@fMatchString[aMatchOffset]));
end
else
exit(TMaskFailCause.mfcMaskNotMatch);
end
end;
TMaskParsedCode.AnyCharToNext:
begin
// if last is "*", everything in remain data matches
if aMaskIndex=fMaskCompiledLimit then
exit(TMaskFailCause.mfcSuccess);
if aMatchOffset>fMatchStringLimit then begin
if aMaskIndex=fMaskCompiledLimit then
exit(TMaskFailCause.mfcSuccess);
exit(TMaskFailCause.mfcMatchStringExhausted);
end;
inc(aMaskIndex);
while aMatchOffset<=fMatchStringLimit do begin
lFailCause:=intfMatches(aMatchOffset,aMaskIndex);
if lFailCause=TMaskFailCause.mfcSuccess then
exit(TMaskFailCause.mfcSuccess)
else if lFailCause=TMaskFailCause.mfcMatchStringExhausted then
exit(TMaskFailCause.mfcMatchStringExhausted);
inc(aMatchOffset,UTF8CodepointSizeFast(@fMatchString[aMatchOffset]));
end;
exit(TMaskFailCause.mfcMatchStringExhausted);
end;
TMaskParsedCode.AnyCharOrNone:
begin
inc(aMaskIndex);
lTryCounter:=PInteger(@fMaskCompiled[aMaskIndex])^;
inc(aMaskIndex,sizeof(integer));
if TMaskParsedCode(fMaskCompiled[aMaskIndex])<>TMaskParsedCode.CharsGroupEnd then
begin //writeln('TMaskUtf8.infMatches: error parsing AnyCharOrNone, missing CharsGroupEnd, fMaskCompiled[',aMaskIndex,']=',fMaskCompiled[aMaskIndex]);
Exception_InternalError() end
else
aMaskIndex:=lSkipOnSuccessGroup+1;
// Try to match remain mask eating, 0,1,2,...,lTryCounter chars.
for j := 0 to lTryCounter do begin
if aMatchOffset>fMatchStringLimit then begin
if aMaskIndex=fMaskCompiledLimit+1 then
exit(TMaskFailCause.mfcSuccess);
exit(TMaskFailCause.mfcMatchStringExhausted);
end;
lFailCause:=intfMatches(aMatchOffset,aMaskIndex);
if lFailCause=TMaskFailCause.mfcSuccess then
exit(TMaskFailCause.mfcSuccess)
else if lFailCause=TMaskFailCause.mfcMatchStringExhausted then
exit(TMaskFailCause.mfcMatchStringExhausted);
inc(aMatchOffset,UTF8CodepointSizeFast(@fMatchString[aMatchOffset]));
end;
exit(TMaskFailCause.mfcMatchStringExhausted);
end;
else // case
begin
writeln('TMaskUtf8.infMatches: XXXX InternalError');
Exception_InternalError();
end;
end;
end;
if (aMaskIndex>fMaskCompiledLimit) and (aMatchOffset>fMatchStringLimit) then
Result:=TMaskFailCause.mfcSuccess
else if aMaskIndex>fMaskCompiledLimit then
Result:=TMaskFailCause.mfcMaskExhausted
else
Result:=TMaskFailCause.mfcMatchStringExhausted;
end;
{$IFDEF debug_maskcompiled}
procedure TMaskUTF8.DumpMaskCompiled;
var
i: Integer;
b: Byte;
begin
for i := low(fMaskCompiled) to fMaskCompiledIndex-1 do
begin
b := fMaskCompiled[i];
writeln(i:2,': ',b:3);
end;
end;
{$ENDIF}
constructor TMaskUTF8.Create(const aMask: String);
begin
Create(aMask, False, DefaultMaskOpCodes);
end;
constructor TMaskUTF8.Create(const aMask: String; aCaseSensitive: Boolean);
begin
Create(aMask, aCaseSensitive, DefaultMaskOpCodes);
end;
constructor TMaskUTF8.Create(const aMask: String;
aCaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes);
begin
inherited Create(aCaseSensitive,aOpcodesAllowed);
fOriginalMask:=aMask;
end;
constructor TMaskUTF8.Create(const aMask: String; aOptions: TMaskOptions);
begin
inherited Create(aOptions);
fOriginalMask:=aMask;
end;
function TMaskUTF8.Matches(const aStringToMatch: String): Boolean;
begin
if not fMaskIsCompiled then Compile;
if fCaseSensitive then
fMatchString:=aStringToMatch
else
fMatchString:=UTF8LowerCase(aStringToMatch);
fMatchStringLimit:=length(fMatchString);
if (fMatchStringLimit>=fMatchMinimumLiteralBytes)
and (fMatchStringLimit<=fMatchMaximumLiteralBytes) then
Result:=intfMatches(1,0)=TMaskFailCause.mfcSuccess
else
Result:=false; // There are too many or not enough bytes to match the string
end;
function TMaskUTF8.MatchesWindowsMask(const AFileName: String): Boolean;
var
WinMask: TWindowsMaskUTF8;
begin
WinMask:=TWindowsMaskUTF8.Create(fOriginalMask, CaseSensitive, fMaskOpCodesAllowed);
try
Result:=WinMask.Matches(AFileName);
finally
WinMask.Free;
end;
end;
{ TWindowsMask }
procedure TWindowsMaskUTF8.SetMask(AValue: String);
begin
if (AValue = fWindowsMask) then Exit;
inherited SetMask(AValue);
fWindowsMask := AValue;
end;
function TWindowsMaskUTF8.GetMask: String;
begin
Result := fWindowsMask;
end;
procedure TWindowsMaskUTF8.SetWindowsQuirkAllowed(AValue: TWindowsQuirks);
begin
if fWindowsQuirkAllowed = AValue then Exit;
fWindowsQuirkAllowed := AValue;
//if (wqFilenameEnd in fWindowsQuirkAllowed) then
// Include(fMaskOpcodesAllowed, mocAnyCharOrNone);
fMaskIsCompiled := False;
end;
procedure TWindowsMaskUTF8.CompileOtherSpecialChars;
begin
inherited CompileOtherSpecialChars;
if (fMask[fMaskInd]=#0) and not (wqFileNameEnd in self.fWindowsQuirkInUse) then
Exception_InternalError;
CompileAnyCharOrNone(#0, False);
end;
function TWindowsMaskUTF8.IsSpecialChar(AChar: Char): Boolean;
begin
Result := (AChar = #0);
end;
class procedure TWindowsMaskUTF8.SplitFileNameExtension(
const aSourceFileName: String; out aFileName: String;
out aExtension: String; aIsMask: Boolean);
var
j: Integer;
lLowLimit: integer;
begin
// Default values
aFileName:=aSourceFileName;
aExtension:='';
// This is because .foo is considered a file name ".foo" as one.
if aIsMask then
lLowLimit:=0
else
lLowLimit:=1;
j:=Length(aSourceFileName);
while j>lLowLimit do begin
if aSourceFileName[j]='.' then begin
aFileName:=copy(aSourceFileName,1,j-1);
aExtension:=copy(aSourceFileName,j);
break;
end;
dec(j);
end;
end;
constructor TWindowsMaskUTF8.Create(const aMask: String;
aCaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes);
begin
Create(aMask, aCaseSensitive, aOpcodesAllowed, DefaultWindowsQuirks);
end;
constructor TWindowsMaskUTF8.Create(const aMask: String; aCaseSensitive: Boolean;
aOpcodesAllowed: TMaskOpCodes; aWindowsQuirksAllowed: TWindowsQuirks);
begin
fWindowsQuirkAllowed:=aWindowsQuirksAllowed;
fWindowsMask:=aMask;
//if (wqFilenameEnd in fWindowsQuirkAllowed) then
// Include(aOpcodesAllowed, mocAnyCharOrNone);
inherited Create(aMask,aCaseSensitive,aOpcodesAllowed);
end;
procedure TWindowsMaskUTF8.Compile;
function OptionalQMarksAtEnd(aMask: String): String;
var
//lCounter: integer;
i: integer;
begin
//Change ending ? in #0
Result := aMask;
for i := Length(aMask) downto 1 do begin
if Result[i]='?' then
Result[i]:=#0
else
Exit;
end;
{
lCounter:=0;
for i := Length(aMask) downto 1 do begin
if aMask[i]='?' then
inc(lCounter)
else
break;
end;
if lCounter>0 then
aMask:=copy(aMask,1,Length(aMask)-lCounter)+'['+StringOfChar('?',lCounter)+']';
Result:=aMask;
}
end;
var
lFileNameMask: String;
lExtensionMask: String;
lModifiedMask: String;
ZeroPos: SizeInt;
begin
lModifiedMask:=fWindowsMask;
//We cannot have #0 in the mask,since we use it internally
ZeroPos:=Pos(#0, fWindowsMask);
if ZeroPos>0 then
Exception_InvalidCharMask('NullCharacter', ZeroPos);
// Quirk "blah.*" = "blah*"
if wqAnyExtension in fWindowsQuirkAllowed then begin
if (RightStr(lModifiedMask,2)='.*') and (Length(lModifiedMask)>2) then begin
//if RightStr(lModifiedMask,3)='*.*' then begin
lModifiedMask:=copy(lModifiedMask,1,Length(lModifiedMask)-2);
fWindowsQuirkInUse:=fWindowsQuirkInUse+[wqAnyExtension];
end;
end;
SplitFileNameExtension(lModifiedMask,lFileNameMask,lExtensionMask,true);
// Quirk "blah.abc" = "blah.abc*"
if wqExtension3More in fWindowsQuirkAllowed then begin
if (Length(lExtensionMask)=4) and (Length(lFileNameMask)>0) then begin
lExtensionMask:=lExtensionMask+'*';
fWindowsQuirkInUse:=fWindowsQuirkInUse+[wqExtension3More];
end;
end;
// Quirk "" = "*"
if (Length(lFileNameMask)=0) and (Length(lExtensionMask)=0) then begin
if wqEmptyIsAny in fWindowsQuirkAllowed then begin
lFileNameMask:='*';
fWindowsQuirkInUse:=fWindowsQuirkInUse+[wqEmptyIsAny];
end;
end else begin
// Quirk ".abc"
if wqAllByExtension in fWindowsQuirkAllowed then begin
if (Length(lFileNameMask)=0) and (length(lExtensionMask)>0) then begin
if lExtensionMask[1]='.' then begin
lFileNameMask:='*';
fWindowsQuirkInUse:=fWindowsQuirkInUse+[wqAllByExtension];
end;
end;
end;
end;
// Quirk "file???.ab?" matches "file1.ab1" and "file123.ab"
if wqFilenameEnd in fWindowsQuirkAllowed then begin
lFileNameMask:=OptionalQMarksAtEnd(lFileNameMask);
lExtensionMask:=OptionalQMarksAtEnd(lExtensionMask);
if (Pos(#0, lFileNameMask)>0) or (Pos(#0, lExtensionMask)>0) then
fWindowsQuirkInUse:=fWindowsQuirkInUse+[wqFilenameEnd];
end;
if wqNoExtension in fWindowsQuirkAllowed then begin
if Length(lExtensionMask)=1 then begin
fWindowsQuirkInUse:=fWindowsQuirkInUse+[wqNoExtension];
lExtensionMask:='';
end;
end;
inherited Mask:=lFileNameMask+lExtensionMask;
inherited Compile;
end;
function TWindowsMaskUTF8.Matches(const aFileName: String): Boolean;
var
lFileName, lExtension: String;
begin
if not fMaskIsCompiled then
Compile;
if wqNoExtension in fWindowsQuirkInUse then begin
SplitFileNameExtension(aFileName,lFileName,lExtension,false);
// wqNoExtension = Empty extension
//if lExtension<>'' then exit(false);
// Its not clear if a file "file." should match an "*." mask because
// there is no way in Windows that a file ends with a dot.
if (lExtension<>'') and (lExtension<>'.') then
exit(false)
end else if wqAnyExtension in fWindowsQuirkInUse then begin
SplitFileNameExtension(aFileName,lFileName,lExtension,false);
Result:=inherited Matches(lFileName);
exit;
end;
Result:=Inherited Matches(aFileName);
end;
{ TParseStringList }
constructor TParseStringList.Create(const AText, ASeparators: String);
var
I, S: Integer;
begin
inherited Create;
S := 1;
for I := 1 to Length(AText) do
begin
if Pos(AText[I], ASeparators) > 0 then
begin
if I > S then
Add(Copy(AText, S, I - S));
S := I + 1;
end;
end;
if Length(AText) >= S then
Add(Copy(AText, S, Length(AText) - S + 1));
end;
{ TMaskList }
constructor TMaskList.Create(const aValue: String; aSeparator: Char;
CaseSensitive: Boolean; aOpcodesAllowed: TMaskOpCodes);
begin
fMask := aValue;
fSeparator := aSeparator;
fCaseSensitive := CaseSensitive;
fMaskOpcodes := aOpcodesAllowed;
fAutoReverseRange := True;
fMasks := TObjectList.Create(True);
FMaskClass := GetMaskClass;
AddMasksToList(aValue, aSeparator, CaseSensitive, aOpcodesAllowed);
end;
constructor TMaskList.Create(const aValue: String; aSeparator: Char; aOptions: TMaskOptions);
var
CaseSens: Boolean;
Opcodes: TMaskOpCodes;
begin
CaseSens:=moCaseSensitive in aOptions;
if moDisableSets in aOptions then
Opcodes:=MaskOpCodesDisableRange
else
Opcodes:=DefaultMaskOpCodes;
Create(aValue, aSeparator, CaseSens, Opcodes);
end;
destructor TMaskList.Destroy;
begin
fMasks.Free;
inherited Destroy;
end;
function TMaskList.GetItem(Index: Integer): TMask;
begin
Result := TMask(fMasks.Items[Index]);
end;
procedure TMaskList.SetAutoReverseRange(AValue: Boolean);
var
i: Integer;
begin
if fAutoReverseRange = AValue then Exit;
fAutoReverseRange := AValue;
for i := 0 to fMasks.Count - 1 do
TMask(fMasks.Items[i]).AutoReverseRange := fAutoReverseRange;
end;
procedure TMaskList.SetCaseSensitive(AValue: Boolean);
var
i: Integer;
begin
if fCaseSensitive = AValue then Exit;
fCaseSensitive := AValue;
for i := 0 to fMasks.Count - 1 do
TMask(fMasks.Items[i]).CaseSensitive := fCaseSensitive;
end;
procedure TMaskList.SetMask(AValue: String);
begin
if fMask = AValue then Exit;
fMask := AValue;
fMasks.Clear;
AddMasksToList(fMask, fSeparator, fCaseSensitive, fMaskOpCodes);
end;
procedure TMaskList.SetMaskOpCodes(AValue: TMaskOpCodes);
var
i: Integer;
begin
if FMaskOpCodes = AValue then Exit;
fMaskOpCodes := AValue;
for i := 0 to fMasks.Count - 1 do
TMask(fMasks.Items[i]).MaskOpCodes := fMaskOpcodes;
end;
function TMaskList.GetMaskClass: TMaskClass;
begin
Result := TMask;
end;
procedure TMaskList.AddMasksToList(const aValue: String; aSeparator: Char; CaseSensitive: Boolean;
aOpcodesAllowed: TMaskOpCodes);
var
S: TParseStringList;
i: Integer;
begin
S := TParseStringList.Create(aValue, aSeparator);
try
for i := 0 to S.Count-1 do begin
fMasks.Add(FMaskClass.Create(S[i], CaseSensitive, aOpcodesAllowed));
end;
finally
S.Free;
end;
end;
function TMaskList.GetCount: Integer;
begin
Result := fMasks.Count;
end;
function TMaskList.Matches(const AFileName: String): Boolean;
var
I: Integer;
begin
Result := False;
for I := 0 to fMasks.Count-1 do
if ((fMasks.Items[I]) as FMaskClass).Matches(AFileName) then
Exit(True);
end;
function TMaskList.MatchesWindowsMask(const AFileName: String): Boolean;
//use the same hack as in TMask.MatchesWindowsMask
var
WML: TWindowsMaskList;
begin
WML := TWindowsMaskList.Create(fMask, fSeparator, fCaseSensitive, fMaskOpcodes, DefaultWindowsQuirks);
try
Result := WML.Matches(AFileName);
finally
WML.Free;
end;
end;
end.
|