Crate unicode_matching

Crate unicode_matching 

Source
Expand description

Rust library crate to match Unicode open/close brackets

Source is generated by a Perl script (bin/matching.pl) to download and parse the following Unicode database files.

Original idea is from this StackOverflow thread and this comment.

§Example

// Use the `FindMatching` trait
use unicode_matching::FindMatching;

// Generate the close/open `BTreeMap`s
let close = unicode_matching::close();
let open = unicode_matching::open();

let s = "fn main() {\n    println!(\"Hello!\");\n}";
//       000000000011 11111111222222 2222333 333 33
//       012345678901 23456789012345 6789012 345 67

// Match the open/close parentheses in `main()`
assert_eq!(s.find_matching(7, &close, &open), 8);
assert_eq!(s.find_matching(8, &close, &open), 7);

// Match the open/close curly braces in `main() {...}`
assert_eq!(s.find_matching(10, &close, &open), 36);
assert_eq!(s.find_matching(36, &close, &open), 10);

// Match the open/close parentheses in `println!("...")`
assert_eq!(s.find_matching(24, &close, &open), 33);
assert_eq!(s.find_matching(33, &close, &open), 24);

let length = s.len();
let more = length + 1;

// Any other index (whether valid or invalid) returns itself
assert_eq!(s.find_matching(0, &close, &open), 0);
assert_eq!(s.find_matching(length, &close, &open), length);
assert_eq!(s.find_matching(more, &close, &open), more);

// Note that regular/straight single or double quotes do not match because they aren't valid
// matching graphemes according to Unicode
assert_eq!(s.find_matching(25, &close, &open), 25);
assert_eq!(s.find_matching(32, &close, &open), 32);

// ... but they can be added manually and then it works
use std::collections::BTreeMap;
let close = unicode_matching::close().into_iter().chain([("'", "'"), ("\"", "\"")]).collect::<BTreeMap<_, _>>();
let open = unicode_matching::open().into_iter().chain([("'", "'"), ("\"", "\"")]).collect::<BTreeMap<_, _>>();
assert_eq!(s.find_matching(25, &close, &open), 32);
assert_eq!(s.find_matching(32, &close, &open), 25);

Constants§

ALL
Matching open/close brackets from UnicodeData.txt, BidiBrackets.txt, and BidiMirroring.txt
BRACKETS
Matching open/close brackets from BidiBrackets.txt
BRACKETS_MATCHING
Matching open/close brackets from UnicodeData.txt and BidiBrackets.txt
BRACKETS_MIRRORING
Matching open/close brackets from BidiMirroring.txt and BidiBrackets.txt
MATCHING
Matching open/close brackets from UnicodeData.txt
MIRRORING
Matching open/close brackets from BidiMirroring.txt
MIRRORING_MATCHING
Matching open/close brackets from UnicodeData.txt and BidiMirroring.txt

Traits§

FindMatching
Trait to provide the FindMatching::find_matching method

Functions§

close
Generate a BTreeMap with the matching close bracket for each open bracket in UnicodeData.txt
close_all
Generate a BTreeMap with the matching close bracket for each open bracket in UnicodeData.txt, BidiBrackets.txt, and BidiMirroring.txt
close_brackets
Generate a BTreeMap with the matching close bracket for each open bracket in BidiBrackets.txt
close_brackets_matching
Generate a BTreeMap with the matching close bracket for each open bracket in UnicodeData.txt and BidiBrackets.txt
close_brackets_mirroring
Generate a BTreeMap with the matching close bracket for each open bracket in BidiMirroring.txt and BidiBrackets.txt
close_mirroring
Generate a BTreeMap with the matching close bracket for each open bracket in BidiMirroring.txt
close_mirroring_matching
Generate a BTreeMap with the matching close bracket for each open bracket in UnicodeData.txt and BidiMirroring.txt
matching
Generate a BTreeMap with an entry for each pair of opening and closing brackets in UnicodeData.txt
matching_all
Generate a BTreeMap with an entry for each pair of opening and closing brackets in UnicodeData.txt, BidiBrackets.txt, and BidiMirroring.txt
matching_brackets
Generate a BTreeMap with an entry for each pair of opening and closing brackets in BidiBrackets.txt
matching_brackets_matching
Generate a BTreeMap with an entry for each pair of opening and closing brackets in UnicodeData.txt and BidiBrackets.txt
matching_brackets_mirroring
Generate a BTreeMap with an entry for each pair of opening and closing brackets in BidiMirroring.txt and BidiBrackets.txt
matching_mirroring
Generate a BTreeMap with an entry for each pair of opening and closing brackets in BidiMirroring.txt
matching_mirroring_matching
Generate a BTreeMap with an entry for each pair of opening and closing brackets in UnicodeData.txt and BidiMirroring.txt
open
Generate a BTreeMap with the matching open bracket for each close bracket in UnicodeData.txt
open_all
Generate a BTreeMap with the matching open bracket for each close bracket in UnicodeData.txt, BidiBrackets.txt, and BidiMirroring.txt
open_brackets
Generate a BTreeMap with the matching open bracket for each close bracket in BidiBrackets.txt
open_brackets_matching
Generate a BTreeMap with the matching open bracket for each close bracket in UnicodeData.txt and BidiBrackets.txt
open_brackets_mirroring
Generate a BTreeMap with the matching open bracket for each close bracket in BidiMirroring.txt and BidiBrackets.txt
open_mirroring
Generate a BTreeMap with the matching open bracket for each close bracket in BidiMirroring.txt
open_mirroring_matching
Generate a BTreeMap with the matching open bracket for each close bracket in UnicodeData.txt and BidiMirroring.txt