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.
- https://www.unicode.org/Public/UNIDATA/UnicodeData.txt
- https://www.unicode.org/Public/UNIDATA/BidiBrackets.txt
- https://www.unicode.org/Public/UNIDATA/BidiMirroring.txt
Original idea is from this StackOverflow thread and this comment.
Example
// Use the `FindMatching` trait
use FindMatching;
// Generate the close/open `BTreeMap`s
let close = close;
let open = 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!;
assert_eq!;
// Match the open/close curly braces in `main() {...}`
assert_eq!;
assert_eq!;
// Match the open/close parentheses in `println!("...")`
assert_eq!;
assert_eq!;
let length = s.len;
let more = length + 1;
// Any other index (whether valid or invalid) returns itself
assert_eq!;
assert_eq!;
assert_eq!;
// Also note that regular/straight single or double quotes do not match because they aren't valid
// matching graphemes according to Unicode
assert_eq!;
assert_eq!;
// ... but they can be added manually and then it works
use BTreeMap;
let close = close.into_iter.chain.;
let open = open.into_iter.chain.;
assert_eq!;
assert_eq!;