MatchBrackets 1.1.0
dotnet add package MatchBrackets --version 1.1.0
NuGet\Install-Package MatchBrackets -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MatchBrackets" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MatchBrackets" Version="1.1.0" />
<PackageReference Include="MatchBrackets" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MatchBrackets --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MatchBrackets, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MatchBrackets@1.1.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MatchBrackets&version=1.1.0
#tool nuget:?package=MatchBrackets&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MatchBrackets
Simple, fast bracket matching utilities for .NET 6 and .NET 8. Finds matching pairs, validates balance, computes nesting depth, and supports multiple bracket types.
- Namespace:
MatchBrackets - Primary type:
Brackets - Target frameworks: .NET 6 and .NET 8
- Thread-safe: Yes (pure static, no shared mutable state)
- Time complexity: O(n)
- Memory complexity: O(n)
Install
- Add a project reference, or install the NuGet package if published.
- Requires .NET 6 or .NET 8.
Quick start
Notes:
- Only the specified bracket characters are considered; all other characters are ignored.
- Many APIs return
nullwhen brackets are not balanced.GetPairsMultiplereturnsnullon mismatch, but returns an empty dictionary for null/empty input.
Here are practical scenarios where MatchBrackets fits well.
Editor/IDE features
- Brace colorization and highlighting: GetPairs, GetPairsMultiple, GetPairsWithDepth, GetDepthMap
- Go to matching brace / caret navigation: FindMatchingIndex
- Code folding/outlining regions from bracketed ranges: GetPairs, GetPairsWithDepth
- Smart selection expansion to the enclosing pair: FindMatchingIndex, GetPairsWithDepth
- Auto-indentation/formatting based on nesting: GetDepthMap, GetPairsWithDepth
Validation and diagnostics
- CI/lint checks for templates, expressions, and configs using (), {}, [], <>: IsBalanced, GetFirstMismatch, GetPairsMultiple
- Pre-parse sanity checks in calculators, DSLs, query builders, SQL/Expression evaluators: IsBalanced, GetFirstMismatch
- User input feedback (highlight first error position/reason): GetFirstMismatch
Parsing and transformations
- Bracket-aware tokenization/splitting (e.g., split commas only at depth 0): GetDepthMap, GetPairsWithDepth
API reference
Single bracket type (defaults to ( and )):
Dictionary<int,int>? GetPairs(string text, char openingnBracket = '(', char closingBracket = ')')- Returns opening→closing index pairs;
nullif unbalanced.
- Returns opening→closing index pairs;
bool IsBalanced(string input, char openingnBracket = '(', char closingBracket = ')')truefor balanced,falseotherwise.
New in 1.1.0:
int[]? GetDepthMap(string text, char openingBracket = '(', char closingBracket = ')')- Per-character depth map;
-1for non-bracket chars;nullif unbalanced.
- Per-character depth map;
int FindMatchingIndex(string text, int position, char openingBracket = '(', char closingBracket = ')')- Returns partner index for the bracket at
position, or-1if invalid/unbalanced.
- Returns partner index for the bracket at
List<(int openIndex, int closeIndex, int depth)>? GetPairsWithDepth(string text, char openingBracket = '(', char closingBracket = ')')- All pairs with their nesting depth (outermost = 0);
nullif unbalanced.
- All pairs with their nesting depth (outermost = 0);
(int index, char character, string reason)? GetFirstMismatch(string text, char openingBracket = '(', char closingBracket = ')')- First mismatch diagnostic;
nullif balanced.
- First mismatch diagnostic;
Dictionary<int,int>? GetPairsMultiple(string text, params (char open, char close)[] bracketTypes)- Matches multiple types simultaneously (defaults to
() { } [ ] < >). Returnsnullon mismatch/imbalance; empty dictionary for null/empty input.
- Matches multiple types simultaneously (defaults to
Behavior details
- Unbalanced input:
GetPairs,GetDepthMap,GetPairsWithDepth→nullFindMatchingIndex→-1GetPairsMultiple→null(unless the input is null/empty → returns empty dictionary)
- Empty or null strings:
IsBalanced→trueGetPairsMultiple→ empty dictionary
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.1.0
- Added multi-targeting: net6.0 + net8.0
- No API changes; purely additive.