find-matching-bracket

Crates.iofind-matching-bracket
lib.rsfind-matching-bracket
version0.2.0
sourcesrc
created_at2024-07-17 22:41:28.220863
updated_at2024-07-17 22:43:18.145778
descriptionFinds the matching closing bracket for a given opening bracket in a string. Supports curly braces, square brackets, and parentheses. This crate is useful for parsing code, validating expressions, and more.
homepage
repositoryhttps://github.com/klebs6/klebs-general
max_upload_size
id1306658
size9,180
(klebs6)

documentation

README

find-matching-bracket

find-matching-bracket is a Rust crate that helps you find the matching closing bracket for a given opening bracket in a string. It supports curly braces, square brackets, and parentheses. This crate is useful for parsing code, validating expressions, and more.

Features

  • Supports curly braces {}, square brackets [], and parentheses ().
  • Handles nested brackets.
  • Handles large inputs efficiently.
  • Supports Unicode characters.

Usage

Add this crate to your Cargo.toml:

[dependencies]
find-matching-bracket = "0.2.0"

Example

use find_matching_bracket::{find_matching_curly_brace, find_matching_square_bracket, find_matching_paren};

fn main() {
    let text = "{content}";
    let position = find_matching_curly_brace(text, 0);
    println!("Matching position: {:?}", position); // Output: Matching position: Some(8)
}

Functions

find_matching_bracket

Finds the matching closing bracket for the given opening bracket in a string.

Parameters

  • text: The input string.
  • start: The starting position of the opening bracket.
  • bracket_type: The type of bracket (Curly, Square, or Paren).

Returns

  • Option<usize>: The position of the matching closing bracket, or None if no matching bracket is found.

find_matching_curly_brace

Finds the matching closing curly bracket {} for the given opening bracket in a string.

Parameters

  • text: The input string.
  • start: The starting position of the opening curly bracket.

Returns

  • Option<usize>: The position of the matching closing bracket, or None if no matching bracket is found.

find_matching_square_bracket

Finds the matching closing square bracket [] for the given opening bracket in a string.

Parameters

  • text: The input string.
  • start: The starting position of the opening square bracket.

Returns

  • Option<usize>: The position of the matching closing bracket, or None if no matching bracket is found.

find_matching_paren

Finds the matching closing parenthesis () for the given opening bracket in a string.

Parameters

  • text: The input string.
  • start: The starting position of the opening parenthesis.

Returns

  • Option<usize>: The position of the matching closing bracket, or None if no matching bracket is found.

Example

let text = "{[()][]}";
let position = find_matching_curly_brace(text, 0);
assert_eq!(position, Some(7));

License

This project is licensed under the MIT License.

Commit count: 110

cargo fmt