ConfigurationPattern Class
Definition
- Namespace
- Bodu.Text.Configuration
- Assembly
- Bodu.Text.Configuration.dll
- Package
- Bodu.Text.Configuration 1.0.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).{n1..n2}- matches any decimal integer in the inclusive range.-
[seq]- matches a single character in the set;[!seq]matches any character not in the set. \- 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.
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 contained an unbalanced brace or bracket.
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 contained an unbalanced brace or bracket.
IsMatch(string)
Determines whether the supplied relative path matches this pattern.
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 |