Tholp's bespoke website generator
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix find_pattern for templates

Tholp 26653810 13a99e73

+14 -17
+14 -17
src/stringtools.rs
··· 266 266 return &tokens[start..end]; 267 267 } 268 268 269 + // Find the first instance of the pattern 269 270 pub fn find_pattern(tokens: &[Token], pat: String) -> Option<(usize, usize)> { 270 271 // (startpoint, length) 271 - // FIXME: this fucks up when the begining of a pattern is repeated 272 - // ex. searching for "[[hello]]" in "[[[[hello]]" yeilds None 273 - // ALSO, this is a coarse search, operating on tokens only, not the characters within 274 - // 272 + 275 273 let split_pattern = split_to_tokens(pat, 0); 276 274 let mut pattern_index: usize = 0; 277 275 let mut token_index: usize = 0; 278 - let mut working_pattern_index: usize = 0; 279 276 280 - for t in tokens { 281 - if t.contents == split_pattern[pattern_index].contents { 282 - pattern_index += 1; 283 - } else { 284 - pattern_index = 0; 285 - working_pattern_index = token_index + 1; 277 + while token_index < tokens.len() && tokens.len() - token_index >= split_pattern.len() { 278 + for t in &tokens[token_index..] { 279 + if t.contents == split_pattern[pattern_index].contents { 280 + pattern_index += 1; 281 + if pattern_index == split_pattern.len() { 282 + return Some((token_index, split_pattern.len())); 283 + } 284 + } else { 285 + pattern_index = 0; 286 + token_index += 1; 287 + break; 288 + } 286 289 } 287 - 288 - if pattern_index == split_pattern.len() { 289 - return Some((working_pattern_index, split_pattern.len())); 290 - } 291 - 292 - token_index += 1; 293 290 } 294 291 295 292 None