rbatis_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 un_packing_string(column: &str) -> &str {
54 if column.len() >= 2 {
55 if column.starts_with("'") && column.ends_with("'") {
56 return &column[1..column.len() - 1];
57 }
58 if column.starts_with("`") && column.ends_with("`") {
59 return &column[1..column.len() - 1];
60 }
61 if column.starts_with("\"") && column.ends_with("\"") {
62 return &column[1..column.len() - 1];
63 }
64 }
65 return column;
66}