Sign in Join
Wiki copytemporary

Masks: what changed

Revision 1 to 2. 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-<table>
1716<thead>
18-<tr>
19-<th>Name</th>
20-<th>Interpretation</th>
21-</tr>
17+
2218</thead>
2319<tbody>
24-<tr>
25-<td>mocAnyChar</td>
26-<td>Treat ? as a wildcard to match exactly one char</td>
27-</tr>
28-<tr>
29-<td>mocAnyCharOrNone</td>
30-<td>Treat [?] to match any char or the absence of a char</td>
31-</tr>
32-<tr>
33-<td>mocAnyText</td>
34-<td>Treat * as a wildcard to mach zero or any mumber of chars</td>
35-</tr>
36-<tr>
37-<td>mocRange</td>
38-<td>Treat [a-z] to match any character in the range from 'a' up to and including 'z'.<br />
20+
3921A '-' is treated as a range indicator and it's presence is required (unless mocSet is also enabled).<br />
4022To have a literal '-' in a range definition, you must escape it with EscapeChar.</td>
41-</tr>
42-<tr>
43-<td>mocSet</td>
44-<td>Treat [a-z] to match either 'a', '-' or 'z'<br />
23+
4524Notice that when mocRange is also enabled, an unescaped '-' will always be treated as a range indicator.</td>
46-</tr>
47-<tr>
48-<td>mocNegateGroup</td>
49-<td>Treat [!a-c] to not match 'a', 'b', or 'c', but match any other char. Requires mocRange and/or mocSet</td>
50-</tr>
51-<tr>
52-<td>mocEscapeChar</td>
53-<td>Treat EscapeChar (defaults to '\') to take the next char as a literal, so '*' is treated as a literal '*'.</td>
54-</tr>
25+
5526</tbody>
56-</table>
5727
5828#### Predefined MaskOpcodes
5929
6030The masks unit has some predefined MaskOpcodes constants.\
6131
6232| Name | Interpretation |
6333|----|----|
6434| AllMaskOpCodes | Self explanatory. |
6535| MaskOpCodesDisableRange | Do not interpret \[ as the start of a range or set. |
6636| MaskOpCodesNoEscape | Interpret \[?\] as a set with a literal question mark instead of 0..1 chars wildcard. Disable escaping. |
6737| DefaultMaskOpCodes | Equals to MaskOpCodesNoEscape. |
6838| | |
6939
7040If no TMaskOpcodes parameter is supplied to one of the Matches methods or functions, DefaultMaskOpCodes is assumed.
7141
7242### Specific Windows Quirks
7343
7444Windows 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.
7545
7646TWindowsQuirks is defined as set of TWindowsQuirks.\
7747The TWindowsQuirk enumeration type consists of:
7848
7949| Name | Example mask | Interpretation |
8050|----|----|----|
8151| wqAnyExtension | Anything*.* | The filename is not required to have an extension |
8252| wqFilenameEnd | Anything??.abc | The '?' matches 1 or 0 chars (except '.') |
8353| wqExtension3More | Anything.abc | Matches Anything.abc but also Anything.abc* (so '*.pas' also matches with 'file.pas.bak') |
8454| wqEmptyIsAny | | Empty string matches anything, so acts like '*' |
8555| wqAllByExtension | .abc | Is treated as *.abc |
8656| wqNoExtension | Anything*. | Matches Anything* without extension |
8757
8858TWindowsQuirks can only be used in the Windows specfic classses and functions.
8959
9060#### Predefined TWindowsQuirks
9161
9262The masks unit has some predefined WindowsQuirks constants.\
9363
9464| Name | Interpretation |
9565|----|----|
9666| AllWindowsQuirks | Self explanatory. |
9767| DefaultWindowsQuirks | Equals to \[wqAnyExtension,wqEmptyIsAny,wqNoExtension\] |
9868| | |
9969
10070If no TWindowsQuirks parameter is supplied to one of the Windows specific Matches methods or functions, DefaultWindowsQuirks is assumed.
10171
10272### TMask classes
10373
10474All pattern matching is done by the TMask classes.\
10575The convenience functions like MatchesMask() internally use an instance of a TMask class.\
10676Not 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.\
10777This also makes sense if you do multiple calls to a matching routine, this avoids repetetive creating of a TMask class instance.
10878
10979#### TMask
11080
11181Does pattern matching unix style (or should one say "non-Windows" style).\
11282The mask '*.*' requires that there must be at least one period ('.') in the filename to match.\
11383The following properties of TMask (and all other TMask classes) are not exposed in the aforementioned convenience functions:
11484
115-<table>
11685<thead>
117-<tr>
118-<th>Name</th>
119-<th>Type</th>
120-<th>Default value</th>
121-<th>Meaning</th>
122-</tr>
86+
12387</thead>
12488<tbody>
125-<tr>
126-<td>AutoReverseRange</td>
127-<td>Boolean</td>
128-<td>True</td>
129-<td>When True, it interprets the range [z-a] as [a-z], otherwise as an empty range.<br />
89+
13090Requires mocRange enabled.</td>
131-</tr>
132-<tr>
133-<td>EscapeChar</td>
134-<td>Char</td>
135-<td>'\'</td>
136-<td>The char used to escape characters that would otherwise have special meaning. E.g. '*' will be interpreted as a literal '*'.<br />
91+
13792The value of EscapeChar must be in the range #0..#127 (so 7-bit ASCII), otherwise an exception is raised.<br />
13893The use of EscapeChar requires mocEscapeChar to be enabled.</td>
139-</tr>
140-<tr>
141-<td></td>
142-<td></td>
143-<td></td>
144-<td></td>
145-</tr>
94+
14695</tbody>
147-</table>
14896
14997To 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.
15098
15199#### TWindowsMask
152100
153101Does pattern matching in Windows style.\
154102So (by default) the mask '*.*' wil match any string of any (including zero) length, with or withoud a period it.\
155103Notice that \#0 is not allowed as a character in the Mask in TWindowsMask (and therefore \#0 can also not be used as EscapeChar).\
156104If you set it's property Quirks to \[\] (empty set), it will behave just like TMask.
157105
158106#### TMaskList and TWindowsMaskList
159107
160108These classes support pattarn matching against a list of patterns separated by a Separator (which defaults to ';').\
161109Typically it will be used to match e.g. a list of filename extensions: '*.pas;*.pp;*.inc'.\
162-\
110+
163111Like TMask, the TMaskList class provides the method MatchesWindowsMask() for backwards compatibility. It's fate will be the same...
164112
165113## Code examples
166114
167115ToDo
168116
169117---
170118*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