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
#![allow(unsafe_code)]
use crate::__ctfe::StrBuf;
pub struct Squish<T>(pub T);
impl Squish<&'_ str> {
pub const fn output_len(&self) -> usize {
let mut len = 0;
macro_rules! push {
($x: expr) => {
len += 1;
};
}
let bytes = self.0.as_bytes();
let mut i = 0;
while i < bytes.len() {
let x = bytes[i];
if x.is_ascii_whitespace() {
let mut j = i + 1;
while j < bytes.len() {
if bytes[j].is_ascii_whitespace() {
j += 1;
} else {
break;
}
}
if !(i == 0 || j == bytes.len()) {
push!(b' ');
}
i = j;
continue;
}
push!(x);
i += 1;
}
len
}
pub const fn const_eval<const N: usize>(&self) -> StrBuf<N> {
let mut buf = [0; N];
let mut pos = 0;
macro_rules! push {
($x: expr) => {
buf[pos] = $x;
pos += 1;
};
}
let bytes = self.0.as_bytes();
let mut i = 0;
while i < bytes.len() {
let x = bytes[i];
if x.is_ascii_whitespace() {
let mut j = i + 1;
while j < bytes.len() {
if bytes[j].is_ascii_whitespace() {
j += 1;
} else {
break;
}
}
if !(i == 0 || j == bytes.len()) {
push!(b' ');
}
i = j;
continue;
}
push!(x);
i += 1;
}
assert!(pos == N);
unsafe { StrBuf::new_unchecked(buf) }
}
}
/// Splits the string by ASCII whitespaces, and then joins the parts with a single space.
///
/// # Examples
///
/// ```rust
/// use const_str::squish;
///
/// assert_eq!(squish!(" SQUISH \t THAT \t CAT! "), "SQUISH THAT CAT!");
///
/// const SQL: &str = squish!(
/// "SELECT
/// name,
/// created_at,
/// updated_at
/// FROM users
/// WHERE id = ?"
/// );
/// assert_eq!(SQL, "SELECT name, created_at, updated_at FROM users WHERE id = ?");
///
///
/// ```
///
#[macro_export]
macro_rules! squish {
($s:expr) => {{
const INPUT: &str = $s;
const N: usize = $crate::__ctfe::Squish(INPUT).output_len();
const OUTPUT: $crate::__ctfe::StrBuf<N> = $crate::__ctfe::Squish(INPUT).const_eval();
OUTPUT.as_str()
}};
}
#[cfg(test)]
mod tessts {
fn join<'a>(iter: impl IntoIterator<Item = &'a str>, sep: &str) -> String {
let mut ans = String::new();
let mut iter = iter.into_iter();
match iter.next() {
None => return ans,
Some(first) => ans.push_str(first),
}
for part in iter {
ans.push_str(sep);
ans.push_str(part);
}
ans
}
fn std_squish(input: &str) -> String {
join(input.split_ascii_whitespace(), " ")
}
#[test]
fn test_squish() {
macro_rules! testcase {
($s:expr) => {{
const OUTPUT: &str = squish!($s);
let expected = std_squish($s);
assert_eq!(OUTPUT, expected);
}};
}
testcase!("");
testcase!(" ");
testcase!(" t");
testcase!("t ");
testcase!(" t ");
testcase!(" t t");
testcase!(" SQUISH \t THAT \t CAT ");
testcase!(
"
All you need to know is to \t
SQUISH THAT CAT! \
"
);
testcase!(concat!("We\n", "always\n", "SQUISH\n", "THAT\n", "CAT."));
testcase!(
"SELECT
name,
created_at,
updated_at
FROM users
WHERE id = ?"
);
}
}