Generalize common_prefix for AsStr inputs.
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
@@ -93,15 +93,19 @@ where
|
||||
/// ```
|
||||
#[must_use]
|
||||
#[allow(clippy::string_slice)]
|
||||
pub fn common_prefix<'a>(choice: &'a [&str]) -> &'a str {
|
||||
pub fn common_prefix<T: AsRef<str>>(choice: &[T]) -> &str {
|
||||
choice.first().map_or(EMPTY, move |best| {
|
||||
choice.iter().skip(1).fold(*best, |best, choice| {
|
||||
&best[0..choice
|
||||
.char_indices()
|
||||
.zip(best.char_indices())
|
||||
.take_while(|&(a, b)| a == b)
|
||||
.count()]
|
||||
})
|
||||
choice
|
||||
.iter()
|
||||
.skip(1)
|
||||
.fold(best.as_ref(), |best, choice| {
|
||||
&best[0..choice
|
||||
.as_ref()
|
||||
.char_indices()
|
||||
.zip(best.char_indices())
|
||||
.take_while(|&(a, b)| a == b)
|
||||
.count()]
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user