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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! Core logic for `git stripspace`.
//!
//! Provides whitespace stripping and comment-line prefixing transformations
//! that match Git's behaviour:
//!
//! - Strip trailing whitespace from every line.
//! - Collapse multiple consecutive blank lines into one.
//! - Remove leading and trailing blank lines.
//! - Ensure non-empty output ends with a newline.
//! - Optionally strip lines that start with a comment prefix string.
//! - Optionally prefix every line with the comment character.
/// Processing mode for [`process`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Mode {
/// Strip trailing whitespace and collapse blank lines.
Default,
/// Same as [`Mode::Default`] but also remove comment lines.
///
/// A comment line is any line whose first bytes match `comment_prefix`
/// (e.g. `"#"`).
StripComments(String),
/// Prefix every input line with the comment character.
///
/// Non-empty lines that do not start with a tab get `comment_prefix + " "`;
/// empty lines and tab-starting lines get just `comment_prefix`. This
/// avoids the `SP-HT` sequence (`# \t…`) that Git also avoids.
CommentLines(String),
}
/// Process `input` bytes according to `mode` and return the result.
///
/// # Parameters
///
/// - `input`: raw bytes read from stdin.
/// - `mode`: controls whether to strip, strip-and-remove-comments, or add comments.
///
/// # Returns
///
/// A `Vec<u8>` with the transformed content. Returns an empty vector when the
/// input consists entirely of whitespace (in strip modes) or is itself empty.
///
/// # Examples
///
/// ```
/// use grit_lib::stripspace::{process, Mode};
///
/// let out = process(b"hello \n\n\nworld\n", &Mode::Default);
/// assert_eq!(out, b"hello\n\nworld\n");
///
/// let out = process(b"# comment\ntext\n", &Mode::StripComments("#".into()));
/// assert_eq!(out, b"text\n");
///
/// let out = process(b"foo\n\nbar\n", &Mode::CommentLines("#".into()));
/// assert_eq!(out, b"# foo\n#\n# bar\n");
/// ```
#[must_use]
pub fn process(input: &[u8], mode: &Mode) -> Vec<u8> {
match mode {
Mode::Default => strip(input, None),
Mode::StripComments(prefix) => strip(input, Some(prefix.as_str())),
Mode::CommentLines(prefix) => comment_lines(input, prefix.as_str()),
}
}
/// Returns a copy of `line` with trailing space/tab bytes removed.
///
/// The line is expected to end with `\n`; the newline is preserved (not
/// considered trailing whitespace).
fn strip_trailing(line: &[u8]) -> Vec<u8> {
let nl_pos = line.iter().rposition(|&b| b == b'\n');
let content_end = nl_pos.unwrap_or(line.len());
let content = &line[..content_end];
let trimmed_end = content
.iter()
.rposition(|&b| b != b' ' && b != b'\t')
.map(|p| p + 1)
.unwrap_or(0);
let mut result = content[..trimmed_end].to_vec();
if nl_pos.is_some() {
result.push(b'\n');
}
result
}
/// Core strip implementation shared by [`Mode::Default`] and
/// [`Mode::StripComments`].
///
/// When `comment_prefix` is `Some(s)`, lines whose bytes begin with `s` are
/// discarded before any other processing.
fn strip(input: &[u8], comment_prefix: Option<&str>) -> Vec<u8> {
if input.is_empty() {
return Vec::new();
}
// Ensure the data ends with a newline so every line is terminated.
let owned;
let data: &[u8] = if input.last() != Some(&b'\n') {
owned = {
let mut v = input.to_vec();
v.push(b'\n');
v
};
&owned
} else {
input
};
let mut result: Vec<u8> = Vec::new();
let mut pending_blank: usize = 0;
let mut saw_content = false;
let mut pos = 0;
while pos < data.len() {
let next = data[pos..]
.iter()
.position(|&b| b == b'\n')
.map(|p| pos + p + 1)
.unwrap_or(data.len());
let raw_line = &data[pos..next];
pos = next;
// Discard comment lines when requested.
if let Some(prefix) = comment_prefix {
if raw_line.starts_with(prefix.as_bytes()) {
continue;
}
}
// Strip trailing whitespace; the result ends with '\n'.
let stripped = strip_trailing(raw_line);
// A line that reduces to just '\n' is blank.
if stripped == [b'\n'] {
if saw_content {
pending_blank += 1;
}
// Skip leading blank lines (before any real content).
continue;
}
// Non-blank line: flush at most one pending blank, then emit the line.
if saw_content && pending_blank > 0 {
result.push(b'\n');
}
pending_blank = 0;
saw_content = true;
result.extend_from_slice(&stripped);
}
result
}
/// Prefix every line of `input` with the comment string.
///
/// - Non-empty lines that do not start with `\t` get `comment_prefix + " "`.
/// - Empty lines and lines starting with `\t` get just `comment_prefix`.
///
/// This mirrors `strbuf_add_commented_lines` in Git, which avoids the
/// `SP-HT` sequence `"# \t…"`.
fn comment_lines(input: &[u8], comment_prefix: &str) -> Vec<u8> {
if input.is_empty() {
return Vec::new();
}
// Ensure the data ends with a newline.
let owned;
let data: &[u8] = if input.last() != Some(&b'\n') {
owned = {
let mut v = input.to_vec();
v.push(b'\n');
v
};
&owned
} else {
input
};
let prefix_bytes = comment_prefix.as_bytes();
let mut result: Vec<u8> = Vec::new();
let mut pos = 0;
while pos < data.len() {
let next = data[pos..]
.iter()
.position(|&b| b == b'\n')
.map(|p| pos + p + 1)
.unwrap_or(data.len());
let raw_line = &data[pos..next];
pos = next;
// Separate content from the terminating newline.
let nl_pos = raw_line.iter().rposition(|&b| b == b'\n');
let content_end = nl_pos.unwrap_or(raw_line.len());
let content = &raw_line[..content_end];
// Prepend comment prefix; add a space unless the content is empty or
// starts with a tab (to avoid the SP-HT sequence).
result.extend_from_slice(prefix_bytes);
if !content.is_empty() && content[0] != b'\t' {
result.push(b' ');
}
result.extend_from_slice(content);
result.push(b'\n');
}
result
}
#[cfg(test)]
mod tests {
use super::*;
// ── Mode::Default ────────────────────────────────────────────────────────
#[test]
fn default_strips_trailing_whitespace() {
let out = process(b"hello \n", &Mode::Default);
assert_eq!(out, b"hello\n");
}
#[test]
fn default_collapses_consecutive_blank_lines() {
let out = process(b"a\n\n\n\nb\n", &Mode::Default);
assert_eq!(out, b"a\n\nb\n");
}
#[test]
fn default_removes_leading_blank_lines() {
let out = process(b"\n\n\ntext\n", &Mode::Default);
assert_eq!(out, b"text\n");
}
#[test]
fn default_removes_trailing_blank_lines() {
let out = process(b"text\n\n\n", &Mode::Default);
assert_eq!(out, b"text\n");
}
#[test]
fn default_all_whitespace_yields_empty() {
assert_eq!(process(b" \n \n\n", &Mode::Default), b"");
assert_eq!(process(b"\n", &Mode::Default), b"");
assert_eq!(process(b"", &Mode::Default), b"");
}
#[test]
fn default_adds_trailing_newline_when_missing() {
let out = process(b"text", &Mode::Default);
assert_eq!(out, b"text\n");
}
#[test]
fn default_preserves_leading_spaces_on_line() {
let out = process(b" indented\n", &Mode::Default);
assert_eq!(out, b" indented\n");
}
#[test]
fn default_blank_lines_between_whitespace_only_lines() {
// Lines with only spaces count as blank.
let out = process(b"a\n \n \nb\n", &Mode::Default);
assert_eq!(out, b"a\n\nb\n");
}
// ── Mode::StripComments ──────────────────────────────────────────────────
#[test]
fn strip_comments_removes_hash_lines() {
// Comment lines are simply removed; no blank is inserted in their place.
let out = process(b"text\n# comment\nmore\n", &Mode::StripComments("#".into()));
assert_eq!(out, b"text\nmore\n");
}
#[test]
fn strip_comments_keeps_non_comment_lines() {
let out = process(b"# comment\n", &Mode::StripComments("#".into()));
assert_eq!(out, b"");
}
#[test]
fn strip_comments_multichar_prefix() {
let out = process(
b"// removed\nnormal line\n",
&Mode::StripComments("//".into()),
);
assert_eq!(out, b"normal line\n");
}
// ── Mode::CommentLines ───────────────────────────────────────────────────
#[test]
fn comment_lines_prefixes_non_empty() {
let out = process(b"foo\n", &Mode::CommentLines("#".into()));
assert_eq!(out, b"# foo\n");
}
#[test]
fn comment_lines_empty_line_gets_bare_prefix() {
let out = process(b"\n", &Mode::CommentLines("#".into()));
assert_eq!(out, b"#\n");
}
#[test]
fn comment_lines_tab_line_avoids_sp_ht() {
// "\tone" → "#\tone", not "# \tone"
let out = process(b"\tone\n", &Mode::CommentLines("#".into()));
assert_eq!(out, b"#\tone\n");
}
#[test]
fn comment_lines_adds_trailing_newline() {
let out = process(b"foo", &Mode::CommentLines("#".into()));
assert_eq!(out, b"# foo\n");
}
#[test]
fn comment_lines_empty_input_yields_empty() {
let out = process(b"", &Mode::CommentLines("#".into()));
assert_eq!(out, b"");
}
#[test]
fn comment_lines_multiple_lines() {
let out = process(b"\tone\n\ntwo\n", &Mode::CommentLines("#".into()));
assert_eq!(out, b"#\tone\n#\n# two\n");
}
}