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