rename crate and add docstrings
This commit is contained in:
parent
17fdff594d
commit
88a4e26897
|
@ -1,6 +1,6 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "rust-fzf"
|
||||
name = "fzf"
|
||||
version = "0.1.0"
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "rust-fzf"
|
||||
name = "fzf"
|
||||
version = "0.1.0"
|
||||
authors = ["kageru <kageru@encode.moe>"]
|
||||
edition = "2018"
|
||||
|
|
28
src/lib.rs
28
src/lib.rs
|
@ -1,3 +1,9 @@
|
|||
#![crate_name = "fzf"]
|
||||
|
||||
|
||||
/**
|
||||
* Return a filtered `Vec` of elements that contain the substring `needle`.
|
||||
*/
|
||||
pub fn filter_substring<'a>(needle: &str, haystack: &Vec<&'a str>) -> Vec<&'a str> {
|
||||
return haystack
|
||||
.iter()
|
||||
|
@ -6,6 +12,11 @@ pub fn filter_substring<'a>(needle: &str, haystack: &Vec<&'a str>) -> Vec<&'a st
|
|||
.collect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a filtered `Vec` of elements which `needle` is a subsequence of.
|
||||
* (I know what I’m doing. Let me end a sentence with a preposition.) :^)
|
||||
* See `is_subsequence` for more information on that.
|
||||
*/
|
||||
pub fn filter_subsequence<'a>(needle: &str, haystack: &Vec<&'a str>) -> Vec<&'a str> {
|
||||
return haystack
|
||||
.iter()
|
||||
|
@ -14,9 +25,20 @@ pub fn filter_subsequence<'a>(needle: &str, haystack: &Vec<&'a str>) -> Vec<&'a
|
|||
.collect();
|
||||
}
|
||||
|
||||
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)
|
||||
/**
|
||||
* Check whether the needle is a subsequence of the string.
|
||||
* This is the case if string contains all of the characters in needle in the correct order, but
|
||||
* not necessarily in succession.
|
||||
*
|
||||
* ```
|
||||
* use fzf::is_subsequence;
|
||||
* // ‘as’ is contained in ‘a(d)s’
|
||||
* assert!(is_subsequence("as", "ads"));
|
||||
* // but not in ‘sa’
|
||||
* assert!(!is_subsequence("as", "sa"));
|
||||
* ```
|
||||
*/
|
||||
pub fn is_subsequence(needle: &str, string: &str) -> bool {
|
||||
let mut chars = string.chars();
|
||||
'needle: for nc in needle.chars() {
|
||||
for c in &mut chars {
|
||||
|
|
Loading…
Reference in New Issue