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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
use std::cmp::Ordering;
use crate::geo::geodb::GeoDB;
use crate::parser::Parser;
use crate::token::Token;
use crate::NaliText;
#[derive(PartialEq, Clone, Copy, Debug)]
enum IPv4State {
Any,
Digit,
Dot,
}
#[derive(Debug)]
struct IPv4Token {
last_state: IPv4State,
dot_count: u8,
current_number: u32,
start: usize,
}
#[derive(PartialEq, Clone, Copy, Debug)]
enum IPv6State {
Any,
Hex,
Colon,
}
#[derive(Debug)]
struct IPv6Token {
last_state: IPv6State,
colon_count: u8,
has_double_colon: bool,
group_len: u8,
start: usize,
}
#[derive(Default)]
pub struct FastParser {}
impl FastParser {
// Check if two ranges are overlapping
fn is_overlapping(start1: usize, end1: usize, start2: usize, end2: usize) -> bool {
start1 < end2 && start2 < end1
}
// Decide which token to keep when two tokens are overlapping
fn should_keep_first(token1: &Token, start1: usize, token2: &Token, start2: usize) -> bool {
match token1.priority().cmp(&token2.priority()) {
Ordering::Greater => true, // First token has higher priority
Ordering::Less => false, // Second token has higher priority
Ordering::Equal => start1 <= start2, // Same priority, keep the first one
}
}
fn match_ipv6(&self, input: &str) -> Vec<(usize, usize)> {
let mut matches = Vec::new();
let mut v6_token = IPv6Token {
last_state: IPv6State::Any,
colon_count: 0,
has_double_colon: false,
group_len: 0,
start: 0,
};
fn is_hex_char(c: char) -> IPv6State {
if c.is_ascii_hexdigit() {
IPv6State::Hex
} else if c == ':' {
IPv6State::Colon
} else {
IPv6State::Any
}
}
let chars: Vec<char> = input.chars().collect();
let mut i = 0;
while i < chars.len() {
let c = chars[i];
match (v6_token.last_state, is_hex_char(c)) {
(IPv6State::Any, IPv6State::Hex) => {
// Hex start
// A new IPv6 token might start here
v6_token.start = i;
v6_token.last_state = IPv6State::Hex;
v6_token.group_len = 1;
}
(IPv6State::Hex, IPv6State::Hex) => {
// Hex continuation
if v6_token.group_len >= 4 {
// Group is full
if v6_token.has_double_colon || v6_token.colon_count == 7 {
// Already have 7 colons, commit the token
matches.push((v6_token.start, i));
// Reset token
v6_token.last_state = IPv6State::Any;
v6_token.colon_count = 0;
v6_token.has_double_colon = false;
v6_token.group_len = 0;
} else {
// Move token
v6_token.group_len = 3;
v6_token.start = i - 3; // Don't keep first digits
v6_token.has_double_colon = false;
v6_token.colon_count = 0;
}
} else {
v6_token.group_len += 1;
}
}
(IPv6State::Hex, IPv6State::Colon) => {
// Hex end by colon
if (v6_token.has_double_colon && v6_token.colon_count == 6)
|| v6_token.colon_count == 7
{
// Already have 7 colons, commit the token
matches.push((v6_token.start, i));
// Reset token
v6_token.last_state = IPv6State::Any;
v6_token.colon_count = 0;
v6_token.has_double_colon = false;
v6_token.group_len = 0;
} else {
v6_token.colon_count += 1;
v6_token.last_state = IPv6State::Colon;
}
}
(IPv6State::Hex, IPv6State::Any) => {
// Hex end by random char
if v6_token.has_double_colon || v6_token.colon_count == 7 {
// Already have 7 colons, commit the token
matches.push((v6_token.start, i));
}
v6_token.last_state = IPv6State::Any;
v6_token.colon_count = 0;
v6_token.has_double_colon = false;
v6_token.group_len = 0;
}
(IPv6State::Colon, IPv6State::Hex) => {
// Colon start after colon
v6_token.last_state = IPv6State::Hex;
v6_token.group_len = 1;
}
(IPv6State::Colon, IPv6State::Colon) => {
// Colon start after colon
if v6_token.has_double_colon {
// Already have double colon
matches.push((v6_token.start, i - 1));
v6_token.last_state = IPv6State::Any;
v6_token.colon_count = 0;
v6_token.has_double_colon = false;
v6_token.group_len = 0;
} else {
v6_token.has_double_colon = true;
v6_token.last_state = IPv6State::Colon;
}
}
(IPv6State::Colon, IPv6State::Any) => {
// Colon end by random char
if v6_token.has_double_colon {
// Already have double colon, commit the token
matches.push((v6_token.start, i));
}
v6_token.last_state = IPv6State::Any;
v6_token.colon_count = 0;
v6_token.has_double_colon = false;
v6_token.group_len = 0;
}
(_, _) => {
v6_token.last_state = IPv6State::Any;
v6_token.colon_count = 0;
v6_token.has_double_colon = false;
v6_token.group_len = 0;
}
}
i += 1;
}
// Last token
if v6_token.has_double_colon || v6_token.colon_count == 7 {
// Find last non colon character
let mut last_non_colon = chars.len();
for i in (0..chars.len()).rev() {
if chars[i] != ':' {
last_non_colon = i + 1;
break;
}
}
matches.push((v6_token.start, last_non_colon));
};
matches
}
fn match_ipv4(&self, input: &str) -> Vec<(usize, usize)> {
let mut matches = Vec::new();
let mut v4_token = IPv4Token {
last_state: IPv4State::Any,
dot_count: 0,
current_number: 0,
start: 0,
};
let chars: Vec<char> = input.chars().collect();
let mut i = 0;
while i < chars.len() {
let c = chars[i];
// IPv4 parsing
match (v4_token.last_state, c) {
(IPv4State::Any, '0'..='9') => {
// Digit start
// A new IPv4 token might start here
v4_token.start = i;
v4_token.last_state = IPv4State::Digit;
v4_token.current_number = c.to_digit(10).unwrap();
}
(IPv4State::Digit, '0'..='9') => {
// Digit continuation
let current_number = v4_token.current_number * 10 + c.to_digit(10).unwrap();
if current_number > 255 {
// Overflow
if v4_token.dot_count == 3 {
// Check if previous token already valid, commit it
matches.push((v4_token.start, i));
// A new IPv4 token might start here
v4_token.start = i;
v4_token.dot_count = 0;
v4_token.current_number = c.to_digit(10).unwrap();
v4_token.last_state = IPv4State::Digit;
} else {
// Move token
v4_token.last_state = IPv4State::Digit;
v4_token.dot_count = 0;
v4_token.start = i - 1;
// dont keep first digit
v4_token.current_number = current_number % 100;
}
} else {
// Normal digit
v4_token.current_number = current_number;
v4_token.last_state = IPv4State::Digit;
}
}
(IPv4State::Digit, '.') => {
// Digit end by dot
if v4_token.dot_count == 3 {
// Already have 3 dots, commit the token
matches.push((v4_token.start, i));
// Reset token
v4_token.last_state = IPv4State::Any;
v4_token.dot_count = 0;
v4_token.current_number = 0;
} else {
v4_token.dot_count += 1;
v4_token.last_state = IPv4State::Dot;
}
}
(IPv4State::Digit, _) => {
// Digit end by random char
if v4_token.dot_count == 3 {
// Already have 3 dots, commit the token
matches.push((v4_token.start, i));
}
v4_token.last_state = IPv4State::Any;
v4_token.dot_count = 0;
v4_token.current_number = 0;
}
(IPv4State::Dot, '0'..='9') => {
// Digit start after dot
v4_token.last_state = IPv4State::Digit;
v4_token.current_number = c.to_digit(10).unwrap();
}
(_, _) => {
v4_token.last_state = IPv4State::Any;
v4_token.dot_count = 0;
v4_token.current_number = 0;
}
}
i += 1;
}
// Last token
if v4_token.dot_count == 3 && v4_token.last_state == IPv4State::Digit {
matches.push((v4_token.start, chars.len()));
};
matches
}
}
impl<G: GeoDB> Parser<G> for FastParser {
fn name(&self) -> &str {
"fast"
}
fn parse(&self, input: &str, db: &G) -> NaliText {
let mut tokens: Vec<Token> = Vec::new();
let mut matches: Vec<(usize, usize, Token)> = Vec::new();
let ipv4_matches = self.match_ipv4(input);
let ipv6_matches = self.match_ipv6(input);
ipv4_matches.iter().for_each(|(start, end)| {
let ip = &input[*start..*end];
matches.push((*start, *end, Token::IPv4(ip.to_string(), db.lookup(ip))));
});
ipv6_matches.iter().for_each(|(start, end)| {
let ip = &input[*start..*end];
matches.push((*start, *end, Token::IPv6(ip.to_string(), db.lookup(ip))));
});
// Sort matches by start position
matches.sort_by_key(|(start, _, _)| *start);
// Handle overlapping matches
let mut filtered_matches = Vec::new();
let mut i = 0;
while i < matches.len() {
let current = &matches[i];
let mut should_add = true;
let mut j = i + 1;
while j < matches.len()
&& Self::is_overlapping(current.0, current.1, matches[j].0, matches[j].1)
{
if !Self::should_keep_first(¤t.2, current.0, &matches[j].2, matches[j].0) {
should_add = false;
break;
}
j += 1;
}
if should_add {
filtered_matches.push(current.clone());
while j < matches.len()
&& Self::is_overlapping(current.0, current.1, matches[j].0, matches[j].1)
{
j += 1;
}
i = j;
} else {
i += 1;
}
}
let mut last_end = 0;
// Construct the final token sequence
for (start, end, token) in filtered_matches {
if start > last_end {
tokens.push(Token::Plain(input[last_end..start].to_string()));
}
tokens.push(token);
last_end = end;
}
// Add the remaining plain text
if last_end < input.len() {
tokens.push(Token::Plain(input[last_end..].to_string()));
}
NaliText::new(tokens)
}
}