mybatis_sql/
string_util.rs1use std::collections::{HashSet, LinkedList};
2
3pub fn find_convert_string(arg: &str) -> LinkedList<(String, String)> {
5 let mut list = LinkedList::new();
6 let mut cache = HashSet::new();
7 let chars: Vec<u8> = arg.bytes().collect();
8 let mut item = String::new();
9 let mut last_index: i32 = -1;
10 let mut index: i32 = -1;
11 for v in &chars {
12 index = index + 1;
13 if last_index == -1 && (*v == '#' as u8 || *v == '$' as u8) {
14 let next = chars.get(index as usize + 1);
15 let next_char = '{' as u8;
16 if next.is_some() && next.unwrap().eq(&next_char) {
17 last_index = index;
18 }
19 continue;
20 }
21 if *v == '}' as u8 && last_index != -1 {
22 item = String::from_utf8(chars[(last_index + 2) as usize..index as usize].to_vec())
23 .unwrap();
24 if cache.get(&item).is_some() {
25 item.clear();
26 last_index = -1;
27 continue;
28 }
29 let value =
30 String::from_utf8(chars[last_index as usize..(index + 1) as usize].to_vec())
31 .unwrap();
32 cache.insert(item.clone());
33 list.push_back((item.clone(), value));
34 item.clear();
35 last_index = -1;
36 }
37 }
38 return list;
39}
40
41pub fn count_string_num(s: &String, c: char) -> usize {
42 let cs = s.chars();
43 let mut num = 0;
44 for x in cs {
45 if x == c {
46 num += 1;
47 }
48 }
49 return num;
50}
51
52pub fn to_snake_name(name: &str) -> String {
53 let chs = name.chars();
54 let mut new_name = String::new();
55 let mut index = 0;
56 let chs_len = name.len();
57 for x in chs {
58 if x.is_uppercase() {
59 if index != 0 && (index + 1) != chs_len {
60 new_name.push_str("_");
61 }
62 new_name.push_str(x.to_lowercase().to_string().as_str());
63 } else {
64 new_name.push(x);
65 }
66 index += 1;
67 }
68 return new_name;
69}
70
71pub fn un_packing_string(column: &str) -> &str {
73 if column.len() >= 2 {
74 if column.starts_with("'") && column.ends_with("'") {
75 return &column[1..column.len() - 1];
76 }
77 if column.starts_with("`") && column.ends_with("`") {
78 return &column[1..column.len() - 1];
79 }
80 if column.starts_with("\"") && column.ends_with("\"") {
81 return &column[1..column.len() - 1];
82 }
83 }
84 return column;
85}