1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112


use crate::base::id_generator::make_node_id;

/**
 * 验证版本号
 */
#[allow(dead_code)]
pub fn validate_version(version: &str) -> bool {
    for ch in version.chars() {
        if (ch > '9' || ch < '0') && ch != '.' {
            return false;
        }
    }
    true
}

#[allow(dead_code)]
pub fn str_ip_to_int(str_ip: &str) -> u32 {
    let words: Vec<&str> = str_ip.split('.').collect();

    if words.len() != 4 {
        return u32::MAX;
    }

    let word1 = words[0].parse::<u8>().unwrap();
    let word2 = words[1].parse::<u8>().unwrap();
    let word3 = words[2].parse::<u8>().unwrap();
    let word4 = words[3].parse::<u8>().unwrap();
    (word4 as u32) << 24 | (word3 as u32) << 16 | (word2 as u32) << 8 | (word1 as u32)
}

/**
 *  将 int 转换为 字符串
 */
#[allow(dead_code)]
pub fn int_ip_to_str(int_ip: u32) -> String {
    let word1 = ((int_ip >> 24) & 255) as u8;
    let word2 = ((int_ip >> 16) & 255) as u8;
    let word3 = ((int_ip >> 8) & 255) as u8;
    let word4 = (int_ip & 255) as u8;
    format!("{}.{}.{}.{}", word4, word3, word2, word1)
}

#[allow(dead_code)]
pub fn int_ip_from_conn_id(conn_id: i64) -> u32 {
    (conn_id >> 16) as u32
}

#[allow(dead_code)]
pub fn addr_to_conn_id(addr: &str) -> i64 {
    let words: Vec<&str> = addr.split(':').collect();
    if words.len() != 2 {
        return 0;
    }
    let port = words[1].parse::<u16>();
    if port.is_err() {
        return 0;
    }
    let port = port.unwrap();
    make_node_id(words[0], port)
}

pub fn convert_to_snake_case(input: &str) -> String {
    let input_length = input.chars().count();
    let mut result = String::with_capacity(input_length + input_length / 2);
    let mut chars = input.chars().peekable();
    while let Some(c) = chars.next() {
        if c.is_ascii_uppercase() {
            if !result.is_empty() {
                result.push('_');
            }
            result.push(c.to_ascii_lowercase());
        } else {
            result.push(c);
        }
    }
    result
}

pub fn convert_to_camel_case(input: &str) -> String {
  let mut output = String::new();
  let mut capitalize_next = true;
  for c in input.chars() {
      if c == '_' || !c.is_ascii_alphanumeric() {
          capitalize_next = true;
      } else if capitalize_next {
          output.push(c.to_ascii_uppercase());
          capitalize_next = false;
      } else {
          output.push(c);
      }
  }
  output
}

pub fn raw_to_string(bytes: *const u8, len: u32) -> String {
    unsafe {
        let file_name_slice = std::slice::from_raw_parts(bytes, len as usize);
        let file_name_bytes = file_name_slice.to_vec();
        String::from_utf8_unchecked(file_name_bytes)
    }
}

pub fn extract_version(input: &str) -> Option<String> {
   let mut words:Vec<&str> = input.split("_").collect();
    if words.len() < 2 {
      return None;
    }
    words.remove(0);
    Some(words.join("_"))
}