Refactor test print to be more readable

pull/1814/head
Gokul Soumya 3 years ago committed by Blaž Hrastnik
parent 45b76db506
commit 64c2490f2d

@ -28,88 +28,69 @@ use std::cmp::Reverse;
/// Panics when missing head or anchor. /// Panics when missing head or anchor.
/// Panics when head come after head or anchor come after anchor. /// Panics when head come after head or anchor come after anchor.
pub fn print(s: &str) -> (String, Selection) { pub fn print(s: &str) -> (String, Selection) {
let mut primary = None; let mut primary_idx = None;
let mut ranges = SmallVec::new(); let mut ranges = SmallVec::new();
let mut iter = s.chars().peekable(); let mut iter = s.chars().peekable();
let mut left = String::with_capacity(s.len()); let mut left = String::with_capacity(s.len());
'outer: while let Some(c) = iter.next() { 'outer: while let Some(c) = iter.next() {
let start = left.len(); let start = left.len();
if c == '#' {
if iter.next_if_eq(&'[').is_some() { if c != '#' {
if primary.is_some() { left.push(c);
panic!("primary `#[` already appeared {left:?} {s:?}"); continue;
} }
if iter.next_if_eq(&'|').is_some() {
while let Some(c) = iter.next() { let (is_primary, close_pair) = match iter.next() {
if c == ']' && iter.next_if_eq(&'#').is_some() { Some('[') => (true, ']'),
primary = Some(ranges.len()); Some('(') => (false, ')'),
ranges.push(Range::new(left.len(), start)); Some(ch) => {
continue 'outer; left.push(ch);
} else { continue;
left.push(c); }
} None => break,
} };
panic!("missing primary end `]#` {left:?} {s:?}");
} else { if is_primary && primary_idx.is_some() {
while let Some(c) = iter.next() { panic!("primary `#[` already appeared {left:?} {s:?}");
if c == '|' { }
if let Some(cc) = iter.next_if_eq(&']') {
if iter.next_if_eq(&'#').is_some() { let head_at_beg = iter.next_if_eq(&'|').is_some();
primary = Some(ranges.len());
ranges.push(Range::new(start, left.len())); while let Some(c) = iter.next() {
continue 'outer; if !(c == close_pair && iter.peek() == Some(&'#')) {
} else {
left.push(c);
left.push(cc);
}
} else {
left.push(c);
}
} else {
left.push(c);
}
}
panic!("missing primary end `|]#` {left:?} {s:?}");
}
} else if iter.next_if_eq(&'(').is_some() {
if iter.next_if_eq(&'|').is_some() {
while let Some(c) = iter.next() {
if c == ')' && iter.next_if_eq(&'#').is_some() {
ranges.push(Range::new(left.len(), start));
continue 'outer;
} else {
left.push(c);
}
}
panic!("missing end `)#` {left:?} {s:?}");
} else {
while let Some(c) = iter.next() {
if c == '|' {
if let Some(cc) = iter.next_if_eq(&')') {
if iter.next_if_eq(&'#').is_some() {
ranges.push(Range::new(start, left.len()));
continue 'outer;
} else {
left.push(c);
left.push(cc);
}
} else {
left.push(c);
}
} else {
left.push(c);
}
}
panic!("missing end `|)#` {left:?} {s:?}");
}
} else {
left.push(c); left.push(c);
continue;
}
if !head_at_beg {
let prev = left.pop().unwrap();
if prev != '|' {
left.push(prev);
left.push(c);
continue;
}
}
iter.next(); // skip "#"
if is_primary {
primary_idx = Some(ranges.len());
} }
let (anchor, head) = match head_at_beg {
true => (left.len(), start),
false => (start, left.len()),
};
ranges.push(Range::new(anchor, head));
continue 'outer;
}
if head_at_beg {
panic!("missing end `{close_pair}#` {left:?} {s:?}");
} else { } else {
left.push(c); panic!("missing end `|{close_pair}#` {left:?} {s:?}");
} }
} }
let primary = match primary {
let primary = match primary_idx {
Some(i) => i, Some(i) => i,
None => panic!("missing primary `#[|]#` {s:?}"), None => panic!("missing primary `#[|]#` {s:?}"),
}; };

Loading…
Cancel
Save