Skip to content

Commit c91dbe4

Browse files
committed
Simplify parser methods a bit
1 parent bec3043 commit c91dbe4

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

tokenizer.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn next_component_value(parser: &mut Parser) -> Option<ComponentValue> {
3535
let c = parser.current_char();
3636
Some(match c {
3737
'-' => {
38-
if parser.starts_with(~"-->") {
38+
if parser.starts_with("-->") {
3939
parser.position += 3;
4040
CDC
4141
}
@@ -46,7 +46,7 @@ pub fn next_component_value(parser: &mut Parser) -> Option<ComponentValue> {
4646
}
4747
},
4848
'<' => {
49-
if parser.starts_with(~"<!--") {
49+
if parser.starts_with("<!--") {
5050
parser.position += 4;
5151
CDO
5252
} else {
@@ -57,6 +57,12 @@ pub fn next_component_value(parser: &mut Parser) -> Option<ComponentValue> {
5757
'0'..'9' | '.' | '+' => consume_numeric(parser),
5858
'u' | 'U' => consume_unicode_range(parser),
5959
'a'..'z' | 'A'..'Z' | '_' | '\\' => consume_ident(parser),
60+
'~' if parser.starts_with("~=") => { parser.position += 2; IncludeMath }
61+
'|' if parser.starts_with("|=") => { parser.position += 2; DashMatch }
62+
'^' if parser.starts_with("^=") => { parser.position += 2; PrefixMatch }
63+
'$' if parser.starts_with("$=") => { parser.position += 2; SuffixMatch }
64+
'*' if parser.starts_with("*=") => { parser.position += 2; SubstringMatch }
65+
'|' if parser.starts_with("||") => { parser.position += 2; Column }
6066
_ if c >= '\x80' => consume_ident(parser), // Non-ASCII
6167
_ => {
6268
match parser.consume_char() {
@@ -82,18 +88,6 @@ pub fn next_component_value(parser: &mut Parser) -> Option<ComponentValue> {
8288
']' => CloseSquareBraket,
8389
'{' => CurlyBraketBlock(consume_block(parser, CloseCurlyBraket)),
8490
'}' => CloseCurlyBraket,
85-
'~' if !parser.is_eof() && parser.current_char() == '='
86-
=> { parser.position += 1; IncludeMath }
87-
'|' if !parser.is_eof() && parser.current_char() == '='
88-
=> { parser.position += 1; DashMatch }
89-
'|' if !parser.is_eof() && parser.current_char() == '|'
90-
=> { parser.position += 1; Column }
91-
'^' if !parser.is_eof() && parser.current_char() == '='
92-
=> { parser.position += 1; PrefixMatch }
93-
'$' if !parser.is_eof() && parser.current_char() == '='
94-
=> { parser.position += 1; SuffixMatch }
95-
'*' if !parser.is_eof() && parser.current_char() == '='
96-
=> { parser.position += 1; SubstringMatch }
9791
_ => Delim(c)
9892
}
9993
}
@@ -170,20 +164,15 @@ impl Parser {
170164
}
171165

172166
#[inline]
173-
fn starts_with(&self, needle: ~str) -> bool {
174-
// XXX Duplicate str::match_at which is not public.
175-
let mut i = self.position;
176-
if i + needle.len() > self.length { return false }
177-
let haystack: &str = self.input;
178-
for needle.bytes_iter().advance |c| { if haystack[i] != c { return false; } i += 1u; }
179-
return true;
167+
fn starts_with(&self, needle: &str) -> bool {
168+
self.input.slice_from(self.position).starts_with(needle)
180169
}
181170
}
182171

183172

184173
#[inline]
185174
fn consume_comments(parser: &mut Parser) {
186-
while parser.starts_with(~"/*") {
175+
while parser.starts_with("/*") {
187176
parser.position += 2; // +2 to consume "/*"
188177
match parser.input.slice_from(parser.position).find_str("*/") {
189178
// +2 to consume "*/"
@@ -215,7 +204,7 @@ macro_rules! is_match(
215204

216205
#[inline]
217206
fn is_invalid_escape(parser: &mut Parser) -> bool {
218-
parser.next_n_chars(2) == ~['\\', '\n']
207+
parser.starts_with("\\\n")
219208
}
220209

221210

@@ -249,11 +238,11 @@ fn consume_quoted_string(parser: &mut Parser, single_quote: bool) -> ComponentVa
249238
return BadString;
250239
},
251240
'\\' => {
252-
match parser.next_n_chars(1) {
253-
['\n'] => parser.position += 1, // Escaped newline
254-
[] => (), // Escaped EOF
255-
_ => string.push_char(consume_escape(parser))
241+
if !parser.is_eof() {
242+
if parser.current_char() == '\n' { parser.position += 1 } // Escaped newline
243+
else { string.push_char(consume_escape(parser)) }
256244
}
245+
// else: escaped EOF, do nothing.
257246
}
258247
c => string.push_char(c),
259248
}
@@ -514,9 +503,11 @@ fn consume_unquoted_url(parser: &mut Parser) -> ComponentValue {
514503
')' => break,
515504
'\x00'..'\x08' | '\x0B' | '\x0E'..'\x1F' | '\x7F' // non-printable
516505
| '"' | '\'' | '(' => return consume_bad_url(parser),
517-
'\\' => match parser.next_n_chars(1) {
518-
['\n'] => return consume_bad_url(parser),
519-
_ => consume_escape(parser)
506+
'\\' => {
507+
if !parser.is_eof() && parser.current_char() == '\n' {
508+
return consume_bad_url(parser)
509+
}
510+
consume_escape(parser)
520511
},
521512
c => c
522513
};

0 commit comments

Comments
 (0)