Skip to content

Commit

Permalink
Add comments re level-run processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfkthame committed Mar 4, 2024
1 parent 7233d8d commit aa06343
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/explicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use super::prepare::removed_by_x9;
use super::LevelRunVec;
use super::TextSource;

/// Compute explicit embedding levels for one paragraph of text (X1-X8).
/// Compute explicit embedding levels for one paragraph of text (X1-X8), and identify
/// level runs (BD7) for use when determining Isolating Run Sequences (X10).
///
/// `processing_classes[i]` must contain the `BidiClass` of the char at byte index `i`,
/// for each char in `text`.
Expand Down Expand Up @@ -191,10 +192,13 @@ pub fn compute<'a, T: TextSource<'a> + ?Sized>(
processing_classes[i + j] = processing_classes[i];
}

// Check if we need to start a new level run.
// Identify level runs to be passed to prepare::isolating_run_sequences().
if i == 0 {
// Initialize for the first (or only) run.
current_run_level = levels[i];
} else {
// Check if we need to start a new level run.
// <https://www.unicode.org/reports/tr9/#BD7>
if !removed_by_x9(original_classes[i]) && levels[i] != current_run_level {
// End the last run and start a new one.
runs.push(current_run_start..i);
Expand All @@ -204,6 +208,7 @@ pub fn compute<'a, T: TextSource<'a> + ?Sized>(
}
}

// Append the trailing level run, if non-empty.
if levels.len() > current_run_start {
runs.push(current_run_start..levels.len());
}
Expand Down
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,24 @@ mod tests {
#[cfg(feature = "hardcoded-data")]
fn test_process_text() {
let tests = vec![
(
// text
"",
// base level
Some(RTL_LEVEL),
// levels
Level::vec(&[]),
// original_classes
vec![],
// paragraphs
vec![],
// levels_u16
Level::vec(&[]),
// original_classes_u16
vec![],
// paragraphs_u16
vec![],
),
(
// text
"abc123",
Expand Down Expand Up @@ -1741,6 +1759,19 @@ mod tests {
paragraphs: t.4.clone(),
}
);
// If it was empty, also test that ParagraphBidiInfo handles it safely.
if t.4.len() == 0 {
assert_eq!(
ParagraphBidiInfo::new(t.0, t.1),
ParagraphBidiInfo {
text: t.0,
original_classes: t.3.clone(),
levels: t.2.clone(),
paragraph_level: RTL_LEVEL,
is_pure_ltr: true,
}
)
}
// If it was a single paragraph, also test ParagraphBidiInfo.
if t.4.len() == 1 {
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl IsolatingRunSequence {
///
/// <http://www.unicode.org/reports/tr9/#BD7>
///
/// Only used for tests; runs are identified during explicit::compute.
/// This is only used by tests; normally level runs are identified during explicit::compute.
#[cfg(test)]
fn level_runs(levels: &[Level], original_classes: &[BidiClass]) -> Vec<LevelRun> {
assert_eq!(levels.len(), original_classes.len());
Expand Down

0 comments on commit aa06343

Please sign in to comment.