Coalesce whitespace in macro args.

This commit is contained in:
Tom Alexander
2023-10-08 15:08:21 -04:00
parent 37bc5ef712
commit a32cea8139
4 changed files with 63 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
use std::borrow::Borrow;
use std::borrow::Cow;
use super::util::coalesce_whitespace;
use super::util::coalesce_whitespace_if_line_break;
use super::util::remove_line_break;
use super::util::remove_whitespace_if_line_break;
@@ -149,6 +150,9 @@ pub struct AngleLink<'s> {
pub struct OrgMacro<'s> {
pub source: &'s str,
pub macro_name: &'s str,
/// The macro args from the source.
///
/// This does not take into account the post-processing that you would get from the upstream emacs org-mode AST. Use `get_macro_args` for an equivalent value.
pub macro_args: Vec<&'s str>,
pub macro_value: &'s str,
}
@@ -733,3 +737,9 @@ impl<'s> AngleLink<'s> {
self.search_option.map(remove_whitespace_if_line_break)
}
}
impl<'s> OrgMacro<'s> {
pub fn get_macro_args<'b>(&'b self) -> impl Iterator<Item = Cow<'s, str>> + 'b {
self.macro_args.iter().map(|arg| coalesce_whitespace(*arg))
}
}

View File

@@ -197,3 +197,49 @@ enum CoalesceWhitespaceIfLineBreakState {
ret: String,
},
}
/// Removes all whitespace from a string.
///
/// Example: "foo bar" => "foobar" and "foo \n bar" => "foobar".
pub(crate) fn coalesce_whitespace<'s>(input: &'s str) -> Cow<'s, str> {
let mut state = CoalesceWhitespace::Normal;
for (offset, c) in input.char_indices() {
match (&mut state, c) {
(CoalesceWhitespace::Normal, ' ' | '\t' | '\r' | '\n') => {
let mut ret = String::with_capacity(input.len());
ret.push_str(&input[..offset]);
ret.push(' ');
state = CoalesceWhitespace::HasWhitespace {
in_whitespace: true,
ret,
};
}
(CoalesceWhitespace::Normal, _) => {}
(
CoalesceWhitespace::HasWhitespace { in_whitespace, ret },
' ' | '\t' | '\r' | '\n',
) => {
if !*in_whitespace {
*in_whitespace = true;
ret.push(' ');
}
}
(CoalesceWhitespace::HasWhitespace { in_whitespace, ret }, _) => {
*in_whitespace = false;
ret.push(c);
}
}
}
match state {
CoalesceWhitespace::Normal => Cow::Borrowed(input),
CoalesceWhitespace::HasWhitespace {
in_whitespace: _,
ret,
} => Cow::Owned(ret),
}
}
enum CoalesceWhitespace {
Normal,
HasWhitespace { in_whitespace: bool, ret: String },
}