Skip to content

Commit f9ce91c

Browse files
authored
Merge pull request #195 from Rawk/until_next_unindented
refactor: Remove regex dependency in `until_next_unindented`
2 parents 75b5b5c + 63137d5 commit f9ce91c

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

rasn-compiler/src/lexer/util.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ where
3838
}
3939

4040
pub fn until_next_unindented(input: &str, at_least_until: usize, fallback_len: usize) -> &str {
41-
match regex::Regex::new("\n[A-Za-z0-9]")
42-
.ok()
43-
.and_then(|needle| needle.find(&input[at_least_until..]))
44-
{
45-
Some(m) => &input[..(m.start() + at_least_until)],
46-
_ => input[..input.len().min(fallback_len)].trim(),
41+
let mut prev_was_newline = false;
42+
for (idx, ch) in input[at_least_until..].char_indices() {
43+
if prev_was_newline && ch.is_ascii_alphanumeric() {
44+
// Found "\n[A-Za-z0-9]" pattern, return up to the newline
45+
return &input[..(idx - 1 + at_least_until)];
46+
}
47+
prev_was_newline = ch == '\n';
4748
}
49+
50+
// No match found, use fallback
51+
input[..input.len().min(fallback_len)].trim()
4852
}
4953

5054
pub fn hex_to_bools(c: char) -> [bool; 4] {

0 commit comments

Comments
 (0)