mirror of
https://github.com/typst/typst
synced 2025-08-12 22:27:56 +08:00
Add split_prefix_suffix
helper
This commit is contained in:
parent
467968af07
commit
ab9ba8c387
@ -2,6 +2,7 @@ use typst_library::diag::SourceResult;
|
||||
use typst_library::foundations::{Packed, StyleChain};
|
||||
use typst_library::layout::{Abs, Axis, Rel};
|
||||
use typst_library::math::{EquationElem, LrElem, MidElem};
|
||||
use typst_utils::SliceExt;
|
||||
use unicode_math_class::MathClass;
|
||||
|
||||
use super::{stretch_fragment, MathContext, MathFragment, DELIM_SHORT_FALL};
|
||||
@ -29,15 +30,7 @@ pub fn layout_lr(
|
||||
let mut fragments = ctx.layout_into_fragments(body, styles)?;
|
||||
|
||||
// Ignore leading and trailing ignorant fragments.
|
||||
let start_idx = fragments
|
||||
.iter()
|
||||
.position(|f| !f.is_ignorant())
|
||||
.unwrap_or(fragments.len());
|
||||
let end_idx = fragments
|
||||
.iter()
|
||||
.skip(start_idx)
|
||||
.rposition(|f| !f.is_ignorant())
|
||||
.map_or(start_idx, |i| start_idx + i + 1);
|
||||
let (start_idx, end_idx) = fragments.split_prefix_suffix(|f| f.is_ignorant());
|
||||
let inner_fragments = &mut fragments[start_idx..end_idx];
|
||||
|
||||
let axis = scaled!(ctx, styles, axis_height);
|
||||
|
@ -128,6 +128,20 @@ pub trait SliceExt<T> {
|
||||
where
|
||||
F: FnMut(&T) -> K,
|
||||
K: PartialEq;
|
||||
|
||||
/// Computes two indices which split a slice into three parts.
|
||||
///
|
||||
/// - A prefix which matches `f`
|
||||
/// - An inner portion
|
||||
/// - A suffix which matches `f` and does not overlap with the prefix
|
||||
///
|
||||
/// If all elements match `f`, the prefix becomes `self` and the suffix
|
||||
/// will be empty.
|
||||
///
|
||||
/// Returns the indices at which the inner portion and the suffix start.
|
||||
fn split_prefix_suffix<F>(&self, f: F) -> (usize, usize)
|
||||
where
|
||||
F: FnMut(&T) -> bool;
|
||||
}
|
||||
|
||||
impl<T> SliceExt<T> for [T] {
|
||||
@ -157,6 +171,19 @@ impl<T> SliceExt<T> for [T] {
|
||||
fn group_by_key<K, F>(&self, f: F) -> GroupByKey<'_, T, F> {
|
||||
GroupByKey { slice: self, f }
|
||||
}
|
||||
|
||||
fn split_prefix_suffix<F>(&self, mut f: F) -> (usize, usize)
|
||||
where
|
||||
F: FnMut(&T) -> bool,
|
||||
{
|
||||
let start = self.iter().position(|v| !f(v)).unwrap_or(self.len());
|
||||
let end = self
|
||||
.iter()
|
||||
.skip(start)
|
||||
.rposition(|v| !f(v))
|
||||
.map_or(start, |i| start + i + 1);
|
||||
(start, end)
|
||||
}
|
||||
}
|
||||
|
||||
/// This struct is created by [`SliceExt::group_by_key`].
|
||||
|
Loading…
x
Reference in New Issue
Block a user