use std::borrow::Cow;
use std::iter::{FusedIterator, Peekable};
use std::ops::Range;
use std::str::CharIndices;
use super::Arg;
#[derive(Debug, Clone)]
pub struct Args<'a> {
text: &'a str,
pos: usize,
}
#[derive(Debug, Clone)]
pub struct Normalised<'a> {
spans: Args<'a>,
}
#[must_use]
pub const fn args(text: &str) -> Args<'_> {
Args { text, pos: 0 }
}
#[must_use]
pub const fn normalised(text: &str) -> Normalised<'_> {
Normalised { spans: args(text) }
}
impl Iterator for Args<'_> {
type Item = Range<usize>;
fn next(&mut self) -> Option<Range<usize>> {
let rest = self.text.get(self.pos..)?;
let start = self.pos + rest.find(|c| !is_break(c))?;
self.pos = word_end(self.text, start);
Some(start..self.pos)
}
}
impl FusedIterator for Args<'_> {}
impl<'a> Iterator for Normalised<'a> {
type Item = Arg<'a>;
fn next(&mut self) -> Option<Arg<'a>> {
let span = self.spans.next()?;
let raw = self.spans.text.get(span.clone())?;
Some(Arg {
span,
value: decode(raw),
})
}
}
impl FusedIterator for Normalised<'_> {}
const fn is_dash(c: char) -> bool {
matches!(c, '-' | '\u{2013}' | '\u{2014}' | '\u{2015}')
}
const fn is_single_quote(c: char) -> bool {
matches!(c, '\'' | '\u{2018}' | '\u{2019}' | '\u{201a}' | '\u{201b}')
}
const fn is_double_quote(c: char) -> bool {
matches!(c, '"' | '\u{201c}' | '\u{201d}' | '\u{201e}')
}
const fn is_break(c: char) -> bool {
matches!(
c,
' ' | '\t' | '\n' | '\r' | '\u{b}' | '\u{c}' | '\u{85}' | '\u{a0}'
) || matches!(
c,
'\u{1680}' | '\u{2000}'..='\u{200a}' | '\u{2028}' | '\u{2029}'
) || matches!(c, '\u{202f}' | '\u{205f}' | '\u{3000}')
}
type Chars<'a> = Peekable<CharIndices<'a>>;
fn word_end(text: &str, start: usize) -> usize {
let mut chars: Chars<'_> = text
.get(start..)
.unwrap_or_default()
.char_indices()
.peekable();
while let Some((i, c)) = chars.next() {
if is_break(c) {
return start + i;
}
if c == '`' {
chars.next();
} else if is_single_quote(c) {
skip_single(&mut chars);
} else if is_double_quote(c) {
skip_double(&mut chars);
}
}
text.len()
}
fn skip_single(chars: &mut Chars<'_>) {
while let Some((_, c)) = chars.next() {
if is_single_quote(c) && chars.next_if(|&(_, d)| is_single_quote(d)).is_none() {
return;
}
}
}
fn skip_double(chars: &mut Chars<'_>) {
while let Some((_, c)) = chars.next() {
if c == '`' {
chars.next();
} else if is_double_quote(c) && chars.next_if(|&(_, d)| is_double_quote(d)).is_none() {
return;
}
}
}
const fn unescape(c: char) -> char {
match c {
'0' => '\0',
'a' => '\u{7}',
'b' => '\u{8}',
'e' => '\u{1b}',
'f' => '\u{c}',
'n' => '\n',
'r' => '\r',
't' => '\t',
'v' => '\u{b}',
other => other,
}
}
fn decode(raw: &str) -> Cow<'_, str> {
let dash = raw.starts_with(|c: char| is_dash(c) && c != '-');
if !dash && !raw.contains(|c: char| c == '`' || is_single_quote(c) || is_double_quote(c)) {
return Cow::Borrowed(raw);
}
let mut out = String::with_capacity(raw.len());
let mut chars = raw.chars().peekable();
if dash {
chars.next();
out.push('-');
}
while let Some(c) = chars.next() {
if c == '`' {
out.extend(chars.next().map(unescape));
} else if is_single_quote(c) {
while let Some(d) = chars.next() {
if !is_single_quote(d) {
out.push(d);
} else if let Some(escaped) = chars.next_if(|&e| is_single_quote(e)) {
out.push(escaped);
} else {
break;
}
}
} else if is_double_quote(c) {
while let Some(d) = chars.next() {
if d == '`' {
out.extend(chars.next().map(unescape));
} else if !is_double_quote(d) {
out.push(d);
} else if let Some(escaped) = chars.next_if(|&e| is_double_quote(e)) {
out.push(escaped);
} else {
break;
}
}
} else {
out.push(c);
}
}
Cow::Owned(out)
}
#[cfg(test)]
mod tests;