Contains classes for matching file names to file masks.

masks.pas contains classes, types, and routines used implement file and directory masks. Masks are patterns used to match file names found on the local file system. Support is provided for both UNIX- and Windows-style masks. The unit includes utility functions to compare one or more file names to a given mask, including: MatchesMask, MatchesWindowsMask, MatchesMaskList, and MatchesWindowsMaskList.

It is fast and optimized, and fully supports Unicode. Also supports DOS/Windows compatible masks which behave differently from standard masks.

masks.pas is part of the LazUtils package.

Author: José Mejuto

Changes and improvements by: Juha Manninen and Bart Broersma.

Exception raised for an invalid character in TMask.

EMaskError is a EConvertError descendant representing the exception raised when an invalid character is found in a mask expression, or when an invalid value is assigned to properties in a mask class instances. EMaskError is the exception type raised in TMaskBase methods like:

  • Exception_InvalidCharMask
  • Exception_MissingCloseChar
  • Exception_IncompleteMask
  • Exception_InvalidEscapeChar
  • Exception_InternalError
EConvertError
Enumeration with values identifiers representing mask exception codes. Represents an exception raised when an unexpected op code is encountered while evaluating a mask expression. Represents an exception raised when an unexpected mask character is found when compiling the op codes for a mask expression. Represents an exception raised when the closing ']' is not found in a set or range expression. Represents an exception raised when a mask does not contain enough characters to evaluate the mask expression. Represents an exception raised when an invalid value is assigned as the EscapeChar for a mask instance. Not used in the current implementation. Member with the mask exception code for an exception instance. Constructor for the class instance.

Calls the CreateFmt constructor using the values in msg and aCode as arguments.

Message for the exception. Error code for the exception. Constructor for the class instance.

CreateFmt is an overridden constructor used to create an exception instance with the values specified in the msg and aCode arguments. It stores the value in ACode to the cCode member, and calls the inherited method.

msg is a String which contains a formatting specification using the notation allowed for the RTL Format routine. Values from the args parameter are substituted for the placeholders found in the msg argument.

Message for the exception with embedded arguments for Format(). Array of constant values for the formatted exception message. Error code for the exception. Provides access to the mask exception code for the exception instance.

Code is a read-only TMaskExceptionCode property with the code for a mask exception class instance. The value in Code is passed as argument to the Create or CreateFmt constructors.

Represents operations needed for an entry in a mask specification. Treats '?' as a wildcard to match exactly one char. Allows '[?]' to match any character or no character. Treats '*' as a wildcard to match zero or any number of characters. Causes '[a-c]'' to match either 'a', 'b' or 'c'. '-' is always treated as the range separator. To use a literal '-' in a range, it must be escaped using the EscapeChar (default is '\') for a mask instance. '[+-\-]' matches '+', ',' or '-'. Causes '[a-c]' to match either 'a', '-' or 'c'. Enables sets notation in a mask expression. Causes '[!a-c]' to not match the group values, but matches any other character. Requires mocRange and/or mocSet. Causes an EscapeChar (defaults to '\') to use the next character as a literal instead of a wildcard; so '\*' is treated as a literal '*' character. Set type used to store values from the TMaskOpcode enumeration.

TMaskOpcodes is the type passed as argument to the constructor for TMaskBase and TWindowsMaskUTF8 classes, and used to implement their MaskOpCodes property.

Set with all of the mask op codes values. Contains the set of op codes allowed when sets and ranges are disabled in mask expressions.

Omits the op code identifiers mocAnyCharOrNone, mocRange, and mocSet.

Contains op codes enabled when escape characters and single character wildcards are not used.

Interprets '[?]' as a literal question mark instead of 0..1 character wildcard. Disables escape characters like '\?'. Omits the op code values mocAnyCharOrNone and mocEscapeChar.

Contains the default op code values used for mask class instances.

