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" />
                    
Directory.Packages.props
<PackageReference Include="MatchBrackets" />
                    
Project file
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
                    
#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
                    
Install as a Cake Addin
#tool nuget:?package=MatchBrackets&version=1.1.0
                    
Install as a Cake Tool

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 null when brackets are not balanced. GetPairsMultiple returns null on 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; null if unbalanced.
  • bool IsBalanced(string input, char openingnBracket = '(', char closingBracket = ')')
    • true for balanced, false otherwise.

New in 1.1.0:

  • int[]? GetDepthMap(string text, char openingBracket = '(', char closingBracket = ')')
    • Per-character depth map; -1 for non-bracket chars; null if unbalanced.
  • int FindMatchingIndex(string text, int position, char openingBracket = '(', char closingBracket = ')')
    • Returns partner index for the bracket at position, or -1 if invalid/unbalanced.
  • List<(int openIndex, int closeIndex, int depth)>? GetPairsWithDepth(string text, char openingBracket = '(', char closingBracket = ')')
    • All pairs with their nesting depth (outermost = 0); null if unbalanced.
  • (int index, char character, string reason)? GetFirstMismatch(string text, char openingBracket = '(', char closingBracket = ')')
    • First mismatch diagnostic; null if balanced.
  • Dictionary<int,int>? GetPairsMultiple(string text, params (char open, char close)[] bracketTypes)
    • Matches multiple types simultaneously (defaults to () { } [ ] < >). Returns null on mismatch/imbalance; empty dictionary for null/empty input.

Behavior details

  • Unbalanced input:
    • GetPairs, GetDepthMap, GetPairsWithDepthnull
    • FindMatchingIndex-1
    • GetPairsMultiplenull (unless the input is null/empty → returns empty dictionary)
  • Empty or null strings:
    • IsBalancedtrue
    • GetPairsMultiple → empty dictionary
Product 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.

Version Downloads Last Updated
1.1.0 187 11/25/2025
1.0.5 699 5/15/2025
1.0.4 500 4/22/2024
1.0.3 1,001 1/31/2024
1.0.2 164 1/31/2024
1.0.1 168 1/31/2024
1.0.0 177 1/31/2024

1.1.0
- Added multi-targeting: net6.0 + net8.0
- No API changes; purely additive.