refactored is_subsequence (thanks, Kübi)

This commit is contained in:
kageru 2019-05-18 15:16:59 +02:00
parent 1c1f51c8fc
commit 17fdff594d
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

View File

@ -18,29 +18,20 @@ fn is_subsequence(needle: &str, string: &str) -> bool {
// we need to explicitly convert here to iterate over chars
// (UTF-8, so possibly multiple bytes long)
let mut chars = string.chars();
let needle_chars = needle.chars();
for nc in needle_chars {
loop {
match chars.next() {
Some(current) => {
// if the character matches, break and continue with the next nc
if current == nc {
break;
}
}
_ => return false
'needle: for nc in needle.chars() {
for c in &mut chars {
if c == nc {
continue 'needle;
}
}
return false;
}
true
}
#[cfg(test)]
mod tests {
use crate::{
filter_substring,
filter_subsequence
};
use crate::{filter_subsequence, filter_substring};
#[test]
fn test_filter_substring_single() {