use regex::Regex;
pub fn index_all(subject: &str, search: &str, from_index: usize) -> Vec<usize> {
if subject.is_empty() || crate::count::count(subject) < from_index {
return vec![];
}
let string_slice = &subject[subject.char_indices().nth(from_index).unwrap().0..];
let mut res = Vec::new();
for (i, c) in crate::split::chars(string_slice).iter().enumerate() {
if search.contains(c) {
res.push(i)
}
}
res
}
pub fn index_of(subject: &str, search: &str, from_index: usize) -> i8 {
match search.len() {
0 => 0,
_ => {
if crate::count::count(subject) < from_index {
return -1;
}
let string_slice = &subject[subject.char_indices().nth(from_index).unwrap().0..];
match crate::split::chars(string_slice)
.iter()
.enumerate()
.position(|(pos, _)| {
match &string_slice[string_slice.char_indices().nth(pos).unwrap().0..]
.find(search)
{
Some(x) => *x == 0,
None => false,
}
}) {
Some(x) => x as i8,
None => -1,
}
}
}
}
pub fn last_index_of(subject: &str, search: &str, from_index: usize) -> i8 {
match search.len() {
0 => 0,
_ => {
if crate::count::count(subject) < from_index {
return -1;
}
let string_slice = &subject[subject.char_indices().nth(from_index).unwrap().0..];
let string_chars = crate::split::chars(string_slice);
match string_chars.iter().enumerate().rev().position(|(pos, _)| {
match &string_slice[string_slice.char_indices().nth(pos).unwrap().0..].find(search)
{
Some(x) => *x == 0,
None => false,
}
}) {
Some(x) => (string_chars.len() - x - 1) as i8,
None => -1,
}
}
}
}
pub fn search(subject: &str, pattern: &str, from_index: usize) -> i8 {
if from_index >= crate::split::chars(subject).len() {
return -1;
}
match pattern.len() {
0 => 0,
_ => {
let re: Regex = match Regex::new(pattern) {
Ok(re) => re,
Err(_) => return -1,
};
match re.find_at(subject, from_index) {
None => -1,
Some(x) => x.start() as i8,
}
}
}
}