DefaultMaskOpCodes includes the TMaskOpCode values found in AllMaskOpCodes with the exception of mocEscapeChar. This disables use of the escape character ('\' is the default) in a mask expression. DefaultMaskOpCodes is used as the default value for an argument passed to the TMaskBase.Create and TMaskList.Create methods.

Represents types of Windows-specific quirks for file and directory masks.

TWindowsQuirk is an enumerated type used to represent ways in which a Windows/DOS mask works differently than a regular mask. Windows/DOS masks have many quirks and corner cases inherited from CP/M, then adapted to DOS (8.3) file names, and adapted again for long file names.

Treats "filename*.*" as if the ".*"" notation is omitted. This causes the mask to behave in a UNIX-like manner, where 'foo.*' matches 'foo'. For "filename??.ext", "?" matches 1 or 0 chars (except for '.'). Not the same as "filename*.ext", but the same as regex "filename.{0,2}\.ext". Internally converted to "filename[??].ext". Anything.abc matches "Anything.abc" but also "Anything.abc*" (3 char extension). Anything.ab matches "Anything.ab" and never "anything.abcd". An empty string ("") matches anything "*" . ".abc" is treated as "*.abc". "Anything*." matches "Anything*" without a file extension. Set type used to store values from the TWindowsQuirk enumeration.

A TWindowsQuirks type is passed as an argument to the Create method in TWindowsMaskUTF8, and is the type used for its Quirks property.

Sets type with all of the Windows-specific quirk values.

AllWindowsQuirks can be passed an argument to the constructor in TWindowsMaskUTF8, or assigned directly to the Quirks property in the mask class instance.

Set with the default Windows-specific quirk values.

DefaultWindowsQuirks can be passed an argument to the constructor in TWindowsMaskUTF8, or assigned directly to the Quirks property in the mask class instance. Omits the values wqExtension3More and wqAllByExtension.

Contains options which can be enabled or disabled in TMask comparisons.

TMaskOption is an enumerated type with values for options that can be enabled in TMask class instances. Values from the enumeration are stored in the TMaskOptions set type, and passed as an argument to an alternate constructor in TMaskBase and descendent classes. Values in the set also control the values assigned to the CaseSensitive and MaskOpCodes properties in the mask class instance.

TMaskOption and TMaskOptions are provided for backward compatibility with previous versions of LazUtils.

Masks Overview
Enables case sensitive comparison. Disables set processing; '[' and ']' are treated as literal characters. Set type used to store values from the TMaskOption enumeration. Masks Overview Implements the base class used to define and evaluate a mask expression.

TMaskBase provides the framework or API used to examine and handle a mask expression. It provides member fields used to store the compiled op codes for the mask expression as well other state information. Methods are provides to raise exceptions for specific mask error conditions. Properties are also implemented to represent the case sensitivity, escape character, range handling, and op codes enabled for a mask expression.

Please note that TMaskBase does not capture, compile or store the mask expression. Those actions are performed in a descendent class.

Represents op codes found when parsing / compiling a mask expression.

Used in the Add and IncrementLastCounterBy methods.

Op code for a literal value in a compiled mask expression. Op code for a range declared in a compiled mask expression. Op code to exclude a range or set declared in a compiled mask expression. Op code to match any single character in a compiled mask expression. The character is required. Op code to match any single character in a compiled mask expression. The character is optional. Op code to match any number of characters in a compiled mask expression. On failure, the match is restarted at the next position and continues until the mask is completed or all matching values are used. Op code to match a single optional character value in the compiled mask expression. Op code for the '[' character which starts an optional range or set in a compiled mask expression. Op code for the ']' character which ends an optional range or set in a compiled mask expression. Represents success or failure conditions when handling mask characters.

TMaskFailCause is the type returned from the IntfMatches method in TMaskUTF8 and descendent classes.

The mask was examined and a match was found. Characters in a compared value were exhausted before a match was found. Values in a mask expression were exhausted before a match was found. The compared value is not a match for a given mask expression. Default error condition when an invalid op code is found for a mask expression. Number bytes used to grow the storage for op codes in a compiled mask expression. Adds one or more mask op codes found in the compiled mask expression.

Add is an overloaded method used to add one or more op codes for compiled mask expression. The overloaded variants allow the op code to be specified as a TMaskParsedCode enumeration value, as a pointer to byte values for the parsed op code(s), or as an Integer.

Add is called is from the Compile method (in descendent classes) when UTF-8 codepoints in the mask expression are translated into their corresponding op codes.

Number of bytes needed to represent the op codes in AData. Pointer to the bytes representing the op codes. Integer value for the TMaskOpCode enumeration value added in the method. Moves the internal counter for a specified op code when processing a range expression.

Calls Exception_InternalError when the op code at the current position does not match the value in AOpCode. IncrementLastCounterBy is called from the private CompileRange method.

Op code affected in the method. Number of characters to advance the pointer offset in the method. Member with the case sensitivity settings for a mask expression. Member with the value for the AutoReverseRange property. Member which indicates if the mask expression has been compiled. Member which stores the op codes needed to evaluate the mask expression and compare specified values. Member with the current offset an op code for the compiled mask expression. Member with the allocated storage for the compiled mask expression. Member with the ordinal position represented in cMaskCompiledIndex. Member with the last position in the mask expression where an op code can be applied. Member with the last position in a compared value that can used to match the mask expression. Member with the minimum number of bytes for a literal character (normally 1). Member with the maximum number of bytes for a literal character (normally 4 for UTF-8 codepoints). Member with the op code identifiers allowed for the mask expression. Member with the escape character for the mask expression. An escape character forces the next mask character to be treated as a literal and not a wildcard. Resets values in internal members used in the implementation.

Used to implement the Compile method in descendent classes.

Raises an exception when the specified mask character or offset is invalid.

Exception_InvalidCharMask is a class procedure used to raise an exception when a character in the mask expression is invalid. Exception_InvalidCharMask is one of the methods called during execution of the Compile method (in descendent classes) when an invalid Mask expression is detected for the class instance.

In TMaskBase, an EMaskError exception is always raised. The base class does not implement the required mask expression, nor does it provide a complete implementation of the Compile method.

Mask character which caused the exception. Offset where the invalid mask character was found. Raises an EMaskError exception when the closing ']' character is omitted from a set or range expression.

Exception_MissingCloseChar is a class procedure used to raise an EMaskError exception when the closing ']' character is omitted from a set or range expression. The exception message is determined by the value in the aOffset argument. When aOffset is a positive value (or zero), the value in rsMissingCloseCharMaskAt is used as the exception message. Otherwise, the value in rsMissingCloseCharMask is used.

Exception_MissingCloseChar is called from the private CompileRange method in TMaskUTF8 when the range or set expression is not closed.

Mask character found instead of the expected value. Offset in the mask expression where the error was found. Raises an EMaskError exception when escaped characters in a set is enabled, but range processing has not been enabled.

The exception is raised with the message in the rsIncompleteMask resource string.

Raises an EMaskError exception when an invalid value is assigned to the EscapeChar property.

Exception_InvalidEscapeChar is a class procedure used to raise an EMaskError exception when an invalid value is assigned to the EscapeChar property. This occurs when the new property value is not in the range #0..#127. The exception is raised with the message in the rsInvalidEscapeChar resource string.

Raises an EMaskError exception when a parsed op code does not match an expected value in the mask expression.

The method is called from the IncrementLastCounterBy method when a range expression is handled in Compile.

Constructor for the class instance.

Create is the overloaded constructor for the class instance. One variant allows case sensitivity for mask expressions and the allowed op codes to be specified as arguments. The other variant accepts an argument with the set of mask options for the class instance.

Create the sets the initial values for the CaseSensitive, AutoReverseRange, and EscapeChar properties.

TMaskBase does not provide a way to specify or store the mask expression for the class instance. Use one of the descendent classes which provide UTF-8 support and implement specific file system behavior for the mask expression.
True if the mask expression is case sensitive. Op codes allowed for the compiled mask expression. Set with the TMaskOption values enabled in the class instance. Indicates if the mask expression is case sensitive.

CaseSensitive is a Boolean property which indicates if character in a mask expression are handle with case sensitivity. Its value is determined by an explicit argument passed to the Create method, or the presence of moCaseSensitive in the TMaskOptions passed to the constructor.

Changing the value for the property causes Compile to be called when the Matches method in descendent classes is executed. When set to False, both the mask expression and the compared values are converted to lowercase for the comparison performed in Matches.

Indicates if a range expression is automatically reversed when the starting value is larger than the ending value.

Setting AutoReverseRange to True would cause a range expression like '[e-a]' to be treated as if it were specified as '[a-e]'. The default value for the property is True, as assigned in the Create constructor.

Changing the value for the property causes Compile to be called during execution of the Matches method. The property value is used in the implementation of the private CompileRange method in descendent classes.

Contains the character used to escape the following character in a mask expression.

EscapeChar causes the character that immediately follows the value to be treated as a literal character instead of a wildcard. It is used to allow wildcard characters (like '?' or '*') or set / range characters (like '[', '-', or ']') to be used as literals in the Mask expression. The default value for the property is '\' (Backslash) as assigned in the Create constructor.

EscapeChar must be one of the 7-bit ASCII characters in the range #0..#127. Setting the property to a value larger than #127 (ASCII Delete) causes an EMaskError exception to be raised with the message in the rsInvalidEscapeChar resource string.

Changing the value for the property causes Compile to be called during execution of the Matches method.

Descendent classes (which include the Mask expression) use the value in their Compile and CompileRange methods. Make sure the class instance has the value mocRange in the MaskOpCodes property to enable range notation in the Mask expression.

rsInvalidEscapeChar
Contains the set of mask op codes enabled for the class instance.

MaskOpCodes is a TMaskOpCodes property with the set of op codes enabled for the Mask expression in the class instance. MaskOpCodes is populated with enumeration values from TMaskOpCode when characters in the mask expression are examined in the Compile method. The allowed values in the set can be specified as an argument to the Create constructor.

Values in MaskOpCodes are used when the Matches method is called to compare a string value to the Mask expression.

Setting a new value for the property causes Compile to be called during execution of the Matches method in descendent classes.

Implements a class used to define, evaluate, and compare a mask expression using UNIX file system mask conventions.

TMaskUTF8 is a TMaskBase descendant which implements a class used for mask expressions using UNIX file system conventions. It extends the ancestor class with support for a UTF-8-encoded Mask expression. The mask expression uses wildcards and other notation specific to UNIX-like file systems, including:

'?'
Represents any single required character in a file or directory name. For instance, 'c?t' matches 'cat' and 'cut', but not 'coat' or 'cult'. An expression like 'cl??' would matches values like 'clap' and 'clot', but not 'clean' or 'clt'.
'*'
Matches all entries in a directory. This includes entries with or without a file extension, and does not differentiate between files and directories. This is commonly referred to as the "all files" mask.
'*.*'
Matches all entries which have any given file extension. The file extension is required though. Please note that this notation is different than the one implemented for the Windows file system.
'*.ext'
Matches all entries which have a '.ext' file extension.
'file*
Matches all entries that start with 'file' and may contain any number of additional optional characters or a file extension. Matches 'file', 'filename' and 'filename.ext'.
'[]' (square brackets)
Represents a choice of characters that may represent a match on the file system. They are used in both set and range expressions. A set is expressed using notation like 'c[au]t'. This would match entries like 'cat' or 'cut', but not 'cot'. A range uses notation like 'g[a-f]z'. This would match values like 'gaz', 'gbz', and 'gcz'. It would not match 'ggz', 'gxz', or 'gz'.
'[!]'
Used to negate a set or range expression. It matches any character value which is NOT in the set or range.
'\' (Backslash)
The default escape character which forces the following character to be interpreted as a literal and not a wildcard. The escape character is configurable in the class instance using the EscapeChar property.

TMaskUTF8 provides an overridden Compile method which examines and converts the Mask expression into a list of op codes needed in Matches and MatchesWindowsMask.

Masks Overview
Member used for the mask expression passed to the constructor. Indicates if the specified character is a wildcard character, or used to represent set / range notation. Always returns False in TMaskUTF8. Character value examined in the method. Has an empty implementation in TMaskUTF8. Indicates the difference between UTF-8 codepoints in the specified values.

CompareUTF8Sequences is an Integer class function used to compare the UTF-8 codepoints in the values specified by P1 and P2. The return value is the numeric difference between the byte values in the differing UTF-8 codepoints. The return value is 0 (zero) when P1 and P2 have the same UTF-8-encoded content.

CompareUTF8Sequences is used in the implementation of the CompileRange and IntfMatches methods.

numeric difference between differing codepoints, or zero when the same. PChar type with UTF-8 codepoints compared in the method. PChar type with UTF-8 codepoints compared in the method. Implements the Matches method for the supported platform.

IntfMatches is a TMaskFailCause function which implements the Matches method in the class instance.

IntfMatches uses the compiled op codes for the Mask expression to determine whether a value in the compared string are suitable for op codes. The return value indicates the success or reason for failure in the method.

IntfMatches is called from the Matches method.

Success or failure reason for the comparison. Offset into the match string compared in the method. Ordinal position for the op code used in the method. Constructor for the class instance.

Create is the overloaded constructor for the class instance. It includes an AMask argument which contains the expression stored in the Mask property. Variants of the method also allow the value in CaseSensitive to be set. Set arguments can be provided which contain the TMaskOpCode values or the TMaskOption values used in the class instance.

The variant which includes a TMaskOptions parameter has been deprecated in version 2.3, and will be removed in version 2.5. Use the variant which includes a TMaskOpCodes parameter.
Mask expression for the class instance. True if comparisons are performed with case sensitivity. Set of op codes allowed for the compiled mask expression. Set of TMaskOption values enabled for the class instance. Examines the mask expression and creates a list of op codes.

Compile is a virtual method used to examine and convert values from the Mask property into TMaskParsedCode values for the positions in the mask expression.

CaseSensitive indicates if case is significant for the value in Mask. Mask is converted to lowercase prior to processing in the method when CaseSensitive is set to False.

Compile examines each of the UTF-8 codepoints in Mask, and adds values to an internal list representing the character ranges, literal values, or other TMaskParsedCode values needed to evaluate the Mask expression. Compile may raise an exception when an invalid or incomplete mask is encountered. If the Compile method is called again using the same value in Mask, it simply returns False rather than re-raise the exception.

Compile is called from the Matches method when the Mask expression has not already been compiled.

Indicates whether the specified value is a match for the Mask expression.

Matches calls Compile if the MaskOpCodes have not been compiled for the Mask expression, or when one the configuration parameters has been changed for the class instance. When CaseSensitive is set to False, the value in aStringToMatch is converted to a lowercase UTF-8 codepoint for the comparison.

Matches calls the IntfMatches method to implement the comparison between the specified value using the MaskOpCodes for the class instance. The return value is True if the call to IntfMatches is successful. It is False if either the Mask expression or the compared value is too long or too short.

True when the specified value matches the Mask expression. Value to compare to the Mask expression. Indicates whether the specified value is a match for the Windows-specific Mask expression. Deprecated in Lazarus version 2.3, and will be removed in 2.5. Create a TWindowsMask instance and call its Matches method instead. True if the specified file name matches the Windows-specific Mask expression. Value compared to the Mask expression. Mask expression used to match files or directories in the class instance.

Mask is a String property which contains the mask expression used to compare / evaluate a value in the Matches method. The property value is initially set using the arguments passed to the Create constructor. Values in Mask use the UNIX file system conventions for wildcards and related notations.

Character values in Mask are translated into op codes in the Compile method. These op codes are used in the Matches method to evaluate and compare a given value to the compiled mask expression.

Changing the value for the property causes Compile to be called when the Matches method is executed.

The TMask class represents a mask expression and performs comparisons.

TMask is an alias for the TMaskUTF8 class. Provided for compatibility with previous LazUtils versions.

Use TWindowsMaskUTF8 or TWindowsMask for mask expressions using DOS/Windows-specific notation and file system conventions.

Masks Overview
Implements a class used to define and evaluate a mask expression using Windows file system mask conventions.

TWindowsMaskUTF8 is a TMask descendant which implements a class used for mask expressions using Windows file system conventions. It extends the ancestor class with support for a UTF-8-encoded Mask expression. The mask expression uses wildcards and other notation specific to Windows/DOS file systems, including:

'?'
Represents any single required character in a file or directory name. For instance, 'c?t' matches 'cat' and 'cut', but not 'coat' or 'cult'. An expression like 'cl??' would matches values like 'clap' and 'clot', but not 'clean' or 'clt'.
'*'
Matches any entries found in a directory. This includes files that have a file extension. Behaves the same as '*.*'.
'*.*'
Matches all entries in a directory. The file extension is optional. Please note that this notation is different than the behavior in TMaskUTF8 / TMask.
'*.ext'
Matches all entries which have a '.ext' file extension.
'file*
Matches all entries that start with 'file' and may contain any number of additional optional characters or an optional file extension. Matches 'file', 'filename' and 'filename.ext'.
'[]' (square brackets)
Represents a choice of characters that may represent a match on the file system. They are used in both set and range expressions. A set is expressed using notation like 'c[au]t'. This would match entries like 'cat' or 'cut', but not 'cot'. A range uses notation like 'g[a-f]z'. This would match values like 'gaz', 'gbz', and 'gcz'. It would not match 'ggz', 'gxz', or 'gz'.
'[!]'
Used to negate a set or range expression. It matches any character value which is NOT in the set or range.
'\' (Backslash)
The default escape character which forces the following character to be interpreted as a literal and not a wildcard. The escape character is configurable in the class instance using the EscapeChar property.

TWindowsMaskUTF8 provides an overridden Compile method which examines and converts the Mask expression into a list of op codes needed in Matches and MatchesWindowsMask. TWindowsMaskUTF8 re-implements the read and write access specifiers for the Mask property to use the FWindowsMask member for the property value.

Masks Overview
Member with the set of quirks enabled in the class instance, Member with the set of quirks in use in the compiled mask expression. Member with the Windows-specific mask expression for the class instance. Generates MaskOpCodes for optional characters or groups in a Windows-specific mask expression. Returns True if the specified character contains a Null character (#0) (ASCII NUL). True if the specified character contains a Null character (#0) (ASCII NUL). Character value examined in the method. Separates the specified value into file name and extension values.

SplitFileNameExtension is a method used to separate the value in ASourceFileName into file name and file extension values.

The values are returned in the AFileName and AExtension arguments. AExtension can contain an empty string ('') if a file extension is not found in ASourceFileName.

AIsMask indicates whether the value in ASourceFileName is treated as a mask specification instead of a file name. When set to True, a value like '.foo' is treated as a mask where the file name is '.foo' instead of a file extension, like a hidden file name on UNIX-like file systems.

Value examined and separated into its component values. File name portion of the specified source file name. File extension portion of the specified source file name. True if the source file name is treated as a mask specification for a file name and not a file extension; such as '.foo'. Constructor for the class instance.

Create is the overloaded, overridden constructor for the class instance. Create ensures that argument values are stored in the Mask, CaseSensitive, MaskOpCodes and Quirks properties. The inherited Create method is called prior to exit.

Mask expression for the class instance. True if masks are compared with case sensitivity. Set of TMaskOpCode values allowed in the class instance. Set of TWindowsQuirk values allowed in the class instance. Examines and converts the mask expression into a list of op codes.

Compile is an overridden method used to examine and convert values from the Mask property into TMaskParsedCode values for the positions in the mask expression. It provides support for Windows-specific quirks that enabled in the class instance using the Quirks property.

CaseSensitive indicates if case is significant for the value in Mask. Mask is converted to lowercase prior to processing in the method when CaseSensitive is set to False.

Compile examines each of the UTF-8 codepoints in Mask, and adds TMaskParsedCode values when the specific mask op codes are included in the MaskOpCodes property. The value in Mask is decomposed into file name and extension component values that are used in the method. These temporary values may be altered when particular quirks are enabled and encountered in the Mask expression. The modified values are applied to the Mask property prior to calling the inherited Compile method.

Compile may raise an exception when an invalid or incomplete mask is encountered. The the Compile method is called again with the same value in Mask, is simply returns False rather than re-raise the exception.

Compile is called from the Matches method when it has not already been compiled.

Indicates whether the specified value is a match for the Mask expression.

Matches calls Compile if the MaskOpCodes have not been compiled for the Mask expression, or when one the configuration parameters has been changed for the class instance.

Matches provides support for values in the Quirks property such as wqNoExtension. When specified and found in the mask expression, SplitFileNameExtension is called to get the file name and extension components for the value in AFileName.

Matches calls the inherited method to compare the specified values using the MaskOpCodes for the class instance. The return value is True if the file name argument matches the Mask expression. It is False if either the Mask expression or the compared value is too long or too short.

True when the specified value matches the Mask expression. Value examined and compared to the Mask expression. Contains the TWindowsQuirk enumeration values for file system quirks enabled in the class instance.

Quirks is a TWindowsQuirks property with the set of Windows-specific "quirks" allowed when comparing a value to the Mask expression. It contains values from the TWindowsQuirk enumeration which are enabled for the class instance.

The default value for Quirks is assigned using an argument passed to the Create constructor. If the value is omitted, values in the DefaultWindowsQuirks constant are assigned to the property.

Values in Quirks are used when the Compile method generates the MaskOpCodes for the Mask expression. Changing the property value causes the Compile method to be called during the next execution of the Matches method.

Implements the class type used to evaluate and compare Windows-specific mask expressions.

TWindowsMask is a TWindowsMaskUTF8 descendant, and introduces no new properties, methods, or events. It is essentially an alias for the TWindowsMaskUTF8 type.

Class type used to create new instances of the TMaskUtf8 type.

TMaskClass is the type returned from the GetMaskClass method in TMaskList.

Parses text into a list of strings using a specified line separator.

TParseStringList is a TStringList descendant used to parse text which uses the specified line separators. An alternate constructor is introduced with parameters for the lines of text and the separators used in the class instance.

TParseStringList is used to get a list of file masks from a string value in the TMaskList.Create method.

TStringList
Creates new string list by parsing the specified text using the separators argument.

Create is constructor for the class instance. Values in the AText and ASeparator arguments determine the content stored as lines of text in the instance. AText contains one or more file mask expressions separated by one of the delimiters characters n ASeparators.

Each line in the string list represents a single mask value from AText.

Text examine and parsed in the method. String with the separators used to delimit lines in the text argument. Implements a list for mask class instances.

TMaskList is a class used to maintain a list with TMaskUtf8 instances for mask expressions. Parameter values passed to the constructor are used when mask instances are created and stored in the list.

TMaskList is used in the implementation of the MatchesMaskList function.

Masks Overview
Gets the value for the Count property. Value for the Count property. Gets the value for the indexed Items property. Value for the Items property. Ordinal position for the TMask instance in the property value. Gets the class type used to create new mask instances for the list.

Used in the Create constructor to set the value for an internal member in the class instance.

Returns the TMaskClass type. Adds new mask instances to the list for the specified delimited mask expressions and settings.

Parses mask expression in AValue using the delimiter in ASeparator. A new TMaskClass instance is created and stored for each of the mask expressions using the settings in CaseSensitive and AOpcodesAllowed.

AddMasksToList is called from the Create constructor to populate the list using the mask expression(s) and settings passed as arguments to the method. It is also called when a new value is assigned to the Mask property.

Delimited mask expressions for the instances added to the list. Delimiter between mask expressions in AValue. Indicates whether mask expressions are case sensitive. Set of Mask op codes allowed for the class instances added to the list. Creates a new list with mask instances for the specified mask values, delimiter and options.

Create is the overloaded constructor for the class instance. Create allocates resources for the internal object list in the class instance. Arguments passed to the constructor are used to configure and populate the values in the Items property by calling the AddMasksToList method. Values in the arguments are also assigned to the CaseSensitive and MaskOpCodes properties.

The overloaded variant which does not include an Options parameter has been deprecated. It will be removed in a future Lazarus version.
Mask value(s) stored in the internal object list. Delimiter used to delimit mask values in AValue. Indicates if case sensitivity is used for masks. Indicates if case sensitivity is used for masks. Contains the options enabled for the mask instances. Destructor for the class instance.

Destroy is the overridden destructor for the class instance. Destroy ensures that the internal object list for the class instance is freed. Destroy calls the inherited destructor prior to exiting from the method.

Determines whether the specified file name matches a mask in the list.

Matches is a Boolean function used to determine if the specified file name matches one of the file masks in the list.

AFileName contains the file name examined in the method.

Matches uses the TMask or TWindowsMask instances in Items to perform the file name comparison. Each mask in Items is used to call its Matches method until a match is found, or until all of the masks have been visited.

The return value is True when a mask is found that matches the file name.

True when the file name matches one of the mask items. File name examined in the method. Tests whether the file name matches a mask in the list using Windows file system masks.

MatchesWindowsMask is a Boolean function used to determined whether the specified file name matches a Windows-specific file mask found in the list.

MatchesWindowsMask creates a temporary TWindowsMaskList instance using the values in Mask, CaseSensitive, and MaskOpCodes. The value in DefaultWindowsQuirks is used to specify which Windows-specific file masks are enabled in the class instance. Its Matches method is called to compare the value in AFileName to the Windows-specific file masks.

The return value is True if the file name matches one of the Windows© file masks in the comparison.

MatchesWindowsMask is available, but should not be called, in the TWindowsMaskList descendant. This would result in an unnecessary allocation of a TWindowsMaskList instance. Use TWindowsMaskList and call its Matches method instead.

Use the Matches method to compare a file name using the mask semantics for the file system represented by the TMask or TWindowsMask used in the implementation.

MatchesWindowsMask is deprecated in LazUtils version 2.3, and will be removed in LazUtils version 2.5. Use the TWindowsMaskList class and call its Matches method instead.
True if the file name matches one the masks in the list. File name examined in the method. The number of mask items in the list.

Count is a read-only Integer property with the number of TMask instances stored in the Items for the list.

The mask items in the list.

Items is a read-only indexed TMask property which contains the mask instances in the list.

Index specifies the ordinal position in the list for the TMask instance in the property. The property value is cast to a TMask instance when it is retrieved from the internal object list.

Values in the Items property are created in the Create constructor. A TMask instance is created and stored in Items for each of the mask values passed an argument to the method.

Use the Count property to determine the number of masks stored in Items.

Ordinal position for the value in the indexed property. String with the delimited list of mask expressions for the class instances in the list.

Mask can contain one or more mask expressions separated by the separator character passed as an argument to the Create constructor. The default separator character is ';' (Semicolon).

Setting a new value for the Mask property causes the internal list of mask instances to be cleared and recreated using the values in the CaseSensitive and MaskOpCodes properties.

The separator character cannot be changed in the current implementation. It is set using an argument passed to the constructor.
Contains the op codes allowed for the mask instances in the list.

MaskOpCodes is a TMaskOpCodes property with the set of mask op codes enabled in the class instance. It contains values from the TMaskOpCode enumeration, and is initially set from arguments passed to the Create constructor. Setting a new value for the property causes each of the TMask instances in Items to be updated with the new value for the property.

Indicates if a range expression is automatically reversed when the starting value is larger than the ending value.

Setting AutoReverseRange to True would cause a range expression like '[e-a]' to be treated as if it were specified as '[a-e]'. The default value for the property is True, as assigned in the Create constructor.

Changing the value for the property causes the corresponding property in each of the Items in the list to be updated. This causes Compile in the TMask instance to be called when it executes its Matches method.

Indicates whether the Mask expressions in the list are case sensitive.

CaseSensitive is a Boolean property which indicates whether Mask expressions in the list are case sensitive. The default value for the property is set using an argument passed to the overloaded constructor. It can be specified using the CaseSensitive argument, or by including / excluding moCaseSensitive in the TMaskOptions argument.

Changing the value for the property causes the corresponding property to be updated for each of the Items in the list.

The property value is used when a new value is assigned to the Mask property. It is passed as an argument to the AddMasksToList method called to re-populate the Items in the list.

Implements a list for mask class instances which support Windows-specific file masks.

TWindowsMaskList is a TMaskList descendant which implements a list used to maintain a list with TWindowsMask instances for mask expressions. Parameter values passed to the constructor are used when mask instances are created and stored in the list.

TWindowsMaskList is used in the implementation of the MatchesWindowsMaskList function.

Masks Overview
Gets the class type used to create new mask instances for the list. Returns the TWindowsMask type. Adds masks to the list using the delimited mask expressions and configuration settings. Delimited mask expressions for the instances added to the list. Delimiter between mask expressions in AValue. Indicates whether mask expressions are case sensitive. Set of Mask op codes allowed for the class instances added to the list. Constructor for the class instance.

Create is the overloaded constructor for the class instance. Create ensures that arguments passed to the constructor are stored in internal members for use in the class instance. The overloaded variants allow configuration settings to include a set of TMaskOption values or a set of Windows "quirks" supported for the masks in the list.

The variant with a TMaskOptions parameter has been deprecated in version 2.3, and will be removed in version 2.5.
Mask expression for the class instance. Delimiter used between mask expressions in AValue. True if the mask expression(s) are case sensitive. Set of TMaskOpCode values enabled for the class instance. Set of TWindowsQuirk values allowed for the class instance. Set of TMaskOption values enabled for the class instance. Contains the TWindowsQuirk values enabled for the class instance.

The initial value for the property is set using an argument passed to the Create constructor.

Changing the value for the property causes the internal TObjectList instance for the masks to be cleared. The AddMasksToList method is called to re-populate the internal list. The delimiter character, case sensitivity, and op codes provided to the constructor are used when re-creating the TWindowsMask instances in the Items property.

Not used in the current LazUtils implementation. Added (but not currently used) in LazUtils 3.0. Indicates whether the file name matches the specified mask.

MatchesMask is an overloaded Boolean function used to determine if the file name specified in FileName matches the specified Mask.

CaseSensitive indicates whether case sensitivity is used when comparing the file name to the mask value.

Options contains a set of zero or more TMaskOption values enabled for the comparison. The Options argument allows enabling or disabling set notation in the mask value. Specifying moDisableSets in the Options parameter will disable interpreting the ' [' character as the beginning of a set in the specified mask. Use an empty set (' []') when options from the TMaskOption enumeration are not needed.

For example:

MatchesMask('[x]','[x]',[moDisableSets]); // returns True

MatchesMask creates a TMask instance which is used to compare the file name to the mask using the specified options. Values in Mask and Options are passed as arguments the TMask constructor. The Matches method in the instance is called using FileName as an argument, and gets the return value for the function.

The overloaded variant which includes a TMaskOptions parameter has been marked as deprecated in version 2.3. It will be removed in version 2.5.
Masks Overview
True when the file name matches the mask value using the specified options. File name compared to the mask value. Mask used to perform the comparison. True when case sensitivity should be used in the comparison. Set of options enabled for the comparison. Set of TMaskOpCode values allowed in the comparison. Indicates whether the file name matches the specified Windows-style file system mask.

MatchesWindowsMask is an overloaded Boolean function used to determine if the file name specified in FileName matches the specified Mask. Mask can contain a Windows-style file mask which uses the following wildcards:

foo*.*
Matches all files starting with 'foo' regardless of the file extension. Same as foo* using the Matches method.
foo*.
Matches foo* but must not include a file extension.
*.
Matches any file name, but must not include a file extension.
foo.
Matches foo but not foo.txt.
foo.*
Matches foo, foo.txt, or foo.bar.
*.*
Matches any file name with an extension.

CaseSensitive indicates whether case sensitivity is used when comparing the file name to the mask value.

Options contains a set of zero or more TMaskOption values enabled for the comparison. The Options argument allows enabling or disabling set notation in the mask value. Specifying moDisableSets in the Options parameter will disable interpreting the ' [' character as the beginning of a set in the specified mask. Use an empty set (' []') when options from the TMaskOption enumeration are not needed.

For example:

MatchesWindowsMask('[x]','[x]',[moDisableSets]); // returns True

MatchesWindowsMask creates a TMask instance which is used to compare the file name to the mask using the specified options. Values in Mask and Options are passed as arguments the TMask constructor. The MatchesWindowsMask method in the TMask instance is called using FileName as an argument, and gets the return value for the function.

The overloaded variant which includes a TMaskOptions parameter has been marked as deprecated in version 2.3. It will be removed in version 2.5.
Masks Overview
True when the file name matches the mask value using the specified options. File name compared to the mask value. Mask used to perform the comparison. True when case sensitivity should be used in the comparison. Set of options enabled for the comparison. Determine whether the specified file name matches at least one of the specified masks.

MatchesMaskList is an overloaded Boolean function used to determine whether the specified file name matches at least one of the specified masks. MatchesMaskList is similar to MatchesWindowsMaskList, but uses Unix-style mask expressions. This includes use of the following:

foo*
Matches all files starting with 'foo' regardless of the file extension. Same as foo*.* using MatchesWindowsMaskList.
foo*.
Matches foo* but must not include a file extension.
*.
Matches any file name, but must not include a file extension.
foo.
Matches foo but not foo.txt.
foo.*
Matches foo, foo.txt, or foo.bar.
*
Matches any file name including those with an optional extension.

Overloaded variants of the routine provide arguments and configuration settings, including:

Filename
File name compared to the file masks in Mask.
Mask
Contains one or more file mask expressions separated by one of the values in Separator.
Separator
Contains the character(s) used as a separator between mask expressions in Mask. The default separator is the SemiColon (;) character.
Options
Contains TMaskOption values which enable or disable features in the comparison. The default value is an empty set ([]).
CaseSensitive
Indicates whether the file name to mask comparison is case sensitive.
The overloaded variant which includes the CaseSensitive argument has been deprecated. Use the variant that includes the TMaskOptions argument.

MatchesMaskList creates a TMaskList instance which uses the values in the Mask, Separator and Options parameters. Its Matches method is called to compare the value in Filename to the mask values in the list.

The return value is True when Filename matches at least one of the masks in the list.

The overloaded variant which includes a TMaskOptions parameter has been marked as deprecated in version 2.3. It will be removed in version 2.5.
Masks Overview
True when Filename matches at least one of the masks in the list. File name compared to the file masks in Mask. one or more file mask expressions separated by one of the values in Separator. Character(s) used as a separator between mask expressions in Mask. True if the file name to mask comparison is case sensitive. TMaskOption values which enable or disable features in the comparison. Set of TMaskOpCode values allowed in the comparison. Determine whether the specified file name matches at least one of the specified Windows-specific masks.

MatchesWindowsMaskList is an overloaded Boolean function used to determine whether the specified file name matches at least one of the specified masks. MatchesWindowsMaskList is similar to MatchesMaskList, but provides support for Windows-style file system masks in the Mask argument. This includes use of the following:

foo*.*
Matches all files starting with 'foo' regardless of the file extension. Same as foo* using MatchesMaskList.
foo*.
Matches foo* but must not include a file extension.
*.
Matches any file name, but must not include a file extension.
foo.
Matches foo but not foo.txt.
foo.*
Matches foo, foo.txt, or foo.bar.
*.*
Matches any file name with an extension.

Overloaded variants of the routine provide arguments and configuration settings, including:

Filename
File name compared to the file masks in Mask.
Mask
Contains one or more file mask expressions separated by one of the values in Separator.
Separator
Contains the character(s) used as a separator between mask expressions in Mask. The default separator is the SemiColon (;) character.
Options
Contains TMaskOption values which enable or disable features in the comparison. The default value is an empty set ([]).
CaseSensitive
Indicates whether the file name to mask comparison is case sensitive.
The overloaded variant which includes the CaseSensitive argument has been deprecated. Use the variant that includes the TMaskOptions argument.

MatchesWindowsMaskList creates a TMaskList instance which uses the values in the Mask, Separator and Options parameters. Its MatchesWindowsMask method is called to compare the value in Filename to the Windows-style masks in the list.

The return value is True when Filename matches at least one of the masks in the list.

The overloaded variant which includes a TMaskOptions parameter has been marked as deprecated in version 2.3. It will be removed in version 2.5.
Masks Overview
True when Filename matches at least one of the masks in the list. File name compared to the file masks in Mask. one or more file mask expressions separated by one of the values in Separator. Character(s) used as a separator between mask expressions in Mask. True if the file name to mask comparison is case sensitive. TMaskOption values which enable or disable features in the comparison. Set of TMaskOpCode values allowed in the comparison. Set of TWindowsQuirk values allowed in the comparison. Gets a string with the value(s) for the specified type. Used to format debugger messages.

DbgS is a overloaded String function used to get a formatted message which can be displayed in the debugger. Values from the type passed as an argument are converted to their string representation and used in the return value for the routine. In the masks.pas unit, TMaskOpCodes and TWindowsQuirks types are allowed as parameter values. Both types result in a String with the set notation needed to represent enumeration values in the argument. For example:

TMaskOpCode
'[mocAnyChar,mocAnyText,mocRange,mocSet]'
TWindowsQuirks
'[wqAnyExtension,wqFilenameEnd,wqAllByExtension,wqNoExtension]'
String representing the value(s) in the specified type. TMaskOpCodes instance with the values returned in the string result. TWindowsQuirks instance with the values returned in the string result. Masks Overview.

What is a Mask

A mask is an expression composed of literal characters, sets or ranges or characters, and wildcards. It is commonly used to match file or directory names against the pattern in the mask expression.

Literal Characters

Each literal character in a mask corresponds to a single character in a compared value. For instance, 'abc.def' would match a file with that exact name and nothing else.

Sets of Characters

A set begins with an opening Square Bracket character ([) and ends with a closing Square Bracket character (]). Values between the brackets represent the characters which are considered a match in that position. For example: 'a[bcd]e.txt' would match 'abe.txt', 'ace.txt', or 'ade.txt'. It would not match 'afe.txt'.

Ranges of Characters

A range begins with an opening Square Bracket character ([) and ends with a closing Square Bracket character (]) like set notation. Character values in the range are defined using a starting and ending values separated by a '-' character. For example: 'abc[0-9].ext'. Any character in the range is allowed in that position. An alpha-numeric range could be represented using '[a-zA-Z0-9]'.

Negated Sets and Ranges

When using set or range notation, character values can be excluded using negation. This is expressed using an Exclamation Point character (!) in front of the set or range specification. For example: '[!0-9]' or '[!0123456789]'. In both examples, any character at the given position except the values in the set or range would be considered a match.

Wildcards

Wildcards allow one or more characters to be considered as a match for a value in the mask. The wildcard characters are '?' and '*'. ? matches a single character (regardless of its value). * matches any number of characters (regardless of their values).

Platform-specific Behaviors

While the syntax and notation for mask expressions is the same for different platforms, there are some platform-specific behaviors.

The TMask and TWindowsMask classes are used to isolate and implement those platform-specific behaviors. TMask conforms the to conventions used for UNIX-like file systems. TWindowsMask implements the conventions used for Windows-based file systems.

Most differences between the platforms center on the implementation of the "any file" mask. This is the notation used to match any file or directory name, regardless of the presence of a file extension. For UNIX file systems (TMask), the notation used is '*'. For Windows file systems (TWindowsMask), the notation '*.*' is used.

They are other Windows-specific behaviors originating from its CP/M and MS DOS heritage. These are implemented in TWindowsMask as Quirks, and can be enabled or disabled in the class instance.

Please refer to the documentation for the TMask and TWindowsMask for more information about mask expressions and their usage in the respective classes.

Configurable Settings

The Mask classes also contain configurable settings which can affect their behavior in their Matches method. For example: CaseSensitive, AutoReverseRange, EscapeChar, and MaskOpCodes. The initial value for these settings can be passed as arguments to the class constructors, or they can be specified using properties in the class instances. MaskOpCodes is particularly important; it determines the behavior of wildcards and escape characters in the mask expression.

Mask Expression Examples

The following FPC unit test program is provided which demonstrates the mask expressions allowed in TMaskUTF8 and TWindowsMaskUTF8 class instances:

$(LazarusDir)/components/lazutils/test/testmasks.lpr

Using Masks

Mask expressions are typically passed as an argument to new instances of TMask or TWindowsMask. It can also be passed as an argument to routines like MatchesMask, MatchesMaskList, MatchesWindowsMask, or MatchesWindowsMaskList. The expression can be assigned to the Mask property in an existing mask class instance. In general, the mask is used to find file names that match the mask expression. But they are not limited to that single use case. They can be used to determine if any string value matches a valid mask expression. They are like regular expressions without all of the complexity, and focused on specific functionality.

Mask values as used in TMask are not related to the mask values used in the TMaskEdit control. Although both compare string values to determine if they match a particular pattern, they use different symbols and syntax.