Skip to content

Commit

Permalink
Introduce BidiMatchedOpeningBracket
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Dec 20, 2022
1 parent 96722a0 commit 02f3ddc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/char_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use core::cmp::Ordering::{Equal, Greater, Less};

#[cfg(feature = "hardcoded-data")]
use self::tables::bidi_class_table;
use crate::data_source::BidiMatchedOpeningBracket;
use crate::BidiClass::*;
#[cfg(feature = "hardcoded-data")]
use crate::BidiDataSource;

/// Hardcoded Bidi data that ships with the unicode-bidi crate.
///
/// This can be enabled with the default `hardcoded-data` Cargo feature.
Expand All @@ -45,11 +45,14 @@ pub fn bidi_class(c: char) -> BidiClass {
/// If this character is a bracket according to BidiBrackets.txt,
/// return the corresponding *normalized* *opening bracket* of the pair,
/// and whether or not it itself is an opening bracket.
pub(crate) fn bidi_matched_opening_bracket(c: char) -> Option<(char, bool)> {
pub(crate) fn bidi_matched_opening_bracket(c: char) -> Option<BidiMatchedOpeningBracket> {
for pair in self::tables::bidi_pairs_table {
if pair.0 == c || pair.1 == c {
let skeleton = pair.2.unwrap_or(pair.0);
return Some((skeleton, pair.0 == c));
return Some(BidiMatchedOpeningBracket {
opening: skeleton,
is_open: pair.0 == c,
});
}
}
None
Expand Down
16 changes: 15 additions & 1 deletion src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@

use crate::BidiClass;

/// This is the return value of [`BidiDataSource::bidi_matched_opening_bracket()`].
///
/// It represents the matching *normalized* opening bracket for a given bracket in a bracket pair,
/// and whether or not that bracket is opening.
pub struct BidiMatchedOpeningBracket {
/// The corresponding opening bracket in this bracket pair, normalized
///
/// In case of opening brackets, this will be the bracket itself, except for when the bracket
/// is not normalized, in which case it will be the normalized form.
pub opening: char,
/// Whether or not the requested bracket was an opening bracket. True for opening
pub is_open: bool,
}

/// This trait abstracts over a data source that is able to produce the Unicode Bidi class for a given
/// character
pub trait BidiDataSource {
Expand All @@ -25,7 +39,7 @@ pub trait BidiDataSource {
/// (since this data is small and changes less often), and in part so that this method can be
/// added without needing a breaking version bump.
/// Override this method in your custom data source to prevent the use of hardcoded data.
fn bidi_matched_opening_bracket(&self, c: char) -> Option<(char, bool)> {
fn bidi_matched_opening_bracket(&self, c: char) -> Option<BidiMatchedOpeningBracket> {
crate::char_data::bidi_matched_opening_bracket(c)
}
}
8 changes: 4 additions & 4 deletions src/implicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ fn identify_bracket_pairs<D: BidiDataSource>(
continue;
}

if let Some((opening, is_open)) = data_source.bidi_matched_opening_bracket(ch) {
if is_open {
if let Some(matched) = data_source.bidi_matched_opening_bracket(ch) {
if matched.is_open {
// If an opening paired bracket is found ...

// ... and there is no room in the stack,
Expand All @@ -398,7 +398,7 @@ fn identify_bracket_pairs<D: BidiDataSource>(
break;
}
// ... push its Bidi_Paired_Bracket property value and its text position onto the stack
stack.push((opening, i))
stack.push((matched.opening, i))
} else {
// If a closing paired bracket is found, do the following

Expand All @@ -409,7 +409,7 @@ fn identify_bracket_pairs<D: BidiDataSource>(
for (stack_index, element) in stack.iter().enumerate().rev() {
// Compare the closing paired bracket being inspected or its canonical
// equivalent to the bracket in the current stack element.
if element.0 == opening {
if element.0 == matched.opening {
// If the values match, meaning the two characters form a bracket pair, then

// Append the text position in the current stack element together with the
Expand Down

0 comments on commit 02f3ddc

Please sign in to comment.