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
use super::Span;
impl Span<'_> {
/// Return a [`Span`] that is the same as the source, but with any trailing
/// space or tab characters removed.
pub(crate) fn trim_trailing_whitespace(&self) -> Self {
// IMPORTANT: We don't use `str::trim_end` because Rust's definition of
// whitespace doesn't match Asciidoc's definition.
let new_len = self
.data
.trim_end_matches([' ', '\t', '\n', '\r', '\x0C', '\x0B'])
.len();
self.slice(0..new_len)
}
/// Return a [`Span`] that is the same as the source, but a single trailing
/// line-ending (if found) removed.
pub(crate) fn trim_trailing_line_end(&self) -> Self {
if self.ends_with("\r\n") {
self.slice(0..self.len() - 2)
} else if self.ends_with('\n') {
self.slice(0..self.len() - 1)
} else {
*self
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
mod trim_trailing_whitespace {
use crate::tests::prelude::*;
#[test]
fn empty_source() {
let s = crate::Span::default().trim_trailing_whitespace();
assert_eq!(
s,
Span {
data: "",
line: 1,
col: 1,
offset: 0,
}
);
}
#[test]
fn nothing_to_trim() {
let s = crate::Span::new("foo").trim_trailing_whitespace();
assert_eq!(
s,
Span {
data: "foo",
line: 1,
col: 1,
offset: 0,
}
);
}
#[test]
fn space_in_middle() {
let s = crate::Span::new("foo bar").trim_trailing_whitespace();
assert_eq!(
s,
Span {
data: "foo bar",
line: 1,
col: 1,
offset: 0,
}
);
}
#[test]
fn trailing_space() {
let s = crate::Span::new("foo ").trim_trailing_whitespace();
assert_eq!(
s,
Span {
data: "foo",
line: 1,
col: 1,
offset: 0,
}
);
}
#[test]
fn trailing_newlines() {
let s = crate::Span::new("foo\n\n").trim_trailing_whitespace();
assert_eq!(
s,
Span {
data: "foo",
line: 1,
col: 1,
offset: 0,
}
);
}
}
mod trim_trailing_line_end {
use crate::tests::prelude::*;
#[test]
fn empty_source() {
let s = crate::Span::default().trim_trailing_line_end();
assert_eq!(
s,
Span {
data: "",
line: 1,
col: 1,
offset: 0,
}
);
}
#[test]
fn nothing_to_trim() {
let s = crate::Span::new("foo").trim_trailing_line_end();
assert_eq!(
s,
Span {
data: "foo",
line: 1,
col: 1,
offset: 0,
}
);
}
#[test]
fn space_in_middle() {
let s = crate::Span::new("foo bar").trim_trailing_line_end();
assert_eq!(
s,
Span {
data: "foo bar",
line: 1,
col: 1,
offset: 0,
}
);
}
#[test]
fn trailing_space() {
let s = crate::Span::new("foo ").trim_trailing_line_end();
assert_eq!(
s,
Span {
data: "foo ",
line: 1,
col: 1,
offset: 0,
}
);
}
#[test]
fn trailing_newlines() {
let s = crate::Span::new("foo\n\n").trim_trailing_line_end();
assert_eq!(
s,
Span {
data: "foo\n",
line: 1,
col: 1,
offset: 0,
}
);
}
#[test]
fn trailing_windows_newlines() {
let s = crate::Span::new("foo\n\r\n").trim_trailing_line_end();
assert_eq!(
s,
Span {
data: "foo\n",
line: 1,
col: 1,
offset: 0,
}
);
}
}
}