-
Notifications
You must be signed in to change notification settings - Fork 498
/
unindent.rs
134 lines (113 loc) · 2.85 KB
/
unindent.rs
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
#[must_use]
pub fn unindent(text: &str) -> String {
// find line start and end indices
let mut lines = Vec::new();
let mut start = 0;
for (i, c) in text.char_indices() {
if c == '\n' || i == text.len() - c.len_utf8() {
let end = i + c.len_utf8();
lines.push(&text[start..end]);
start = end;
}
}
let common_indentation = lines
.iter()
.filter(|line| !blank(line))
.copied()
.map(indentation)
.fold(
None,
|common_indentation, line_indentation| match common_indentation {
Some(common_indentation) => Some(common(common_indentation, line_indentation)),
None => Some(line_indentation),
},
)
.unwrap_or("");
let mut replacements = Vec::with_capacity(lines.len());
for (i, line) in lines.iter().enumerate() {
let blank = blank(line);
let first = i == 0;
let last = i == lines.len() - 1;
let replacement = match (blank, first, last) {
(true, false, false) => "\n",
(true, _, _) => "",
(false, _, _) => &line[common_indentation.len()..],
};
replacements.push(replacement);
}
replacements.into_iter().collect()
}
fn indentation(line: &str) -> &str {
let i = line
.char_indices()
.take_while(|(_, c)| matches!(c, ' ' | '\t'))
.map(|(i, _)| i + 1)
.last()
.unwrap_or(0);
&line[..i]
}
fn blank(line: &str) -> bool {
line.chars().all(|c| matches!(c, ' ' | '\t' | '\r' | '\n'))
}
fn common<'s>(a: &'s str, b: &'s str) -> &'s str {
let i = a
.char_indices()
.zip(b.chars())
.take_while(|((_, ac), bc)| ac == bc)
.map(|((i, c), _)| i + c.len_utf8())
.last()
.unwrap_or(0);
&a[0..i]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unindents() {
assert_eq!(unindent("foo"), "foo");
assert_eq!(unindent("foo\nbar\nbaz\n"), "foo\nbar\nbaz\n");
assert_eq!(unindent(""), "");
assert_eq!(unindent(" foo\n bar"), "foo\nbar");
assert_eq!(unindent(" foo\n bar\n\n"), "foo\nbar\n");
assert_eq!(
unindent(
"
hello
bar
"
),
"hello\nbar\n"
);
assert_eq!(unindent("hello\n bar\n foo"), "hello\n bar\n foo");
assert_eq!(
unindent(
"
hello
bar
"
),
"\nhello\nbar\n\n"
);
}
#[test]
fn indentations() {
assert_eq!(indentation(""), "");
assert_eq!(indentation("foo"), "");
assert_eq!(indentation(" foo"), " ");
assert_eq!(indentation("\t\tfoo"), "\t\t");
assert_eq!(indentation("\t \t foo"), "\t \t ");
}
#[test]
fn blanks() {
assert!(blank(" \n"));
assert!(!blank(" foo\n"));
assert!(blank("\t\t\n"));
}
#[test]
fn commons() {
assert_eq!(common("foo", "foobar"), "foo");
assert_eq!(common("foo", "bar"), "");
assert_eq!(common("", ""), "");
assert_eq!(common("", "bar"), "");
}
}