use crate::if_return;
pub trait StartsWithStr {
fn starts_with_str(&self, needle: &str) -> bool;
}
impl StartsWithStr for [char] {
fn starts_with_str(&self, needle: &str) -> bool {
if_return! { needle.is_empty() => true }
if_return! { self.is_empty() => false }
let mut needle_chars = needle.chars();
for c in self.iter() {
match needle_chars.next() {
Some(c2) if *c == c2 => (),
None => return true,
_ => return false,
}
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{asserts, macro_once};
#[test]
fn test_starts_with_str() {
macro_once! {
macro test_starts_with_str( $( [ $( $char:literal )* ] => $prefix:expr ; )* ) {
asserts! {
$(
[$( $char ),*].starts_with_str($prefix),
)*
}
}
['a' 'b' 'c'] => "abc";
['a' 'b' 'c'] => "ab";
['a' 'b' 'c'] => "a";
['a' 'b' 'c'] => "";
}
}
}