mitoo 0.3.0

mitoo is a Rust toolkit library that encapsulates methods such as configuration reading, file operations, encryption and decryption, transcoding, regular expressions, threading, collections, trees, sqlite, rabbitMQ, etc., and customizes or integrates various Util tool classes.
Documentation
/// String-related operations
pub struct StrUtil;

impl StrUtil {
    /// 将字符串按照指定分隔符进行分割
    ///
    /// # 参数
    /// * `str` - 需要分割的字符串切片
    /// * `delimiter` - 用作分割依据的分隔符字符串
    ///
    /// # 返回值
    /// 返回一个包含分割后子字符串的向量,每个元素都是原字符串的切片引用
    ///
    /// # 示例
    /// ```
    /// let result = split("hello,world,rust", ",");
    /// assert_eq!(result, vec!["hello", "world", "rust"]);
    /// ```
    pub fn split<'a>(str: &'a str, delimiter: &str) -> Vec<&'a str> {
        str.split(delimiter).collect()
    }

    /// 从字符串右侧开始查找分隔符,并将字符串分割为两部分
    /// 
    /// # 参数
    /// * [s](file://d:\git-repo-rust\mitoo\src\id_util.rs) - 需要分割的字符串切片
    /// * `delimiter` - 用作分割依据的分隔符字符串
    /// 
    /// # 返回值
    /// 返回一个Option类型,如果找到分隔符则包含分割后的两个字符串切片的元组,
    /// 第一个元素是分隔符左侧的部分,第二个元素是分隔符右侧的部分;
    /// 如果未找到分隔符则返回None
    pub fn rsplit<'a>(s: &'a str, delimiter: &str) -> Option<(&'a str, &'a str)> {
        // 找到最后一个分隔符的索引位置
        s.rfind(delimiter).map(|index| {
            // 分割为两部分:索引位置前的部分和之后的部分
            (&s[..index], &s[index + 1..])
        })
    }

    /// 从字符串中移除指定的前缀
    ///
    /// 如果字符串以指定前缀开头,则返回移除前缀后的字符串切片
    pub fn remove_prefix<'a>(str: &'a str, prefix: &str) -> &'a str {
        if str.starts_with(prefix) { &str[prefix.len()..] } else { str }
    }

    /// 从字符串中移除指定的后缀
    ///
    /// 如果字符串以指定后缀结尾,则返回移除该后缀后的字符串切片
    pub fn remove_suffix<'a>(str: &'a str, suffix: &str) -> &'a str {
        if str.ends_with(suffix) { &str[..str.len() - suffix.len()] } else { str }
    }

    /// 字符串反转
    pub fn reverse(str: &str) -> String {
        // 创建一个空的字符串用于存储反转后的结果
        let mut reversed = String::new();
        // 遍历字符串的每个字符
        for c in str.chars() {
            // 在每次迭代中,将当前字符添加到反转后的字符串的开头
            reversed.insert(0, c);
        }
        // 返回反转后的字符串
        reversed
    }
    
    /// 根据字母顺序比较获取两个字符串中的最大值
    pub fn max<'a>(str1: &'a str, str2: &'a str) -> &'a str {
        if str1 > str2 { str1 } else { str2 }
    }

    /// 根据字母顺序比较获取两个字符串中的最小值
    pub fn min<'a>(str1: &'a str, str2: &'a str) -> &'a str {
        if str1 < str2 { str1 } else { str2 }
    }
    
}