use oneyaml_core::code::{
INVALID_ESCAPE, INVALID_QUOTED_CHARACTER, QUOTED_SCALAR_INDENTATION, UNTERMINATED_QUOTED_SCALAR,
};
use oneyaml_core::{Code, Replacement, Span};
use super::scan::{
any_marker_at, break_len, byte, char_at, is_nb_json, plain_line_end, skip_white,
};
pub(super) trait Sink {
fn from_raw(raw: &str) -> Self;
fn push_str(&mut self, s: &str);
fn push_char(&mut self, c: char);
fn push_repeat(&mut self, c: char, n: usize);
fn len(&self) -> usize;
fn truncate(&mut self, len: usize);
}
impl Sink for String {
fn from_raw(raw: &str) -> Self {
raw.to_owned()
}
fn push_str(&mut self, s: &str) {
Self::push_str(self, s);
}
fn push_char(&mut self, c: char) {
self.push(c);
}
fn push_repeat(&mut self, c: char, n: usize) {
self.extend(core::iter::repeat_n(c, n));
}
fn len(&self) -> usize {
Self::len(self)
}
fn truncate(&mut self, len: usize) {
if self.is_char_boundary(len) {
Self::truncate(self, len);
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) struct Count(pub(super) usize);
impl Sink for Count {
fn from_raw(raw: &str) -> Self {
Self(raw.len())
}
fn push_str(&mut self, s: &str) {
self.0 = self.0.saturating_add(s.len());
}
fn push_char(&mut self, c: char) {
self.0 = self.0.saturating_add(c.len_utf8());
}
fn push_repeat(&mut self, c: char, n: usize) {
self.0 = self.0.saturating_add(c.len_utf8().saturating_mul(n));
}
fn len(&self) -> usize {
self.0
}
fn truncate(&mut self, len: usize) {
self.0 = self.0.min(len);
}
}
pub(super) fn split_lines(text: &str) -> impl Iterator<Item = &str> {
let mut rest = Some(text);
core::iter::from_fn(move || {
let t = rest?;
let Some(at) = t.find(['\n', '\r']) else {
rest = None;
return Some(t);
};
let width = break_len(t, at);
rest = t.get(at.saturating_add(width)..);
t.get(..at)
})
}
pub(super) struct Flow<S> {
pub(super) end: usize,
pub(super) value: Option<S>,
pub(super) len: usize,
pub(super) multiline: bool,
}
struct NextLine {
start: usize,
spaces: usize,
content: usize,
empties: usize,
bad_empty: bool,
}
fn next_line(src: &str, i: usize, parent: Option<usize>) -> NextLine {
let mut j = i.saturating_add(break_len(src, i));
let mut empties = 0usize;
loop {
let spaces = src
.as_bytes()
.get(j..)
.map_or(0, |r| r.iter().take_while(|b| **b == b' ').count());
let content = skip_white(src, j);
let blen = break_len(src, content);
let tabbed = content > j.saturating_add(spaces);
let bad_empty = blen > 0 && tabbed && parent.is_some_and(|n| spaces <= n);
if blen == 0 || bad_empty {
return NextLine {
start: j,
spaces,
content,
empties,
bad_empty,
};
}
empties = empties.saturating_add(1);
j = content.saturating_add(blen);
}
}
fn push_fold<S: Sink>(out: &mut S, empties: usize) {
if empties == 0 {
out.push_char(' ');
} else {
out.push_repeat('\n', empties);
}
}
pub(super) fn plain(
src: &str,
start: usize,
first_end: usize,
parent: Option<usize>,
flow: bool,
) -> Flow<Count> {
let mut end = first_end;
let mut multiline = false;
loop {
let after = skip_white(src, end);
if break_len(src, after) == 0 {
break;
}
let line = next_line(src, after, parent);
let stop = line.bad_empty
|| line.content >= src.len()
|| any_marker_at(src, line.start) && line.start == line.content
|| parent.is_some_and(|n| line.spaces <= n)
|| byte(src, line.content) == Some(b'#');
if stop {
break;
}
let line_end = plain_line_end(src, line.content, flow);
if line_end <= line.content {
break;
}
multiline = true;
end = line_end;
}
let text = src.get(start..end).unwrap_or("");
let len = if multiline {
plain_value::<Count>(text).0
} else {
text.len()
};
Flow {
end,
value: None,
len,
multiline,
}
}
pub(super) fn plain_value<S: Sink>(text: &str) -> S {
let mut lines = split_lines(text);
let first = lines.next().unwrap_or("").trim_matches([' ', '\t']);
let mut out = S::from_raw(first);
let mut empties = 0usize;
for line in lines {
let line = line.trim_matches([' ', '\t']);
if line.is_empty() {
empties = empties.saturating_add(1);
} else {
push_fold(&mut out, empties);
out.push_str(line);
empties = 0;
}
}
out
}
fn escape(src: &str, e: usize) -> Option<(char, usize)> {
let hex = |n: usize| -> Option<(char, usize)> {
let from = e.saturating_add(1);
let digits = src.get(from..from.saturating_add(n))?;
if digits.len() != n || !digits.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
let v = u32::from_str_radix(digits, 16).ok()?;
Some((char::from_u32(v)?, n.saturating_add(1)))
};
let simple = match char_at(src, e)? {
'0' => '\0',
'a' => '\x07',
'b' => '\x08',
't' | '\t' => '\t',
'n' => '\n',
'v' => '\x0b',
'f' => '\x0c',
'r' => '\r',
'e' => '\x1b',
' ' => ' ',
'"' => '"',
'/' => '/',
'\\' => '\\',
'N' => '\u{85}',
'_' => '\u{a0}',
'L' => '\u{2028}',
'P' => '\u{2029}',
'x' => return hex(2),
'u' => return hex(4),
'U' => return hex(8),
_ => return None,
};
Some((simple, 1))
}
#[derive(Debug, Default)]
pub(super) struct EscapeScan {
pub(super) first: Option<Span>,
pub(super) edits: Vec<Replacement>,
pub(super) valid: bool,
pub(super) ambiguous: bool,
pub(super) apostrophe: bool,
pub(super) end: Option<usize>,
}
pub(super) fn escape_scan(src: &str, start: usize) -> EscapeScan {
let mut scan = EscapeScan::default();
let mut i = start.saturating_add(1);
while let Some(c) = char_at(src, i) {
match c {
'"' => {
scan.end = Some(i.saturating_add(1));
break;
}
'\n' | '\r' => break,
'\\' => {
let e = i.saturating_add(1);
if let Some((_, len)) = escape(src, e) {
scan.valid = true;
scan.ambiguous |= !matches!(char_at(src, e), Some('\\' | '"'));
i = e.saturating_add(len);
continue;
}
let Some(next) = char_at(src, e).filter(|n| !matches!(n, '\n' | '\r')) else {
break;
};
let after = e.saturating_add(next.len_utf8());
scan.first.get_or_insert(Span::new(i, after));
scan.apostrophe |= next == '\'';
scan.edits.push(if next == '\'' {
Replacement::new(Span::new(i, e), "")
} else {
Replacement::new(Span::empty(i), "\\")
});
i = after;
}
c => {
scan.apostrophe |= c == '\'';
i = i.saturating_add(c.len_utf8());
}
}
}
scan
}
pub(super) type QuotedError = (Code, &'static str);
fn check_line(src: &str, line: &NextLine, parent: Option<usize>) -> Result<(), QuotedError> {
if line.bad_empty {
return Err((
QUOTED_SCALAR_INDENTATION,
"an empty line with a tab must be indented like the scalar's content",
));
}
if line.content >= src.len() {
return Err((UNTERMINATED_QUOTED_SCALAR, "quoted scalar is not closed"));
}
if line.start == line.content && any_marker_at(src, line.start) {
return Err((
UNTERMINATED_QUOTED_SCALAR,
"document marker inside a quoted scalar",
));
}
if parent.is_some_and(|n| line.spaces <= n) {
return Err((
QUOTED_SCALAR_INDENTATION,
"quoted scalar continuation line is not indented enough",
));
}
Ok(())
}
pub(super) fn quoted<S: Sink>(
src: &str,
start: usize,
parent: Option<usize>,
double: bool,
) -> Result<Flow<S>, QuotedError> {
let body = start.saturating_add(1);
let mut out: Option<S> = None;
let mut ws_mark: Option<usize> = None;
let mut multiline = false;
let mut i = body;
let own = |out: &mut Option<S>, upto: usize| -> S {
out.take()
.unwrap_or_else(|| S::from_raw(src.get(body..upto).unwrap_or("")))
};
loop {
let Some(c) = char_at(src, i) else {
return Err((UNTERMINATED_QUOTED_SCALAR, "quoted scalar is not closed"));
};
match c {
'\'' if !double && byte(src, i.saturating_add(1)) == Some(b'\'') => {
let mut o = own(&mut out, i);
o.push_char('\'');
out = Some(o);
ws_mark = None;
i = i.saturating_add(2);
}
'\'' if !double => break,
'"' if double => break,
'\\' if double => {
let e = i.saturating_add(1);
let mut o = own(&mut out, i);
ws_mark = None;
if break_len(src, e) > 0 {
multiline = true;
let line = next_line(src, e, parent);
check_line(src, &line, parent)?;
o.push_repeat('\n', line.empties);
i = line.content;
} else {
let (ch, len) =
escape(src, e).ok_or((INVALID_ESCAPE, "invalid escape sequence"))?;
o.push_char(ch);
i = e.saturating_add(len);
}
out = Some(o);
}
'\n' | '\r' => {
multiline = true;
let mut o = match out.take() {
Some(mut o) => {
if let Some(mark) = ws_mark {
o.truncate(mark);
}
o
}
None => {
let text = src.get(body..i).unwrap_or("");
S::from_raw(text.trim_end_matches([' ', '\t']))
}
};
ws_mark = None;
let line = next_line(src, i, parent);
check_line(src, &line, parent)?;
push_fold(&mut o, line.empties);
out = Some(o);
i = line.content;
}
c if is_nb_json(c) => {
if let Some(o) = out.as_mut() {
if c == ' ' || c == '\t' {
ws_mark.get_or_insert(o.len());
} else {
ws_mark = None;
}
o.push_char(c);
}
i = i.saturating_add(c.len_utf8());
}
_ => {
return Err((
INVALID_QUOTED_CHARACTER,
"control character in a quoted scalar",
));
}
}
}
let len = out.as_ref().map_or(i.saturating_sub(body), Sink::len);
Ok(Flow {
end: i.saturating_add(1),
value: out,
len,
multiline,
})
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::indexing_slicing)]
use super::*;
fn q(src: &str, double: bool) -> Result<(usize, String, bool), Code> {
quoted::<String>(src, 0, None, double)
.map(|f| {
let raw = src.get(1..f.end.saturating_sub(1)).unwrap_or("").to_owned();
let v = f.value.unwrap_or(raw);
assert_eq!(f.len, v.len());
(f.end, v, f.multiline)
})
.map_err(|(c, _)| c)
}
#[test]
fn single_quoted() {
assert_eq!(q("'a''b' x", false), Ok((6, "a'b".into(), false)));
assert_eq!(
q("'a \n b\n\n c'", false),
Ok((13, "a b\nc".into(), true))
);
assert_eq!(q("'a", false), Err(UNTERMINATED_QUOTED_SCALAR));
assert_eq!(q("'a\n---\n'", false), Err(UNTERMINATED_QUOTED_SCALAR));
}
#[test]
fn double_quoted() {
assert_eq!(q(r#""a\tb\x41\u00e9\"""#, true).unwrap().1, "a\tbAé\"");
assert_eq!(q("\"a \\\n b\"", true).unwrap().1, "a b");
assert_eq!(q("\"a\\\n\n b\"", true).unwrap().1, "a\nb");
assert_eq!(q("\"a \\t\n b\"", true).unwrap().1, "a \t b");
assert_eq!(q(r#""\q""#, true), Err(INVALID_ESCAPE));
assert_eq!(q(r#""\x4""#, true), Err(INVALID_ESCAPE));
assert_eq!(
quoted::<String>("\"a\nb\"", 0, Some(0), true)
.map_err(|e| e.0)
.err(),
Some(QUOTED_SCALAR_INDENTATION)
);
}
fn p(src: &str, first_end: usize, parent: Option<usize>, flow: bool) -> (usize, String, bool) {
let f = plain(src, 0, first_end, parent, flow);
let v: String = plain_value(&src[..f.end]);
assert_eq!(f.len, v.len(), "{src:?}");
(f.end, v, f.multiline)
}
#[test]
fn plain_multi_line() {
assert_eq!(
p("a b\n c\n\n d\ne", 3, Some(0), false),
(12, "a b c\nd".into(), true)
);
assert_eq!(p("a\n# c\n b", 1, None, false), (1, "a".into(), false));
assert_eq!(p("a\n---\n", 1, None, false).0, 1);
assert_eq!(p("a\n- b", 1, None, false).1, "a - b");
assert_eq!(p("a\n b]", 1, None, true).1, "a b");
assert_eq!(p("a\n ]", 1, None, true).0, 1);
assert_eq!(p("a \r\n b\r\r c\t\n\t d", 1, None, false).1, "a b\nc d");
}
#[test]
fn sinks_agree() {
let mut s = String::from_raw("ab");
let mut c = Count::from_raw("ab");
s.push_char('\u{e9}');
c.push_char('\u{e9}');
s.push_repeat('\n', 3);
c.push_repeat('\n', 3);
Sink::push_str(&mut s, "xyz");
c.push_str("xyz");
assert_eq!((Sink::len(&s), c.len()), (10, 10));
Sink::truncate(&mut s, 2);
c.truncate(2);
assert_eq!((s.as_str(), c.0), ("ab", 2));
let mut e = String::from("\u{e9}");
Sink::truncate(&mut e, 1);
assert_eq!(e, "\u{e9}");
let lines: Vec<&str> = split_lines("a\r\nb\rc\n").collect();
assert_eq!(lines, ["a", "b", "c", ""]);
}
}