simple_string_patterns/
utils.rs

1
2use crate::{enums::StringBounds, BoundsPosition, CaseMatchMode};
3
4/// Miscellaneous utility functions that do not belong to structs
5/// corrects a numeric string after it has been extracted by removing trailing dots or commas
6pub(crate) fn add_sanitized_numeric_string(output: &mut Vec<String>, num_string: &str) {
7  output.push(num_string.trim_end_matches(".").trim_end_matches(",").to_string());
8}
9
10
11/// Convert an array of strs to a vector of SimpleBounds with start/end/contains and case-sensity rules
12/// as used in matched_conditional
13/// Only used internally with interger mode
14pub(crate) fn strs_to_string_bounds<'a>(strs: &'a [&str], case_mode: CaseMatchMode, mode: BoundsPosition) -> Vec<StringBounds<'a>> {
15  strs.into_iter().map(|txt| StringBounds::new(mode, *txt, true, case_mode)).collect()
16}
17
18/// Convert an array of strs to a vector of SimpleBounds with start/end/contains and case-sensity rules
19/// as used in matched_conditional
20/// Only used internally with interger mode
21pub(crate) fn strs_to_negative_string_bounds<'a>(strs: &'a [&str], case_mode: CaseMatchMode, mode: BoundsPosition) -> Vec<StringBounds<'a>> {
22  strs.into_iter().map(|txt| StringBounds::new(mode, *txt, false, case_mode)).collect()
23}
24
25/// Convert an array of str/boolean tuples to a vector of SimpleBounds with start/end/contains
26/// as used in matched_conditional
27/// Only used internally with interger mode
28pub(crate) fn pairs_to_string_bounds<'a>(pairs: &'a [(&str, bool)], mode: BoundsPosition) -> Vec<StringBounds<'a>> {
29  pairs.into_iter().map(|(txt, ci)| StringBounds::new(mode, *txt, true, CaseMatchMode::insensitive(*ci))).collect()
30}
31
32/// Extract a string segment by its index where a negative value starts from the end
33/// and an unmatched element returns None
34pub(crate) fn extract_string_element_by_index(parts: Vec<String>, index: i32) -> Option<String> {
35  let num_parts = parts.len();
36  let target_index = if index >= 0 { index as usize } else { (num_parts as i32 + index) as usize };
37  if target_index < num_parts {
38    parts.get(target_index).map(|part| part.to_owned())
39  } else {
40    None
41  }
42}