pub fn is_space(ch: char) -> bool {
ch.is_whitespace() || ('\u{1c}'..='\u{1f}').contains(&ch)
}
pub fn flatten(text: &str) -> String {
text.split(is_space)
.filter(|word| !word.is_empty())
.collect::<Vec<_>>()
.join(" ")
}
pub fn splitlines(text: &str) -> Vec<&str> {
let mut lines = Vec::new();
let mut start = 0;
let mut chars = text.char_indices().peekable();
while let Some((at, ch)) = chars.next() {
let mut after = at + ch.len_utf8();
match ch {
'\r' => {
if chars.peek().is_some_and(|(_, next)| *next == '\n') {
chars.next();
after += 1;
}
}
'\n' | '\u{b}' | '\u{c}' | '\u{1c}' | '\u{1d}' | '\u{1e}' | '\u{85}' | '\u{2028}'
| '\u{2029}' => {}
_ => continue,
}
lines.push(&text[start..at]);
start = after;
}
if start < text.len() {
lines.push(&text[start..]);
}
lines
}
pub fn strip(text: &str) -> &str {
text.trim_matches(is_space)
}
pub fn rstrip(text: &str) -> &str {
text.trim_end_matches(is_space)
}
pub fn lstrip(text: &str) -> &str {
text.trim_start_matches(is_space)
}
pub fn len(text: &str) -> usize {
text.chars().count()
}
pub fn head(text: &str, at: usize) -> &str {
let end = text
.char_indices()
.nth(at)
.map_or(text.len(), |(byte, _)| byte);
&text[..end]
}
pub fn tail(text: &str, at: usize) -> &str {
let start = text
.char_indices()
.nth(at)
.map_or(text.len(), |(byte, _)| byte);
&text[start..]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn whitespace_is_pythons() {
assert!(is_space('\u{1c}') && is_space('\u{a0}') && is_space('\n'));
assert!(!is_space('x') && !is_space('\u{200b}'));
assert_eq!(flatten(" a \u{1f}b\n\n c "), "a b c");
assert_eq!(strip(" \u{1d}x\u{85}"), "x");
assert_eq!((rstrip("x \n"), lstrip("\t x ")), ("x", "x "));
}
#[test]
fn splitlines_is_pythons() {
assert_eq!(splitlines("a\r\nb\rc\nd\n"), ["a", "b", "c", "d"]);
assert_eq!(splitlines("a\u{2028}b\u{c}c"), ["a", "b", "c"]);
assert_eq!(splitlines(""), Vec::<&str>::new());
assert_eq!(splitlines("\n\n"), ["", ""]);
assert_eq!(splitlines("one"), ["one"]);
}
#[test]
fn indices_count_characters() {
assert_eq!(len("café ✓"), 6);
assert_eq!(head("café ✓", 4), "café");
assert_eq!(tail("café ✓", 5), "✓");
assert_eq!((head("ab", 9), tail("ab", 9)), ("ab", ""));
}
}