1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
//! Sift duplicate whitespaces away in just one function call.
//!
//! This crate helps you remove duplicate [whitespaces](https://doc.rust-lang.org/reference/whitespace.html) within the `&str`.
//! Other than that, it naturally removes the whitespaces at the start and end of the `&str`.
//!
//! # Examples
//!
//! ```rust
//! use whitespace_sifter::*;
//!
//! // This prints `1.. 2.. 3.. 4.. 5..`.
//! println!("{}", sift(
//! "1.. \n2.. \n\n3.. \n\n\n4.. \n\n\n\n5.. \n\n\n\n\n"
//! ));
//!
//! // This prints `A..\r\nB..\r\nC..\r\nD..\r\nE..`.
//! println!("{}", sift_with_carriage_return(
//! "A..\r\n B..\r\n\r\n C..\r\n\r\n\r\n D..\r\n\r\n\r\n\r\n E..\r\n\r\n\r\n\r\n\r\n"
//! ));
//!
//! // This prints `1..\n2..\n3..\n4..\n5..`.
//! println!("{}", preserve_newline::sift(
//! "1.. \n2.. \n\n3.. \n\n\n4.. \n\n\n\n5.. \n\n\n\n\n"
//! ));
//!
//! // This prints `A..\r\nB..\r\nC..\r\nD..\r\nE..`.
//! println!("{}", preserve_newline::sift_with_carriage_return(
//! "A.. \r\n B.. \r\n\r\n C.. \r\n\r\n\r\n D.. \r\n\r\n\r\n\r\n E.. \r\n\r\n\r\n\r\n\r\n"
//! ));
//! ```
/// This remove duplicate [whitespaces](https://doc.rust-lang.org/reference/whitespace.html) within the `&str`.
///
/// If the `&str` contains carriage-returns do not use this.
/// Use [`whitespace-sifter::sift_with_carriage_return(...)`](./fn.sift_with_carriage_return.html) instead.
pub fn sift(input: &str) -> String {
let mut out: String = String::with_capacity(input.len());
let mut is_last_whitespace: bool = false;
for c in input.trim().chars() {
let is_whitespace: bool = c.is_ascii_whitespace();
if is_whitespace && is_last_whitespace {
continue;
}
is_last_whitespace = is_whitespace;
out.push(c);
}
out
}
/// This remove duplicate [whitespaces](https://doc.rust-lang.org/reference/whitespace.html) within the `&str` that contains carriage-returns.
///
/// This treats carriage-returns as just one `char` in the `&str`.
/// If the `&str` does not contain carriage-returns do not use this.
/// Use [`whitespace-sifter::sift(...)`](./fn.sift.html) instead.
pub fn sift_with_carriage_return(input: &str) -> String {
let mut out: String = String::with_capacity(input.len());
let mut is_last_whitespace: bool = false;
let mut is_last_carriage_return: bool = false;
for c in input.trim().chars() {
let is_carriage_return: bool = c == '\r';
let is_newline: bool = c == '\n';
let is_whitespace: bool = c.is_ascii_whitespace();
if is_newline && is_last_carriage_return {
out.push(c);
is_last_carriage_return = false;
continue;
}
if is_whitespace && is_last_whitespace {
continue;
}
out.push(c);
is_last_carriage_return = is_carriage_return;
is_last_whitespace = is_whitespace;
}
out
}
#[cfg(feature = "preserve_newline")]
/// Sift through all the lines in the `&str` while preserving deduplicated newlines.
/// This is only available if the `preserve_newline` feature is explicitly turned on. (default)
pub mod preserve_newline {
/// This remove duplicate [whitespaces](https://doc.rust-lang.org/reference/whitespace.html) within the `&str`.
///
/// If the `&str` contains carriage-returns do not use this.
/// Use [`whitespace-sifter::sift_with_carriage_return(...)`](./fn.sift_with_carriage_return.html) instead.
pub fn sift(input: &str) -> String {
let mut out: String = String::with_capacity(input.len());
for val in input.trim().split('\n') {
let val: &str = val.trim();
if val.is_empty() {
continue;
}
let mut is_last_whitespace: bool = false;
for c in val.trim().chars() {
let is_whitespace: bool = c.is_ascii_whitespace();
if is_whitespace && is_last_whitespace {
continue;
}
is_last_whitespace = is_whitespace;
out.push(c);
}
out.push('\n');
}
out.remove(out.len() - 1);
out
}
/// This remove duplicate [whitespaces](https://doc.rust-lang.org/reference/whitespace.html) within the `&str` that contains carriage-returns.
///
/// This treats carriage-returns as just one `char` in the `&str`.
/// If the `&str` does not contain carriage-returns do not use this.
/// Use [`whitespace-sifter::sift(...)`](./fn.sift.html) instead.
pub fn sift_with_carriage_return(input: &str) -> String {
let mut out: String = String::with_capacity(input.len());
for val in input.trim().split("\r\n") {
let val: &str = val.trim();
if val.is_empty() {
continue;
}
let mut is_last_whitespace: bool = false;
let mut is_last_carriage_return: bool = false;
for c in val.trim().chars() {
let is_carriage_return: bool = c == '\r';
let is_newline: bool = c == '\n';
let is_whitespace: bool = c.is_ascii_whitespace();
if is_newline && is_last_carriage_return {
out.push(c);
is_last_carriage_return = false;
continue;
}
if is_whitespace && is_last_whitespace {
continue;
}
out.push(c);
is_last_carriage_return = is_carriage_return;
is_last_whitespace = is_whitespace;
}
out.push_str("\r\n");
}
out.remove(out.len() - 1);
out.remove(out.len() - 1);
out
}
}
#[cfg(test)]
mod tests {
use super::{sift, sift_with_carriage_return};
#[test]
fn test_sift() {
let input: &str = &format!(
"{}\n\n{}\n\n{}\n\n\n",
"This\u{0020}\u{0020}is\u{0020}\u{0020}\u{0020}a\u{0020}\u{0020}sentence...",
"With\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}some\u{0020}\u{0020}duplicate...",
"Whitespaces."
);
assert_eq!(
&sift(input),
"This is a sentence...\nWith some duplicate...\nWhitespaces."
);
}
#[test]
fn test_sift_with_carriage_return() {
let input: &str = &format!(
"{}\r\n\r\n{}\r\n\r\n{}\r\n\r\n\r\n",
"This\u{0020}\u{0020}is\u{0020}\u{0020}\u{0020}a\u{0020}\u{0020}sentence...",
"With\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}some\u{0020}\u{0020}duplicate...",
"Whitespaces."
);
assert_eq!(
&sift_with_carriage_return(input),
"This is a sentence...\r\nWith some duplicate...\r\nWhitespaces."
);
}
#[test]
fn test_sift_preserved() {
let input: &str = &format!(
"{}\n\n{}\n\n{}\n\n\n",
"This. \n\nis. \n\na. \n\nsentence... \n\n",
"With. \n\nsome. \n\nduplicate... \n\n",
"Whitespaces. \n\n"
);
assert_eq!(
&super::preserve_newline::sift(input),
"This.\nis.\na.\nsentence...\nWith.\nsome.\nduplicate...\nWhitespaces."
);
}
#[test]
fn test_sift_with_carriage_return_preserved() {
let input: &str = &format!(
"{}\r\n\r\n{}\r\n\r\n{}\r\n\r\n\r\n",
"This. \r\n\r\nis. \r\n\r\na. \r\n\r\nsentence... \r\n\r\n",
"With. \r\n\r\nsome. \r\n\r\nduplicate... \r\n\r\n",
"Whitespaces. \r\n\r\n"
);
assert_eq!(
&super::preserve_newline::sift_with_carriage_return(input),
"This.\r\nis.\r\na.\r\nsentence...\r\nWith.\r\nsome.\r\nduplicate...\r\nWhitespaces."
);
}
}