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
331
332
333
334
335
336
337
338
pub fn rejustify(src: &str) -> String {
let s = &src[1..];
let mut sb = String::new();
for l in s.lines() {
let tabless = &l[4..];
sb.push_str(tabless);
if tabless.len() != 0 {
sb.push_str("\n");
}
}
let newlen = sb.len()-1;
sb.truncate(newlen);
sb
}
pub fn slice(src: &str, (begin, end): (usize, usize)) -> &str {
&src[begin..end]
}
#[derive(Clone,Copy)]
enum State {
StateCode,
StateComment,
StateCommentBlock,
StateString,
StateChar,
StateFinished
}
#[derive(Clone,Copy)]
pub struct CodeIndicesIter<'a> {
src: &'a str,
pos: usize,
state: State
}
impl<'a> Iterator for CodeIndicesIter<'a> {
type Item = (usize, usize);
#[inline]
fn next(&mut self) -> Option<(usize, usize)> {
match self.state {
State::StateCode => Some(self.code()),
State::StateComment => Some(self.comment()),
State::StateCommentBlock => Some(self.comment_block()),
State::StateString => Some(self.string()),
State::StateChar => Some(self.char()),
State::StateFinished => None
}
}
}
impl<'a> CodeIndicesIter<'a> {
fn code(&mut self) -> (usize, usize) {
let mut pos = self.pos;
let start = match self.state {
State::StateString |
State::StateChar => { pos-1 },
_ => { pos }
};
let src_bytes = self.src.as_bytes();
for &b in &src_bytes[pos..] {
pos += 1;
match b {
b'/' if src_bytes.len() > pos => match src_bytes[pos] {
b'/' => {
self.state = State::StateComment;
self.pos = pos + 1;
return (start, pos-1);
},
b'*' => {
self.state = State::StateCommentBlock;
self.pos = pos + 1;
return (start, pos-1);
},
_ => {}
},
b'"' => {
self.state = State::StateString;
self.pos = pos;
return (start, pos);
},
b'\'' => {
if src_bytes.len() > pos + 1 &&
(src_bytes[pos] == b'\\' ||
src_bytes[pos+1] == b'\'') {
self.state = State::StateChar;
self.pos = pos;
return (start, pos);
}
},
_ => {}
}
}
self.state = State::StateFinished;
(start, self.src.len())
}
fn comment(&mut self) -> (usize, usize) {
let mut pos = self.pos;
let src_bytes = self.src.as_bytes();
for &b in &src_bytes[pos..] {
pos += 1;
if b == b'\n' {
if pos + 2 <= src_bytes.len() && &src_bytes[pos..pos+2] == &[b'/', b'/'] {
continue;
}
break;
}
}
self.pos = pos;
self.code()
}
fn comment_block(&mut self) -> (usize, usize) {
let mut nesting_level = 0usize;
let mut prev = b' ';
let mut pos = self.pos;
for &b in &self.src.as_bytes()[pos..] {
pos += 1;
match b {
b'/' if prev == b'*' => {
if nesting_level == 0 {
break;
} else {
nesting_level -= 1;
}
},
b'*' if prev == b'/' => {
nesting_level += 1;
},
_ => { prev = b; }
}
}
self.pos = pos;
self.code()
}
fn string(&mut self) -> (usize, usize) {
let src_bytes = self.src.as_bytes();
let mut pos = self.pos;
if pos > 1 && src_bytes[pos-2] == b'r' {
match src_bytes[pos..].iter().position(|&b| b == b'"') {
Some(p) => pos += p+1,
None => pos = src_bytes.len()
}
} else {
let mut is_not_escaped = true;
for &b in &src_bytes[pos..] {
pos += 1;
match b {
b'"' if is_not_escaped => { break; },
b'\\' => { is_not_escaped = !is_not_escaped; },
_ => { is_not_escaped = true; }
}
}
}
self.pos = pos;
self.code()
}
fn char(&mut self) -> (usize, usize) {
let mut is_not_escaped = true;
let mut pos = self.pos;
for &b in &self.src.as_bytes()[pos..] {
pos += 1;
match b {
b'\'' if is_not_escaped => { break; },
b'\\' => { is_not_escaped = !is_not_escaped; },
_ => { is_not_escaped = true; }
}
}
self.pos = pos;
self.code()
}
}
pub fn code_chunks(src: &str) -> CodeIndicesIter {
CodeIndicesIter { src: src, state: State::StateCode, pos: 0 }
}
#[test]
fn removes_a_comment() {
let src = &rejustify("
this is some code // this is a comment
some more code
");
let mut it = code_chunks(src);
assert_eq!("this is some code ", slice(src, it.next().unwrap()));
assert_eq!("some more code", slice(src, it.next().unwrap()));
}
#[test]
fn removes_consecutive_comments() {
let src = &rejustify("
this is some code // this is a comment
// this is more comment
// another comment
some more code
");
let mut it = code_chunks(src);
assert_eq!("this is some code ", slice(src, it.next().unwrap()));
assert_eq!("some more code", slice(src, it.next().unwrap()));
}
#[test]
fn removes_string_contents() {
let src = &rejustify("
this is some code \"this is a string\" more code
");
let mut it = code_chunks(src);
assert_eq!("this is some code \"", slice(src, it.next().unwrap()));
assert_eq!("\" more code", slice(src, it.next().unwrap()));
}
#[test]
fn removes_char_contents() {
let src = &rejustify("
this is some code \'\"\' more code \'\\x00\' and \'\\\'\' that\'s it
");
let mut it = code_chunks(src);
assert_eq!("this is some code \'", slice(src, it.next().unwrap()));
assert_eq!("\' more code \'", slice(src, it.next().unwrap()));
assert_eq!("\' and \'", slice(src, it.next().unwrap()));
assert_eq!("\' that\'s it", slice(src, it.next().unwrap()));
}
#[test]
fn removes_string_contents_with_a_comment_in_it() {
let src = &rejustify("
this is some code \"string with a // fake comment \" more code
");
let mut it = code_chunks(src);
assert_eq!("this is some code \"", slice(src, it.next().unwrap()));
assert_eq!("\" more code", slice(src, it.next().unwrap()));
}
#[test]
fn removes_a_comment_with_a_dbl_quote_in_it() {
let src = &rejustify("
this is some code // comment with \" double quote
some more code
");
let mut it = code_chunks(src);
assert_eq!("this is some code ", slice(src, it.next().unwrap()));
assert_eq!("some more code", slice(src, it.next().unwrap()));
}
#[test]
fn removes_multiline_comment() {
let src = &rejustify("
this is some code /* this is a
\"multiline\" comment */some more code
");
let mut it = code_chunks(src);
assert_eq!("this is some code ", slice(src, it.next().unwrap()));
assert_eq!("some more code", slice(src, it.next().unwrap()));
}
#[test]
fn handles_nesting_of_block_comments() {
let src = &rejustify("
this is some code /* nested /* block */ comment */ some more code
");
let mut it = code_chunks(src);
assert_eq!("this is some code ", slice(src, it.next().unwrap()));
assert_eq!(" some more code", slice(src, it.next().unwrap()));
}
#[test]
fn removes_string_with_escaped_dblquote_in_it() {
let src = &rejustify("
this is some code \"string with a \\\" escaped dblquote fake comment \" more code
");
let mut it = code_chunks(src);
assert_eq!("this is some code \"", slice(src, it.next().unwrap()));
assert_eq!("\" more code", slice(src, it.next().unwrap()));
}
#[test]
fn removes_raw_string_with_dangling_escape_in_it() {
let src = &rejustify("
this is some code br\" escaped dblquote raw string \\\" more code
");
let mut it = code_chunks(src);
assert_eq!("this is some code br\"", slice(src, it.next().unwrap()));
assert_eq!("\" more code", slice(src, it.next().unwrap()));
}
#[test]
fn removes_string_with_escaped_slash_before_dblquote_in_it() {
let src = &rejustify("
this is some code \"string with an escaped slash, so dbl quote does end the string after all \\\\\" more code
");
let mut it = code_chunks(src);
assert_eq!("this is some code \"", slice(src, it.next().unwrap()));
assert_eq!("\" more code", slice(src, it.next().unwrap()));
}
#[test]
fn handles_tricky_bit_from_str_rs() {
let src = &rejustify("
before(\"\\\\\'\\\\\\\"\\\\\\\\\");
more_code(\" skip me \")
");
for (start, end) in code_chunks(src) {
println!("BLOB |{}|", &src[start..end]);
if (&src[start..end]).contains("skip me") {
panic!("{}", &src[start..end]);
}
}
}