e_utils/system/
regex.rs

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use memchr::memmem;
pub use regex::Regex;

fn split_pattern(pattern: &str) -> Vec<&str> {
  let mut parts = Vec::new();
  let mut start = 0;

  for (i, c) in pattern.char_indices() {
    if c == '*' || c == '~' || c == '$' || c == '!' {
      if start != i {
        parts.push(&pattern[start..i]);
      }
      parts.push(&pattern[i..i + 1]);
      start = i + 1;
    }
  }

  if start < pattern.len() {
    parts.push(&pattern[start..]);
  }

  parts
}
/// 在输入字符串中搜索匹配给定模式的行
///
/// # 参数
///
/// * `input` - 要搜索的输入字符串
/// * `pattern` - 搜索模式
///
/// # 返回值
///
/// 返回一个元组 `(bool, Option<&str>)`,其中:
/// - 布尔值表示是否找到匹配
/// - `Option<&str>` 包含匹配的子字符串(如果找到)
///
/// # 示例
///
/// ```
///  let test_str = "test\nexe \"Number 123 321-\"\n更多测试2\n12";
/// let inputs = [
///   (test_str, "!ABC123", true, Some("test\nexe \"Number 123 321-\"\n更多测试2\n12\n1xxx Done W  \"1a2\"\nNNNWD Done R   TEST1")),
///   (test_str, "!Done R   ", false, Some("NNNWD Done R   TEST1")),
///   (test_str, "$ Done R   TEST1", true, Some("NNNWD")),
///   (test_str, "Done W  \"$\"", true, Some("1a2")),
///   (test_str, "Done R   $", true, Some("TEST1")),
///   (test_str, "tes~", true, Some("tes")),
///   (test_str, "tes*", true, Some("test")),
///   (test_str, "*123*", true, Some("exe \"Number 123 321-\"")),
///   (test_str, "更多测试2", true, Some("更多测试2")),
///   (test_str, "*123~", true, Some("exe \"Number 123")),
///   (test_str, "~123*", true, Some("123 321-\"")),
///   (test_str, "123*", false, Some(test_str)),
///   (test_str, "123", false, Some(test_str)),
///   (test_str, "~123", false, Some(test_str)),
/// ];
/// for (input, pattern, expected_match, expected_capture) in &inputs {
///   let result = regex2(input, pattern);
///   println!(
///     "Input: '{}', Pattern: '{}', Result: {:?}",
///     input, pattern, result
///   );
///   assert_eq!(result, (*expected_match, *expected_capture));
/// }
/// ```
pub fn regex2<'a>(input: &'a str, pattern: &'a str) -> (bool, Option<&'a str>) {
  let parts = split_pattern(pattern);
  match parts.as_slice() {
    ["!", suffix] => {
      for line in input.lines() {
        if line.contains(suffix) {
          return (false, Some(line));
        }
      }
      return (true, None);
    }
    [_, "!"] => {}
    _ => {
      for line in input.lines() {
        match parts.as_slice() {
          [] | [_] if line == pattern => return (true, Some(line)),
          ["$", suffix, "*"] => {
            if line.contains(suffix) {
              if let Some((res, _)) = line.split_once(suffix) {
                return (true, Some(res));
              }
            }
          }
          ["*", prefix, "$"] => {
            if line.contains(prefix) {
              if let Some((_left, right)) = line.split_once(prefix) {
                return (true, Some(right));
              }
            }
          }
          [prefix, "$"] => {
            if let Some((left, right)) = line.split_once(prefix) {
              if left.is_empty() {
                return (true, Some(right));
              }
            }
          }
          ["$", suffix] => {
            if let Some((left, right)) = line.split_once(suffix) {
              if right.is_empty() {
                return (true, Some(left));
              }
            }
          }
          [prefix, suffix] => {
            if prefix.contains('*') || prefix.contains('~') {
              if let Some(pos) = memmem::find(line.as_bytes(), suffix.as_bytes()) {
                let res = &line[..pos + suffix.len()];
                if res == line {
                  return (true, Some(if prefix.contains('*') { res } else { suffix }));
                }
              }
            } else if let Some(pos) = memmem::find(line.as_bytes(), prefix.as_bytes()) {
              if pos == 0 {
                let res = &line[..prefix.len()
                  + if suffix.contains('*') {
                    line.len() - prefix.len()
                  } else {
                    0
                  }];
                return (true, Some(res));
              }
            }
          }
          [prefix, "$", suffix] => {
            if line.contains(prefix) || line.contains(suffix) {
              if let Some((_, v)) = line.split_once(prefix) {
                if let Some((res, _)) = v.split_once(suffix) {
                  return (true, Some(res));
                }
              }
            }
          }
          [prefix, mid, suffix] => {
            if let Some(pos) = memmem::find(line.as_bytes(), mid.as_bytes()) {
              let start = if prefix.contains('*') { 0 } else { pos };
              let end = if suffix.contains('*') {
                line.len()
              } else {
                pos + mid.len()
              };
              return (true, Some(&line[start..end]));
            }
          }
          _ => {}
        }
      }
    }
  }
  (false, Some(input))
}

#[cfg(test)]
mod tests {
  use super::*;
  #[test]
  fn test_regex2_various_inputs() {
    let test_str =
      "test\nexe \"Number 123 321-\"\n更多测试2\n12\n1xxx Done W  \"1a2\"\nNNNWD Done R   TEST1\ntest_1_.txt";
    let inputs = [
      (test_str, "!ABC123", true, None),
      (test_str, "tes~", true, Some("tes")),
      (test_str, "tes*", true, Some("test")),
      (test_str, "*123*", true, Some("exe \"Number 123 321-\"")),
      (test_str, "更多测试2", true, Some("更多测试2")),
      (test_str, "*123~", true, Some("exe \"Number 123")),
      (test_str, "~123*", true, Some("123 321-\"")),
      (test_str, "123*", false, Some(test_str)),
      (test_str, "*.txt", true, Some("test_1_.txt")),
      (test_str, "*.txt", true, Some("test_1_.txt")),
      (test_str, "$ Done R   TEST1", true, Some("NNNWD")),
      (test_str, "Done W  \"$\"", true, Some("1a2")),
      (test_str, "*WD Done R   $", true, Some("TEST1")),
      (test_str, "123", false, Some(test_str)),
      (test_str, "~123", false, Some(test_str)),
      (test_str, "!Done R   ", false, Some("NNNWD Done R   TEST1")),
    ];

    for (input, pattern, expected_match, expected_capture) in &inputs {
      let result = regex2(input, pattern);
      println!(
        "Input: '{}', Pattern: '{}', Result: {:?}",
        input, pattern, result
      );
      assert_eq!(result, (*expected_match, *expected_capture));
    }
  }
}