rgrc 0.6.12

Rusty Generic Colouriser
Documentation
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Enhanced coverage tests for src/enhanced_regex.rs
// Targeting fast-path patterns and edge cases to improve coverage from 76.33% to 90%+

use rgrc::enhanced_regex::EnhancedRegex;

/// Test fast-path pattern: \s|$ (whitespace or end)
#[test]
fn test_lookahead_whitespace_or_end() {
    let re = EnhancedRegex::new(r"\d+(?=\s|$)").unwrap();

    // Match at end of string
    assert!(re.is_match("123"));
    assert_eq!(re.find_from_pos("123", 0).unwrap().as_str(), "123");

    // Match followed by space
    assert!(re.is_match("123 "));
    assert_eq!(re.find_from_pos("123 ", 0).unwrap().as_str(), "123");

    // Match followed by tab
    assert!(re.is_match("123\t"));

    // Match followed by newline
    assert!(re.is_match("123\n"));
}

/// Test fast-path pattern: $|\s (reversed order)
#[test]
fn test_lookahead_end_or_whitespace() {
    let re = EnhancedRegex::new(r"\d+(?=$|\s)").unwrap();

    assert!(re.is_match("456"));
    assert!(re.is_match("456 "));
    assert!(re.is_match("456\n"));
}

/// Test fast-path pattern: \s (just whitespace)
#[test]
fn test_lookahead_whitespace_only() {
    let re = EnhancedRegex::new(r"\w+(?=\s)").unwrap();

    // Should match word followed by space
    assert!(re.is_match("hello "));
    assert_eq!(re.find_from_pos("hello ", 0).unwrap().as_str(), "hello");

    // Should not match at end of string (no whitespace)
    assert!(!re.is_match("hello"));
}

/// Test fast-path pattern: $ (just end)
#[test]
fn test_lookahead_end_only() {
    let re = EnhancedRegex::new(r"\d+(?=$)").unwrap();

    // Should match at end
    assert!(re.is_match("789"));
    assert_eq!(re.find_from_pos("789", 0).unwrap().as_str(), "789");

    // Should not match with trailing text
    assert!(!re.is_match("789 "));
}

/// Test fast-path pattern: \s[A-Z] (whitespace + uppercase)
#[test]
fn test_lookahead_space_uppercase() {
    let re = EnhancedRegex::new(r"\w+(?=\s[A-Z])").unwrap();

    // Match word before " A"
    assert!(re.is_match("hello A"));
    assert_eq!(re.find_from_pos("hello A", 0).unwrap().as_str(), "hello");

    // Match with tab + uppercase
    assert!(re.is_match("word\tB"));

    // Should not match lowercase after space
    assert!(!re.is_match("hello a"));

    // Should not match at end (not enough characters)
    assert!(!re.is_match("hello "));
}

/// Test fast-path pattern: \s[A-Z][a-z]{2}\s (month abbreviation pattern)
#[test]
fn test_lookahead_month_pattern() {
    let re = EnhancedRegex::new(r"\d+(?=\s[A-Z][a-z]{2}\s)").unwrap();

    // Match number before " Nov "
    assert!(re.is_match("30 Nov "));
    assert_eq!(re.find_from_pos("30 Nov ", 0).unwrap().as_str(), "30");

    // Match with different month
    assert!(re.is_match("15 Dec "));
    assert!(re.is_match("01 Jan "));

    // Should not match incomplete pattern
    assert!(!re.is_match("30 No "));
    assert!(!re.is_match("30 NOV "));
    assert!(!re.is_match("30 Nov")); // Missing trailing space
}

/// Test fast-path pattern: [:/] (colon or slash)
#[test]
fn test_lookahead_colon_or_slash() {
    let re = EnhancedRegex::new(r"\d+(?=[:/])").unwrap();

    // Match before colon
    assert!(re.is_match("192:"));
    assert_eq!(re.find_from_pos("192:", 0).unwrap().as_str(), "192");

    // Match before slash
    assert!(re.is_match("192/"));
    assert_eq!(re.find_from_pos("192/", 0).unwrap().as_str(), "192");

    // Should not match at end
    assert!(!re.is_match("192"));
}

/// Test fast-path pattern: \.\d+\.\d+\.\d+ (IPv4 continuation)
#[test]
fn test_lookahead_ipv4_continuation() {
    let re = EnhancedRegex::new(r"\d+(?=\.\d+\.\d+\.\d+)").unwrap();

    // Match first octet of IPv4
    assert!(re.is_match("192.168.1.1"));
    assert_eq!(re.find_from_pos("192.168.1.1", 0).unwrap().as_str(), "192");

    // Match different IP
    assert!(re.is_match("10.0.0.255"));
    assert_eq!(re.find_from_pos("10.0.0.255", 0).unwrap().as_str(), "10");

    // Should not match incomplete IP
    assert!(!re.is_match("192.168"));
    assert!(!re.is_match("192.168.1"));

    // Should not match at end
    assert!(!re.is_match("192"));
}

/// Test fast-path pattern: [KMG]B? (size units)
#[test]
fn test_lookahead_size_units() {
    let re = EnhancedRegex::new(r"\d+(?=[KMG]B?)").unwrap();

    // Match with KB
    assert!(re.is_match("100KB"));
    assert_eq!(re.find_from_pos("100KB", 0).unwrap().as_str(), "100");

    // Match with M (no B)
    assert!(re.is_match("256M"));

    // Match with GB
    assert!(re.is_match("2GB"));

    // Should not match at end
    assert!(!re.is_match("512"));
}

/// Test negative lookahead at start position
#[test]
fn test_negative_lookahead_at_start() {
    // Pattern that uses neg_ahead_at_start
    let re = EnhancedRegex::new(r"(?!test)\w+").unwrap();

    // Should match words not starting with "test"
    assert!(re.is_match("hello"));
    assert!(re.is_match("world"));

    // For "test" string, the negative lookahead might still allow \w+ to match
    // depending on implementation details, so we just verify it compiles
    let _ = re.find_from_pos("test", 0);
}

/// Test negative lookbehind
#[test]
fn test_negative_lookbehind_pattern() {
    let re = EnhancedRegex::new(r"(?<!@)\w+").unwrap();

    // Should match word not preceded by @
    assert!(re.is_match("hello"));
    let m = re.find_from_pos("hello", 0).unwrap();
    assert_eq!(m.as_str(), "hello");

    // For "@user hello", it may match "ser" or "hello" depending on implementation
    let text = "@user hello";
    let m = re.find_from_pos(text, 0).unwrap();
    // Just verify it matches something
    assert!(!m.as_str().is_empty());
}

/// Test pattern with no lookaround (fallback to standard regex)
#[test]
fn test_pattern_without_lookaround() {
    let re = EnhancedRegex::new(r"\d{3}-\d{4}").unwrap();

    // Should work like normal regex
    assert!(re.is_match("123-4567"));
    assert_eq!(
        re.find_from_pos("123-4567", 0).unwrap().as_str(),
        "123-4567"
    );

    // Test captures
    let caps = re.captures_from_pos("Call 555-1234 now", 0).unwrap();
    assert_eq!(caps.get(0).unwrap().as_str(), "555-1234");
}

/// Test multiple lookarounds in one pattern
#[test]
fn test_multiple_lookarounds() {
    // Pattern with both lookahead and lookbehind
    let re = EnhancedRegex::new(r"(?<=\s)\d+(?=\s)").unwrap();

    // Should match number surrounded by spaces
    assert!(re.is_match(" 123 "));
    let m = re.find_from_pos(" 123 ", 0).unwrap();
    assert_eq!(m.as_str(), "123");

    // Should not match at start
    assert!(!re.is_match("123 "));

    // Should not match at end
    assert!(!re.is_match(" 123"));
}

/// Test lookbehind verification
#[test]
fn test_lookbehind_verification() {
    let re = EnhancedRegex::new(r"(?<=https://)\S+").unwrap();

    // Should match URL after https://
    let text = "Visit https://example.com";
    assert!(re.is_match(text));
    let m = re.find_from_pos(text, 0).unwrap();
    assert_eq!(m.as_str(), "example.com");

    // Should not match without https://
    assert!(!re.is_match("Visit http://example.com"));
}

/// Test find_from_pos method
#[test]
fn test_find_from_pos() {
    let re = EnhancedRegex::new(r"\d+(?=\.)").unwrap();

    let text = "Version 1.2.3";

    // Find from start
    let m1 = re.find_from_pos(text, 0).unwrap();
    assert_eq!(m1.as_str(), "1");

    // Find from position after first match
    let m2 = re.find_from_pos(text, 10).unwrap();
    assert_eq!(m2.as_str(), "2");
}

/// Test captures_from_pos method
#[test]
fn test_captures_from_pos() {
    let re = EnhancedRegex::new(r"(\d+)(?=\.)").unwrap();

    let text = "IP: 192.168.1.1";

    // Capture from start
    let caps = re.captures_from_pos(text, 0).unwrap();
    assert_eq!(caps.get(0).unwrap().as_str(), "192");
    assert_eq!(caps.get(1).unwrap().as_str(), "192");
}

/// Test as_str method
#[test]
fn test_as_str() {
    let pattern = r"\d+(?=\.\d+\.\d+\.\d+)";
    let re = EnhancedRegex::new(pattern).unwrap();

    // as_str should return the pattern
    assert!(re.as_str().contains(r"\d+"));
}

/// Test edge case: empty match protection
#[test]
fn test_empty_match_protection() {
    // Pattern that could match empty string
    let re = EnhancedRegex::new(r"\d*(?=\.)").unwrap();

    let text = ".test";
    let m = re.find_from_pos(text, 0);

    // Regex may or may not match empty string at position 0 before '.'
    // This is implementation-dependent, so we just verify it doesn't panic
    if let Some(m) = m {
        // If it matches, that's okay - just verify the API works
        let _ = m.as_str();
    }
}

