Skip to content

Commit 3b644b6

Browse files
match delimiter on bytes (#254)
1 parent d053693 commit 3b644b6

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

crates/djls-templates/src/tokens.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,40 @@ impl TagDelimiter {
1515
pub const LENGTH_U32: u32 = 2;
1616

1717
#[must_use]
18-
pub fn from_input(input: &str) -> Option<TagDelimiter> {
19-
[Self::Block, Self::Variable, Self::Comment]
20-
.into_iter()
21-
.find(|kind| input.starts_with(kind.opener()))
18+
pub fn from_input(input: &str) -> Option<Self> {
19+
let bytes = input.as_bytes();
20+
21+
if bytes.len() < Self::LENGTH {
22+
return None;
23+
}
24+
25+
if bytes[0] != Self::CHAR_OPEN as u8 {
26+
return None;
27+
}
28+
29+
match bytes[1] {
30+
b'%' => Some(Self::Block),
31+
b'{' => Some(Self::Variable),
32+
b'#' => Some(Self::Comment),
33+
_ => None,
34+
}
2235
}
2336

2437
#[must_use]
2538
pub fn opener(self) -> &'static str {
2639
match self {
27-
TagDelimiter::Block => "{%",
28-
TagDelimiter::Variable => "{{",
29-
TagDelimiter::Comment => "{#",
40+
Self::Block => "{%",
41+
Self::Variable => "{{",
42+
Self::Comment => "{#",
3043
}
3144
}
3245

3346
#[must_use]
3447
pub fn closer(self) -> &'static str {
3548
match self {
36-
TagDelimiter::Block => "%}",
37-
TagDelimiter::Variable => "}}",
38-
TagDelimiter::Comment => "#}",
49+
Self::Block => "%}",
50+
Self::Variable => "}}",
51+
Self::Comment => "#}",
3952
}
4053
}
4154
}

0 commit comments

Comments
 (0)