Skip to main content

rbatis_codegen/codegen/
string_util.rs

1use std::collections::LinkedList;
2
3//find like #{*,*},${*,*} value *
4pub fn find_convert_string(arg: &str) -> LinkedList<(String, String)> {
5    let mut list = LinkedList::new();
6    let chars: Vec<u8> = arg.bytes().collect();
7    let mut item = String::with_capacity(arg.len());
8    let mut index: i32 = -1;
9    for v in &chars {
10        index = index + 1;
11        if !item.is_empty() {
12            item.push(*v as char);
13            if *v == '}' as u8 {
14                let key = item[2..item.len() - 1].to_string();
15                list.push_back((key, item.clone()));
16                item.clear();
17            }
18            continue;
19        }
20        if (*v == '#' as u8 || *v == '$' as u8)
21            && chars.get(index as usize + 1).eq(&Some(&('{' as u8)))
22        {
23            item.push(*v as char);
24        }
25    }
26    return list;
27}
28
29pub fn count_string_num(s: &String, c: char) -> usize {
30    let cs = s.chars();
31    let mut num = 0;
32    for x in cs {
33        if x == c {
34            num += 1;
35        }
36    }
37    return num;
38}
39
40///input 'strings' => strings
41pub fn un_packing_string(column: &str) -> &str {
42    if column.len() >= 2 {
43        if column.starts_with("'") && column.ends_with("'") {
44            return &column[1..column.len() - 1];
45        }
46        if column.starts_with("`") && column.ends_with("`") {
47            return &column[1..column.len() - 1];
48        }
49        if column.starts_with("\"") && column.ends_with("\"") {
50            return &column[1..column.len() - 1];
51        }
52    }
53    return column;
54}
55
56/// Efficiently concatenates SQL fragments with automatic space insertion.
57/// Optimized for hot path in SQL code generation.
58#[inline(always)]
59pub fn concat_str(text: &mut String, append_str: &str) {
60    // Fast path: empty text - just push (no space needed)
61    if text.is_empty() {
62        text.push_str(append_str);
63        return;
64    }
65
66    // Check if we need to add a space between fragments
67    // Space is added when: text doesn't end with space AND append_str doesn't start with space
68    let need_space = !text.ends_with(' ') && !append_str.starts_with(' ');
69
70    // Reserve capacity to reduce reallocations
71    if need_space {
72        text.reserve(1 + append_str.len());
73        text.push(' ');
74        text.push_str(append_str);
75    } else {
76        text.push_str(append_str);
77    }
78}