Skip to content

Commit 06f26c7

Browse files
committed
added separate AnyRecursiveSequence token
I think this is probably a more straightforward implementation. Also conformed to whitespace conventions.
1 parent 6984d92 commit 06f26c7

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/lib.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use std::io::fs::{mod, PathExtensions};
3030
use std::path::is_sep;
3131
use std::string::String;
3232

33-
use PatternToken::{Char, AnyChar, AnySequence, AnyWithin, AnyExcept};
33+
use PatternToken::{Char, AnyChar, AnySequence, AnyRecursiveSequence, AnyWithin, AnyExcept};
3434
use CharSpecifier::{SingleChar, CharRange};
3535
use MatchResult::{Match, SubPatternDoesntMatch, EntirePatternDoesntMatch};
3636

@@ -192,7 +192,8 @@ pub struct Pattern {
192192
enum PatternToken {
193193
Char(char),
194194
AnyChar,
195-
AnySequence(bool),
195+
AnySequence,
196+
AnyRecursiveSequence,
196197
AnyWithin(Vec<CharSpecifier> ),
197198
AnyExcept(Vec<CharSpecifier> )
198199
}
@@ -248,23 +249,23 @@ impl Pattern {
248249
i += 1;
249250
}
250251
'*' => {
251-
let old = i;
252+
let old = i;
252253

253-
while i < chars.len() && chars[i] == '*' {
254-
i += 1;
255-
}
254+
while i < chars.len() && chars[i] == '*' {
255+
i += 1;
256+
}
256257

257-
let count = i - old;
258+
let count = i - old;
258259

259-
if count > 2 {
260-
for _ in range(0u, count) {
261-
tokens.push(Char('*'));
260+
if count > 2 {
261+
for _ in range(0u, count) {
262+
tokens.push(Char('*'));
263+
}
264+
} else if count == 2 {
265+
tokens.push(AnyRecursiveSequence);
266+
} else {
267+
tokens.push(AnySequence);
262268
}
263-
} else if count == 2 {
264-
tokens.push(AnySequence(true));
265-
} else {
266-
tokens.push(AnySequence(false));
267-
}
268269
}
269270
'[' => {
270271

@@ -382,7 +383,7 @@ impl Pattern {
382383

383384
for (ti, token) in self.tokens.slice_from(i).iter().enumerate() {
384385
match *token {
385-
AnySequence(recursive) => {
386+
AnySequence | AnyRecursiveSequence => {
386387
loop {
387388
match self.matches_from(prev_char.get(), file, i + ti + 1, options) {
388389
SubPatternDoesntMatch => (), // keep trying
@@ -394,9 +395,12 @@ impl Pattern {
394395
Some(pair) => pair
395396
};
396397

397-
if !recursive && require_literal(c) {
398-
return SubPatternDoesntMatch;
398+
if let AnySequence = *token {
399+
if require_literal(c) {
400+
return SubPatternDoesntMatch;
401+
}
399402
}
403+
400404
prev_char.set(Some(c));
401405
file = next;
402406
}
@@ -426,7 +430,7 @@ impl Pattern {
426430
Char(c2) => {
427431
chars_eq(c, c2, options.case_sensitive)
428432
}
429-
AnySequence(_) => {
433+
AnySequence | AnyRecursiveSequence => {
430434
unreachable!()
431435
}
432436
};

0 commit comments

Comments
 (0)