Sign in Join
Wiki copytemporary

Masks: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11This page describes the Masks unit as per Lazarus version 2.3
22
33UNDER CONSTRUCTION
44
55## Overview
66
77The masks unit provides classes and functions for pattern matching using wildcards, sets/ranges and/or literal characters.\
88
99## Controlling how the mask is interpreted
1010
1111### TMaskOpcodes
1212
1313TMaskOpcodes is defined as set of TMaskOpcode.\
1414The TMaskOpcode enumeration type consist of:
1515
16-<thead>
17-
18-</thead>
19-<tbody>
20-
21-A '-' is treated as a range indicator and it's presence is required (unless mocSet is also enabled).<br />
22-To have a literal '-' in a range definition, you must escape it with EscapeChar.</td>
23-
24-Notice that when mocRange is also enabled, an unescaped '-' will always be treated as a range indicator.</td>
25-
26-</tbody>
16+| Name | Interpretation |
17+|---|---|
18+| mocAnyChar | Treat ? as a wildcard to match exactly one char |
19+| mocAnyCharOrNone | Treat [?] to match any char or the absence of a char |
20+| mocAnyText | Treat * as a wildcard to mach zero or any mumber of chars |
21+| mocRange | Treat [a-z] to match any character in the range from 'a' up to and including 'z'. · A '-' is treated as a range indicator and it's presence is required (unless mocSet is also enabled). · To have a literal '-' in a range definition, you must escape it with EscapeChar. |
22+| mocSet | Treat [a-z] to match either 'a', '-' or 'z' · Notice that when mocRange is also enabled, an unescaped '-' will always be treated as a range indicator. |
23+| mocNegateGroup | Treat [!a-c] to not match 'a', 'b', or 'c', but match any other char. Requires mocRange and/or mocSet |
24+| mocEscapeChar | Treat EscapeChar (defaults to '\') to take the next char as a literal, so '*' is treated as a literal '*'. |
2725
2826#### Predefined MaskOpcodes
2927
3028The masks unit has some predefined MaskOpcodes constants.\
3129
3230| Name | Interpretation |
3331|----|----|
3432| AllMaskOpCodes | Self explanatory. |
3533| MaskOpCodesDisableRange | Do not interpret \[ as the start of a range or set. |
3634| MaskOpCodesNoEscape | Interpret \[?\] as a set with a literal question mark instead of 0..1 chars wildcard. Disable escaping. |
3735| DefaultMaskOpCodes | Equals to MaskOpCodesNoEscape. |
3836| | |
3937
4038If no TMaskOpcodes parameter is supplied to one of the Matches methods or functions, DefaultMaskOpCodes is assumed.
4139
4240### Specific Windows Quirks
4341
4442Windows mask works in a different mode than regular masks, it has many quirks and corner cases inherited from CP/M, then adapted to DOS (8.3) file names and adapted again for long file names.
4543
4644TWindowsQuirks is defined as set of TWindowsQuirks.\
4745The TWindowsQuirk enumeration type consists of:
4846
4947| Name | Example mask | Interpretation |
5048|----|----|----|
5149| wqAnyExtension | Anything*.* | The filename is not required to have an extension |
5250| wqFilenameEnd | Anything??.abc | The '?' matches 1 or 0 chars (except '.') |
5351| wqExtension3More | Anything.abc | Matches Anything.abc but also Anything.abc* (so '*.pas' also matches with 'file.pas.bak') |
5452| wqEmptyIsAny | | Empty string matches anything, so acts like '*' |
5553| wqAllByExtension | .abc | Is treated as *.abc |
5654| wqNoExtension | Anything*. | Matches Anything* without extension |
5755
5856TWindowsQuirks can only be used in the Windows specfic classses and functions.
5957
6058#### Predefined TWindowsQuirks
6159
6260The masks unit has some predefined WindowsQuirks constants.\
6361
6462| Name | Interpretation |
6563|----|----|
6664| AllWindowsQuirks | Self explanatory. |
6765| DefaultWindowsQuirks | Equals to \[wqAnyExtension,wqEmptyIsAny,wqNoExtension\] |
6866| | |
6967
7068If no TWindowsQuirks parameter is supplied to one of the Windows specific Matches methods or functions, DefaultWindowsQuirks is assumed.
7169
7270### TMask classes
7371
7472All pattern matching is done by the TMask classes.\
7573The convenience functions like MatchesMask() internally use an instance of a TMask class.\
7674Not all of the properties of the TMask classes are exposed in these convenience functions, so when you need more control over how the mask is interpreted, you should use a TMask class directly.\
7775This also makes sense if you do multiple calls to a matching routine, this avoids repetetive creating of a TMask class instance.
7876
7977#### TMask
8078
8179Does pattern matching unix style (or should one say "non-Windows" style).\
8280The mask '*.*' requires that there must be at least one period ('.') in the filename to match.\
8381The following properties of TMask (and all other TMask classes) are not exposed in the aforementioned convenience functions:
8482
85-<thead>
86-
87-</thead>
88-<tbody>
89-
90-Requires mocRange enabled.</td>
91-
92-The value of EscapeChar must be in the range #0..#127 (so 7-bit ASCII), otherwise an exception is raised.<br />
93-The use of EscapeChar requires mocEscapeChar to be enabled.</td>
94-
95-</tbody>
83+| Name | Type | Default value | Meaning |
84+|---|---|---|---|
85+| AutoReverseRange | Boolean | True | When True, it interprets the range [z-a] as [a-z], otherwise as an empty range. · Requires mocRange enabled. |
86+| EscapeChar | Char | '\' | The char used to escape characters that would otherwise have special meaning. E.g. '*' will be interpreted as a literal '*'. · The value of EscapeChar must be in the range #0..#127 (so 7-bit ASCII), otherwise an exception is raised. · The use of EscapeChar requires mocEscapeChar to be enabled. |
9687
9788To maintain backwards compatibility the TMask class provides the method MatchesWindowsMask(), but this hase been deprecated in favour of the direct use of the Matches method of TWindowsMask. This method will be removed in a future release.
9889
9990#### TWindowsMask
10091
10192Does pattern matching in Windows style.\
10293So (by default) the mask '*.*' wil match any string of any (including zero) length, with or withoud a period it.\
10394Notice that \#0 is not allowed as a character in the Mask in TWindowsMask (and therefore \#0 can also not be used as EscapeChar).\
10495If you set it's property Quirks to \[\] (empty set), it will behave just like TMask.
10596
10697#### TMaskList and TWindowsMaskList
10798
10899These classes support pattarn matching against a list of patterns separated by a Separator (which defaults to ';').\
109100Typically it will be used to match e.g. a list of filename extensions: '*.pas;*.pp;*.inc'.\
110101
111102Like TMask, the TMaskList class provides the method MatchesWindowsMask() for backwards compatibility. It's fate will be the same...
112103
113104## Code examples
114105
115106ToDo
116107
117108---
118109*From the Free Pascal wiki, page "Masks", as it stood on 2022-01-02 after a change by Bart, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history