ConfigurationPattern Class
Definition
- Namespace
- Bodu.Text.Configuration
- Assembly
- Bodu.Text.Configuration.dll
- Package
- Bodu.Text.Configuration 1.3.0
A compiled EditorConfig-style glob pattern that matches forward-slash-delimited paths.
public sealed class ConfigurationPattern
- Inheritance
-
ConfigurationPattern
- Inherited Members
- Extension Methods
Examples
// Compile once, match many times.
ConfigurationPattern csFiles = ConfigurationPattern.Compile("**/*.cs");
Console.WriteLine(csFiles.IsMatch("src/Foo.cs")); // true
Console.WriteLine(csFiles.IsMatch("docs/notes.md")); // false
// Alternation + numeric range.
ConfigurationPattern markup = ConfigurationPattern.Compile("**/*.{md,mdx,txt}");
ConfigurationPattern logs = ConfigurationPattern.Compile("logs/run-{1..99}.log");
Remarks
Patterns support the EditorConfig glob grammar:
*- matches any character except/.**- matches any sequence of characters including/.?- matches a single character except/.-
{a,b,c}- matches any of the comma-separated alternatives (nesting permitted). A brace group without a top-level comma is not a choice: its braces are literal text, so{single}matches only{single}and{}only{}. -
{n1..n2}- matches any decimal integer in the inclusive range. Both bounds must be integers; otherwise the group is literal text, so{a..z}matches only{a..z}. -
[seq]- matches a single character in the set;[!seq]matches any character not in the set. A bracket expression that holds a/is literal text, brackets included, because a single character cannot match a path separator. \- escapes the next character so it is matched literally.
Anchoring follows EditorConfig: a pattern with no / matches at any directory depth; a pattern with /
is anchored to the start of the relative path. A pattern that starts with / is anchored at the root as well,
and a path given relative to the root matches it as if the path began with /, so /src/*.cs matches
both src/Foo.cs and /src/Foo.cs, but not lib/src/Foo.cs.
Compilation parses the glob once into a culture-invariant Regex; subsequent IsMatch(string) calls are allocation-free over the compiled state. Cache compiled patterns when the same glob is matched repeatedly against many paths.
Properties
Source
Gets the source pattern as authored.
public string Source { get; }
Property Value
- string
The original glob expression.
Methods
Compile(string)
Compiles the supplied glob expression using ordinal (case-sensitive) matching.
public static ConfigurationPattern Compile(string pattern)
Parameters
patternstringThe pattern to compile.
Returns
- ConfigurationPattern
A compiled ConfigurationPattern.
Exceptions
- ArgumentException
patternis null, empty, or contains only whitespace.- ConfigurationParseException
The pattern is not a valid glob. The exception's diagnostic carries the ConfigurationDiagnosticCode:
UnbalancedBracketorUnbalancedBrace,BraceNestingTooDeepfor braces nested more than 32 deep,NumericRangeTooLargefor a numeric range of more than 10,000 values, orPatternTooLongfor more than 4,096 characters.
Compile(string, StringComparison)
Compiles the supplied glob expression under the requested string comparison. Case-insensitive comparisons ( OrdinalIgnoreCase, InvariantCultureIgnoreCase, CurrentCultureIgnoreCase) compile the regex with IgnoreCase; case-sensitive comparisons do not.
public static ConfigurationPattern Compile(string pattern, StringComparison comparison)
Parameters
patternstringThe pattern to compile.
comparisonStringComparisonThe comparison applied during matching.
Returns
- ConfigurationPattern
A compiled ConfigurationPattern.
Remarks
Compiled patterns are memoized in a bounded process-wide cache so that repeated resolve calls against the same
document avoid re-walking the glob grammar. The cache key is the pair (pattern, comparison); on overflow
the cache is cleared in bulk.
Exceptions
- ArgumentException
patternis null, empty, or contains only whitespace.- ConfigurationParseException
The pattern is not a valid glob. The exception's diagnostic carries the ConfigurationDiagnosticCode:
UnbalancedBracketorUnbalancedBrace,BraceNestingTooDeepfor braces nested more than 32 deep,NumericRangeTooLargefor a numeric range of more than 10,000 values, orPatternTooLongfor more than 4,096 characters.
IsMatch(string)
Determines whether the supplied relative path matches this pattern. When the pattern starts with /, a
path that does not is matched as if it began with /, because both name the same file under the root.
public bool IsMatch(string relativePath)
Parameters
relativePathstringThe path to test, with forward-slash separators.
Returns
Exceptions
- ArgumentNullException
relativePathis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |