refactored is_subsequence (thanks, Kübi)
This commit is contained in:
parent
1c1f51c8fc
commit
17fdff594d
21
src/lib.rs
21
src/lib.rs
|
@ -18,29 +18,20 @@ fn is_subsequence(needle: &str, string: &str) -> bool {
|
||||||
// we need to explicitly convert here to iterate over chars
|
// we need to explicitly convert here to iterate over chars
|
||||||
// (UTF-8, so possibly multiple bytes long)
|
// (UTF-8, so possibly multiple bytes long)
|
||||||
let mut chars = string.chars();
|
let mut chars = string.chars();
|
||||||
let needle_chars = needle.chars();
|
'needle: for nc in needle.chars() {
|
||||||
for nc in needle_chars {
|
for c in &mut chars {
|
||||||
loop {
|
if c == nc {
|
||||||
match chars.next() {
|
continue 'needle;
|
||||||
Some(current) => {
|
|
||||||
// if the character matches, break and continue with the next nc
|
|
||||||
if current == nc {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{
|
use crate::{filter_subsequence, filter_substring};
|
||||||
filter_substring,
|
|
||||||
filter_subsequence
|
|
||||||
};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_filter_substring_single() {
|
fn test_filter_substring_single() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user