Skip to main content

normalie/impls/
metaspace.rs

1//! Metaspace. Spaces become `▁`. Whitespace-dropping is `MetaspaceWords`.
2
3use crate::formula::{Formula, Window};
4use crate::impls::edit::expand_spaces;
5
6const MARK: &str = "▁";
7const MARK_B: [u8; 3] = [0xE2, 0x96, 0x81];
8
9pub struct Metaspace;
10pub struct MetaspaceFirst;
11pub struct MetaspaceNever;
12pub struct MetaspaceWords;
13
14impl Metaspace {
15    /// `always` prepends the mark to every segment, so the caller's
16    /// `at_start` is irrelevant. Drive with a true initial start: the formula's
17    /// `at_start` gate then makes the first window rewrite (prepend) instead
18    /// of copy. Later windows still see `at_start == false` from the kernel.
19    pub fn write(text: &str, _at_start: bool, out: &mut String) -> bool {
20        <Self as Formula>::drive(&Self, text, true, out)
21    }
22}
23
24impl MetaspaceFirst {
25    pub fn write(text: &str, at_start: bool, out: &mut String) -> bool {
26        <Self as Formula>::drive(&Self, text, at_start, out)
27    }
28}
29
30impl MetaspaceNever {
31    pub fn write(text: &str, at_start: bool, out: &mut String) -> bool {
32        <Self as Formula>::drive(&Self, text, at_start, out)
33    }
34}
35
36impl MetaspaceWords {
37    pub fn write(text: &str, at_start: bool, out: &mut String) -> bool {
38        <Self as Formula>::drive(&Self, text, at_start, out)
39    }
40}
41
42impl Formula for Metaspace {
43    fn unchanged(&self, _text: &str, _at_start: bool) -> Option<usize> {
44        None
45    }
46
47    fn write_scalar(&self, text: &str, at_start: bool, out: &mut String) -> usize {
48        expand_spaces(text, at_start && needs_prepend(text.as_bytes()), out);
49        text.len()
50    }
51
52    #[inline(always)]
53    fn try_window<const ISA: u8>(&self, bytes: &[u8], at_start: bool) -> Option<Window> {
54        let _ = ISA;
55        if at_start && needs_prepend(bytes) {
56            return None;
57        }
58        copy_until_space(bytes)
59    }
60}
61
62impl Formula for MetaspaceFirst {
63    fn unchanged(&self, _text: &str, _at_start: bool) -> Option<usize> {
64        None
65    }
66
67    fn write_scalar(&self, text: &str, at_start: bool, out: &mut String) -> usize {
68        expand_spaces(text, at_start && needs_prepend(text.as_bytes()), out);
69        text.len()
70    }
71
72    #[inline(always)]
73    fn try_window<const ISA: u8>(&self, bytes: &[u8], at_start: bool) -> Option<Window> {
74        let _ = ISA;
75        if at_start && needs_prepend(bytes) {
76            return None;
77        }
78        copy_until_space(bytes)
79    }
80}
81
82impl Formula for MetaspaceNever {
83    fn unchanged(&self, _text: &str, _at_start: bool) -> Option<usize> {
84        None
85    }
86
87    fn write_scalar(&self, text: &str, _at_start: bool, out: &mut String) -> usize {
88        expand_spaces(text, false, out);
89        text.len()
90    }
91
92    #[inline(always)]
93    fn try_window<const ISA: u8>(&self, bytes: &[u8], _at_start: bool) -> Option<Window> {
94        let _ = ISA;
95        copy_until_space(bytes)
96    }
97}
98
99impl Formula for MetaspaceWords {
100    fn drive(&self, text: &str, _at_start: bool, out: &mut String) -> bool {
101        if text.is_empty() {
102            return false;
103        }
104        let bytes = text.as_bytes();
105        out.reserve(text.len() + text.len() / 8 + MARK.len());
106        let mut i = 0;
107        let mut in_word = false;
108        let mut saw_ws = false;
109        while i < bytes.len() {
110            let b = bytes[i];
111            if b < 0x80 {
112                if is_ascii_ws(b) {
113                    saw_ws = true;
114                    in_word = false;
115                    i += 1;
116                    continue;
117                }
118                if !in_word {
119                    out.push_str(MARK);
120                    in_word = true;
121                }
122                let start = i;
123                i += 1;
124                while i < bytes.len() && bytes[i] < 0x80 && !is_ascii_ws(bytes[i]) {
125                    i += 1;
126                }
127                out.push_str(&text[start..i]);
128                continue;
129            }
130            let c = text[i..].chars().next().unwrap();
131            let w = c.len_utf8();
132            if c.is_whitespace() {
133                saw_ws = true;
134                in_word = false;
135                i += w;
136                continue;
137            }
138            if !in_word {
139                if !text[i..].starts_with(MARK) {
140                    out.push_str(MARK);
141                }
142                in_word = true;
143            }
144            out.push_str(&text[i..i + w]);
145            i += w;
146        }
147        in_word || saw_ws
148    }
149
150    fn unchanged(&self, text: &str, _at_start: bool) -> Option<usize> {
151        let _ = text;
152        None
153    }
154
155    fn write_scalar(&self, text: &str, _at_start: bool, _out: &mut String) -> usize {
156        text.len().max(1)
157    }
158}
159
160fn is_ascii_ws(b: u8) -> bool {
161    matches!(b, b' ' | b'\t' | b'\n' | b'\r' | 0x0B | 0x0C)
162}
163
164fn needs_prepend(bytes: &[u8]) -> bool {
165    !bytes.is_empty() && bytes[0] != b' ' && !bytes.starts_with(&MARK_B)
166}
167
168fn copy_until_space(bytes: &[u8]) -> Option<Window> {
169    match memchr::memchr(b' ', bytes) {
170        None if !bytes.is_empty() => Some(Window::Copy(bytes.len())),
171        Some(0) | None => None,
172        Some(k) => Some(Window::Copy(k)),
173    }
174}