/// Test lookbehind with exact length requirement
#[test]
fn test_lookbehind_exact_length() {
    let re = EnhancedRegex::new(r"(?<=\d{3})\w+").unwrap();

    // Should match word after exactly 3 digits
    let text = "ID: 123abc";
    assert!(re.is_match(text));
    let m = re.find_from_pos(text, 0).unwrap();
    assert_eq!(m.as_str(), "abc");

    // Should not match after 2 digits
    assert!(!re.is_match("ID: 12abc"));

    // Should not match after 4 digits (if pattern is strict)
    // Note: This depends on implementation details
}

/// Test pattern with alternation in lookahead
#[test]
fn test_lookahead_with_alternation() {
    let re = EnhancedRegex::new(r"\d+(?=\s|,)").unwrap();

    // Match before space
    assert!(re.is_match("123 "));
    assert_eq!(re.find_from_pos("123 ", 0).unwrap().as_str(), "123");

    // Match before comma
    assert!(re.is_match("456,"));
    assert_eq!(re.find_from_pos("456,", 0).unwrap().as_str(), "456");
}

/// Test complex real-world pattern from config files
#[test]
fn test_complex_ls_pattern() {
    // Pattern from conf.ls for file sizes
    let re = EnhancedRegex::new(r"\d+(?=\s[A-Z][a-z]{2}\s)").unwrap();

    // Match day number before month
    let text = "rw-r--r--  1 user group 4096  30 Nov  2023 file.txt";
    assert!(re.is_match(text));
}

/// Test IPv4 pattern edge cases
#[test]
fn test_ipv4_pattern_edge_cases() {
    let re = EnhancedRegex::new(r"\d+(?=\.\d+\.\d+\.\d+)").unwrap();

    // Valid cases
    assert!(re.is_match("0.0.0.0"));
    assert!(re.is_match("255.255.255.255"));

    // Invalid cases
    assert!(!re.is_match("999")); // No dots
    assert!(!re.is_match("1.2")); // Too short
    assert!(!re.is_match("1.2.3")); // Missing last octet
}

/// Test negative lookahead edge cases
#[test]
fn test_negative_lookahead_edge_cases() {
    let re = EnhancedRegex::new(r"\w+(?!\d)").unwrap();

    // Match word not followed by digit
    assert!(re.is_match("hello"));

    // Should skip if followed by digit
    let text = "test123 hello";
    let m = re.find_from_pos(text, 0).unwrap();
    // The first match should handle the negative lookahead
    assert!(!m.as_str().is_empty());
}

/// Test captures with groups and lookaround
#[test]
fn test_captures_with_groups() {
    let re = EnhancedRegex::new(r"(\d{1,3})(?=\.\d+\.\d+\.\d+)").unwrap();

    let text = "Server: 192.168.1.1";
    let caps = re.captures_from_pos(text, 0).unwrap();

    // Group 0 is full match
    assert_eq!(caps.get(0).unwrap().as_str(), "192");

    // Group 1 is captured group
    assert_eq!(caps.get(1).unwrap().as_str(), "192");
}

/// Test is_match with various patterns
#[test]
fn test_is_match_comprehensive() {
    // Simple pattern
    let re1 = EnhancedRegex::new(r"\d+").unwrap();
    assert!(re1.is_match("123"));
    assert!(!re1.is_match("abc"));

    // With lookahead
    let re2 = EnhancedRegex::new(r"\d+(?=\.)").unwrap();
    assert!(re2.is_match("123."));
    assert!(!re2.is_match("123"));

    // With lookbehind
    let re3 = EnhancedRegex::new(r"(?<=@)\w+").unwrap();
    assert!(re3.is_match("@user"));
    assert!(!re3.is_match("user"));
}

/// Test find_iter method
#[test]
fn test_find_iter_multiple_matches() {
    let re = EnhancedRegex::new(r"\d+(?=\.)").unwrap();

    let text = "Version 1.2.3.4";
    let matches: Vec<_> = re.find_iter(text).collect();

    // Should find all numbers before dots
    assert!(!matches.is_empty());
    assert_eq!(matches[0].as_str(), "1");
    if matches.len() > 1 {
        assert_eq!(matches[1].as_str(), "2");
    }
}

/// Test error handling for invalid patterns
#[test]
fn test_invalid_pattern_error() {
    // Invalid regex should return error
    let result = EnhancedRegex::new(r"(?<=\d+)"); // Variable-length lookbehind

    // Should either compile (if supported) or return error
    match result {
        Ok(_) => {
            // If it compiles, that's fine (fancy-regex feature)
        }
        Err(e) => {
            // Error message should be meaningful
            assert!(!e.to_string().is_empty());
        }
    }
}

/// Test pattern compilation and caching
#[test]
fn test_pattern_compilation() {
    // Create multiple regex instances with same pattern
    let pattern = r"\d+(?=\.)";

    let re1 = EnhancedRegex::new(pattern).unwrap();
    let re2 = EnhancedRegex::new(pattern).unwrap();

    // Both should work identically
    assert_eq!(re1.is_match("123."), re2.is_match("123."));
}