mybatis_util/
string_util.rs

1use std::collections::{BTreeMap, HashSet, LinkedList};
2use std::io::Read;
3
4use rbson::Bson;
5
6pub const LOG_SPACE: &'static str = "                                                         ";
7
8//find like #{*,*},${*,*} value *
9pub fn find_convert_string(arg: &str) -> LinkedList<(String, String)> {
10    let mut list = LinkedList::new();
11    let mut cache = HashSet::new();
12    let chars: Vec<u8> = arg.bytes().collect();
13    let mut item = String::with_capacity(arg.len());
14    let mut index: i32 = -1;
15    for v in &chars {
16        index = index + 1;
17        if !item.is_empty() {
18            item.push(*v as char);
19            if *v == '}' as u8 {
20                if cache.get(&item).is_some() {
21                    item.clear();
22                    continue;
23                }
24                let key = item[2..item.len() - 1].to_string();
25                cache.insert(item.clone());
26                list.push_back((key, item.clone()));
27                item.clear();
28            }
29            continue;
30        }
31        if (*v == '#' as u8 || *v == '$' as u8)
32            && chars.get(index as usize + 1).eq(&Some(&('{' as u8)))
33        {
34            item.push(*v as char);
35        }
36    }
37    return list;
38}
39
40pub fn count_string_num(s: &String, c: char) -> usize {
41    let cs = s.chars();
42    let mut num = 0;
43    for x in cs {
44        if x == c {
45            num += 1;
46        }
47    }
48    return num;
49}
50
51pub fn to_snake_name(name: &str) -> String {
52    let chs = name.chars();
53    let mut new_name = String::new();
54    let mut index = 0;
55    let chs_len = name.len();
56    for x in chs {
57        if x.is_uppercase() {
58            if index != 0 && (index + 1) != chs_len {
59                new_name.push_str("_");
60            }
61            new_name.push_str(x.to_lowercase().to_string().as_str());
62        } else {
63            new_name.push(x);
64        }
65        index += 1;
66    }
67    return new_name;
68}
69
70///input 'strings' => strings
71pub fn un_packing_string(column: &str) -> &str {
72    if column.len() >= 2 {
73        if column.starts_with("'") && column.ends_with("'") {
74            return &column[1..column.len() - 1];
75        }
76        if column.starts_with("`") && column.ends_with("`") {
77            return &column[1..column.len() - 1];
78        }
79        if column.starts_with("\"") && column.ends_with("\"") {
80            return &column[1..column.len() - 1];
81        }
82    }
83    return column;
84}
85
86//find like {*},{*} value *
87pub fn find_format_string(arg: &str) -> LinkedList<(String, String)> {
88    let mut list = LinkedList::new();
89    let chars: Vec<u8> = arg.bytes().collect();
90    let mut item = String::with_capacity(arg.len());
91    let mut index: i32 = -1;
92    for v in &chars {
93        index = index + 1;
94        if !item.is_empty() {
95            item.push(*v as char);
96            if *v == '}' as u8 {
97                let key = item[1..item.len() - 1].to_string();
98                list.push_back((key, item.clone()));
99                item.clear();
100            }
101            continue;
102        }
103        if *v == '{' as u8 {
104            item.push(*v as char);
105        }
106    }
107    return list;
108}
109
110#[test]
111fn test_find_formats() {
112    println!("{:?}", find_format_string("1111{}222{1}2"));
113}