xCommonLib/utils/
string_utils.rs

1
2
3use crate::base::id_generator::make_node_id;
4
5/**
6 * 验证版本号
7 */
8#[allow(dead_code)]
9pub fn validate_version(version: &str) -> bool {
10    for ch in version.chars() {
11        if (ch > '9' || ch < '0') && ch != '.' {
12            return false;
13        }
14    }
15    true
16}
17
18#[allow(dead_code)]
19pub fn str_ip_to_int(str_ip: &str) -> u32 {
20    let words: Vec<&str> = str_ip.split('.').collect();
21
22    if words.len() != 4 {
23        return u32::MAX;
24    }
25
26    let word1 = words[0].parse::<u8>().unwrap();
27    let word2 = words[1].parse::<u8>().unwrap();
28    let word3 = words[2].parse::<u8>().unwrap();
29    let word4 = words[3].parse::<u8>().unwrap();
30    (word4 as u32) << 24 | (word3 as u32) << 16 | (word2 as u32) << 8 | (word1 as u32)
31}
32
33/**
34 *  将 int 转换为 字符串
35 */
36#[allow(dead_code)]
37pub fn int_ip_to_str(int_ip: u32) -> String {
38    let word1 = ((int_ip >> 24) & 255) as u8;
39    let word2 = ((int_ip >> 16) & 255) as u8;
40    let word3 = ((int_ip >> 8) & 255) as u8;
41    let word4 = (int_ip & 255) as u8;
42    format!("{}.{}.{}.{}", word4, word3, word2, word1)
43}
44
45#[allow(dead_code)]
46pub fn int_ip_from_conn_id(conn_id: i64) -> u32 {
47    (conn_id >> 16) as u32
48}
49
50#[allow(dead_code)]
51pub fn addr_to_conn_id(addr: &str) -> i64 {
52    let words: Vec<&str> = addr.split(':').collect();
53    if words.len() != 2 {
54        return 0;
55    }
56    let port = words[1].parse::<u16>();
57    if port.is_err() {
58        return 0;
59    }
60    let port = port.unwrap();
61    make_node_id(words[0], port)
62}
63
64pub fn convert_to_snake_case(input: &str) -> String {
65    let input_length = input.chars().count();
66    let mut result = String::with_capacity(input_length + input_length / 2);
67    let mut chars = input.chars().peekable();
68    while let Some(c) = chars.next() {
69        if c.is_ascii_uppercase() {
70            if !result.is_empty() {
71                result.push('_');
72            }
73            result.push(c.to_ascii_lowercase());
74        } else {
75            result.push(c);
76        }
77    }
78    result
79}
80
81pub fn convert_to_camel_case(input: &str) -> String {
82  let mut output = String::new();
83  let mut capitalize_next = true;
84  for c in input.chars() {
85      if c == '_' || !c.is_ascii_alphanumeric() {
86          capitalize_next = true;
87      } else if capitalize_next {
88          output.push(c.to_ascii_uppercase());
89          capitalize_next = false;
90      } else {
91          output.push(c);
92      }
93  }
94  output
95}
96
97pub fn raw_to_string(bytes: *const u8, len: u32) -> String {
98    unsafe {
99        let file_name_slice = std::slice::from_raw_parts(bytes, len as usize);
100        let file_name_bytes = file_name_slice.to_vec();
101        String::from_utf8_unchecked(file_name_bytes)
102    }
103}
104
105pub fn extract_version(input: &str) -> Option<String> {
106   let mut words:Vec<&str> = input.split("_").collect();
107    if words.len() < 2 {
108      return None;
109    }
110    words.remove(0);
111    Some(words.join("_"))
112}