Skip to content

Commit

Permalink
fix: re-enable optimized debug assertions in Symbol::flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
nfejzic committed Oct 9, 2023
1 parent 1a404bd commit 35f9919
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
41 changes: 24 additions & 17 deletions commons/src/scanner/symbol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,35 @@ impl Symbol<'_> {
}

/// Flattens the input of consecutive symbols. Returns the slice of input starting from start
/// position of first symbol until the end of last symbol.
/// position of first symbol until the end of last symbol. Returns [`None`] if slice is empty.
///
/// # Panics
///
/// It's assumed that all [`Symbol`]s in slice reference the same input. If not, the function
/// might panic (guaranteed in debug) if inputs are not the same and last [`Symbol`] in slice
/// references input that is longer than the one referenced in the first [`Symbol`].
///
/// # Examples
///
/// ```
/// use unimarkup_commons::scanner::{scan_str, Symbol};
///
/// let input = "This is a string";
/// let symbols: Vec<_> = scan_str(input);
///
/// Returns `None` if the referenced input in the given symbols is not the same.
pub fn flatten(symbols: &[Self]) -> &str {
// debug_assert!(symbols
// .windows(2)
// .all(|window| window[0].input == window[1].input));
/// assert_eq!(input, Symbol::flatten(&symbols).unwrap());
/// ```
pub fn flatten(symbols: &[Self]) -> Option<&str> {
let (first, last) = (symbols.first()?, symbols.last()?);

let first = symbols.first();
let last = symbols.last();
debug_assert_eq!(first.input, last.input);

match (first, last) {
(Some(first), Some(last)) => {
let input = first.input;
let input = first.input;

let start = first.offset.start;
let end = last.offset.end;
let start = first.offset.start;
let end = last.offset.end;

&input[start..end]
}
_ => "",
}
Some(&input[start..end])
}

/// Flattens the iterator of consecutive symbols. Returns the slice of input starting from start
Expand Down
2 changes: 1 addition & 1 deletion commons/src/test_runner/snap_test_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<'a> SnapTestRunner<'a> {
SnapTestRunner {
info: None,
desc: None,
input: Some(Symbol::flatten(input.as_ref())),
input: Symbol::flatten(input.as_ref()),
name: name.into(),
sub_path: None,
snapshot,
Expand Down
4 changes: 2 additions & 2 deletions inline/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl<'input> TokenIterator<'input> {

let curr_index = self.index + symbol_len;
let content = subst.map_or_else(
|| Symbol::flatten(&self.symbols[self.index..curr_index]),
|| Symbol::flatten(&self.symbols[self.index..curr_index]).unwrap(),
|subst| subst.as_str(),
);

Expand Down Expand Up @@ -476,7 +476,7 @@ impl<'input> TokenIterator<'input> {
kind,
span,
spacing,
content: Some(content),
content,
};

Some(token)
Expand Down

0 comments on commit 35f9919

Please sign in to comment.