rumdl_lib/utils/
regex_cache.rs1use fancy_regex::Regex as FancyRegex;
23use regex::Regex;
24use std::collections::HashMap;
25use std::sync::LazyLock;
26use std::sync::{Arc, Mutex};
27
28#[derive(Debug)]
30pub struct RegexCache {
31 cache: HashMap<String, Arc<Regex>>,
32 usage_stats: HashMap<String, u64>,
33}
34
35impl Default for RegexCache {
36 fn default() -> Self {
37 Self::new()
38 }
39}
40
41impl RegexCache {
42 pub fn new() -> Self {
43 Self {
44 cache: HashMap::new(),
45 usage_stats: HashMap::new(),
46 }
47 }
48
49 pub fn get_regex(&mut self, pattern: &str) -> Result<Arc<Regex>, regex::Error> {
51 if let Some(regex) = self.cache.get(pattern) {
52 *self.usage_stats.entry(pattern.to_string()).or_insert(0) += 1;
53 return Ok(regex.clone());
54 }
55
56 let regex = Arc::new(Regex::new(pattern)?);
57 self.cache.insert(pattern.to_string(), regex.clone());
58 *self.usage_stats.entry(pattern.to_string()).or_insert(0) += 1;
59 Ok(regex)
60 }
61
62 pub fn get_stats(&self) -> HashMap<String, u64> {
64 self.usage_stats.clone()
65 }
66
67 pub fn clear(&mut self) {
69 self.cache.clear();
70 self.usage_stats.clear();
71 }
72}
73
74static GLOBAL_REGEX_CACHE: LazyLock<Arc<Mutex<RegexCache>>> = LazyLock::new(|| Arc::new(Mutex::new(RegexCache::new())));
76
77pub fn get_cached_regex(pattern: &str) -> Result<Arc<Regex>, regex::Error> {
83 let mut cache = GLOBAL_REGEX_CACHE.lock().unwrap_or_else(|poisoned| {
84 let mut guard = poisoned.into_inner();
86 guard.clear();
87 guard
88 });
89 cache.get_regex(pattern)
90}
91
92pub fn get_cache_stats() -> HashMap<String, u64> {
96 match GLOBAL_REGEX_CACHE.lock() {
97 Ok(cache) => cache.get_stats(),
98 Err(_) => HashMap::new(),
99 }
100}
101
102#[macro_export]
121macro_rules! regex_lazy {
122 ($pattern:expr) => {{
123 static REGEX: LazyLock<regex::Regex> = LazyLock::new(|| regex::Regex::new($pattern).unwrap());
124 &*REGEX
125 }};
126}
127
128#[macro_export]
135macro_rules! regex_cached {
136 ($pattern:expr) => {{ $crate::utils::regex_cache::get_cached_regex($pattern).expect("Failed to compile regex") }};
137}
138
139pub use crate::regex_lazy;
141
142pub const URL_STANDARD_STR: &str = concat!(
176 r#"(?:https?|ftps?|ftp)://"#, r#"(?:"#,
178 r#"\[[0-9a-fA-F:%.\-a-zA-Z]+\]"#, r#"|"#,
180 r#"[^\s<>\[\]()\\'\"`/]+"#, r#")"#,
182 r#"(?::\d+)?"#, r#"(?:/[^\s<>\[\]\\'\"`]*)?"#, r#"(?:\?[^\s<>\[\]\\'\"`]*)?"#, r#"(?:#[^\s<>\[\]\\'\"`]*)?"#, );
187
188pub const URL_WWW_STR: &str = concat!(
201 r#"www\.(?:[a-zA-Z0-9][-a-zA-Z0-9]*\.)+[a-zA-Z]{2,}"#, r#"(?::\d+)?"#, r#"(?:/[^\s<>\[\]\\'\"`]*)?"#, r#"(?:\?[^\s<>\[\]\\'\"`]*)?"#, r#"(?:#[^\s<>\[\]\\'\"`]*)?"#, );
207
208pub const URL_IPV6_STR: &str = concat!(
213 r#"(?:https?|ftps?|ftp)://"#,
214 r#"\[[0-9a-fA-F:%.\-a-zA-Z]+\]"#, r#"(?::\d+)?"#, r#"(?:/[^\s<>\[\]\\'\"`]*)?"#, r#"(?:\?[^\s<>\[\]\\'\"`]*)?"#, r#"(?:#[^\s<>\[\]\\'\"`]*)?"#, );
220
221pub const XMPP_URI_STR: &str = r#"xmpp:[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:/[^\s<>\[\]\\'\"`]*)?"#;
230
231pub const URL_QUICK_CHECK_STR: &str = r#"(?:https?|ftps?|ftp|xmpp)://|xmpp:|@|www\."#;
237
238pub const URL_SIMPLE_STR: &str = r#"(?:https?|ftps?|ftp)://[^\s<>]+[^\s<>.,]"#;
244
245pub static URL_STANDARD_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(URL_STANDARD_STR).unwrap());
250
251pub static URL_WWW_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(URL_WWW_STR).unwrap());
254
255pub static URL_IPV6_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(URL_IPV6_STR).unwrap());
258
259pub static URL_QUICK_CHECK_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(URL_QUICK_CHECK_STR).unwrap());
262
263pub static URL_SIMPLE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(URL_SIMPLE_STR).unwrap());
266
267pub static URL_PATTERN: LazyLock<Regex> = LazyLock::new(|| URL_SIMPLE_REGEX.clone());
269
270pub static XMPP_URI_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(XMPP_URI_STR).unwrap());
273
274pub static ATX_HEADING_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(\s*)(#{1,6})(\s+|$)").unwrap());
276
277pub static UNORDERED_LIST_MARKER_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(\s*)([*+-])(\s+)").unwrap());
279pub static ORDERED_LIST_MARKER_REGEX: LazyLock<Regex> =
280 LazyLock::new(|| Regex::new(r"^(\s*)(\d+)([.)])(\s+)").unwrap());
281
282pub static ASTERISK_EMPHASIS: LazyLock<Regex> =
288 LazyLock::new(|| Regex::new(r"(?:^|[^*])\*(\s+[^*]+\s*|\s*[^*]+\s+)\*(?:[^*]|$)").unwrap());
289pub static UNDERSCORE_EMPHASIS: LazyLock<Regex> =
290 LazyLock::new(|| Regex::new(r"(?:^|[^_])_(\s+[^_]+\s*|\s*[^_]+\s+)_(?:[^_]|$)").unwrap());
291pub static DOUBLE_UNDERSCORE_EMPHASIS: LazyLock<Regex> =
292 LazyLock::new(|| Regex::new(r"(?:^|[^_])__(\s+[^_]+\s*|\s*[^_]+\s+)__(?:[^_]|$)").unwrap());
293pub static FENCED_CODE_BLOCK_START: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(\s*)```(?:[^`\r\n]*)$").unwrap());
295pub static FENCED_CODE_BLOCK_END: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(\s*)```\s*$").unwrap());
296
297pub static HTML_TAG_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"<([a-zA-Z][^>]*)>").unwrap());
299pub static HTML_TAG_QUICK_CHECK: LazyLock<Regex> = LazyLock::new(|| Regex::new("(?i)</?[a-zA-Z]").unwrap());
300
301pub static IMAGE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"!\[([^\]]*)\]\(([^)]+)\)").unwrap());
303
304pub static BLOCKQUOTE_PREFIX_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(\s*>+\s*)").unwrap());
306
307pub fn is_blank_in_blockquote_context(line: &str) -> bool {
330 if line.trim().is_empty() {
331 return true;
332 }
333 if let Some(m) = BLOCKQUOTE_PREFIX_RE.find(line) {
336 let remainder = &line[m.end()..];
337 is_blank_in_blockquote_context(remainder)
339 } else {
340 false
341 }
342}
343
344pub static IMAGE_REF_PATTERN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^!\[.*?\]\[.*?\]$").unwrap());
346pub static LINK_REF_PATTERN: LazyLock<Regex> =
347 LazyLock::new(|| Regex::new(r#"^\[.*?\]:\s*\S+(\s+["'(].*)?\s*$"#).unwrap());
348pub static ABBREVIATION: LazyLock<Regex> = LazyLock::new(|| {
349 Regex::new(r"\b(?:Mr|Mrs|Ms|Dr|Prof|Sr|Jr|vs|etc|i\.e|e\.g|Inc|Corp|Ltd|Co|St|Ave|Blvd|Rd|Ph\.D|M\.D|B\.A|M\.A|Ph\.D|U\.S|U\.K|U\.N|N\.Y|L\.A|D\.C)\.\s+[A-Z]").unwrap()
350});
351pub static LIST_ITEM: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^\s*\d+\.\s+").unwrap());
352
353pub static EMAIL_PATTERN: LazyLock<Regex> =
355 LazyLock::new(|| Regex::new(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}").unwrap());
356
357pub static REF_LINK_REGEX: LazyLock<FancyRegex> =
361 LazyLock::new(|| FancyRegex::new(r"(?<!\\)\[((?:[^\[\]\\]|\\.|\[[^\]]*\])*)\]\[([^\]]*)\]").unwrap());
362
363pub static SHORTCUT_REF_REGEX: LazyLock<FancyRegex> =
368 LazyLock::new(|| FancyRegex::new(r"(?<![\\)\]])\[((?:[^\[\]\\]|\\.|\[[^\]]*\])*)\](?!\s*[\[\(])").unwrap());
369
370pub static INLINE_LINK_FANCY_REGEX: LazyLock<FancyRegex> =
372 LazyLock::new(|| FancyRegex::new(r"(?<!\\)\[([^\]]+)\]\(([^)]+)\)").unwrap());
373
374pub static INLINE_IMAGE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"!\[([^\]]*)\]\(([^)]+)\)").unwrap());
376
377pub static LINKED_IMAGE_INLINE_INLINE: LazyLock<Regex> =
385 LazyLock::new(|| Regex::new(r"\[!\[([^\]]*)\]\(([^)]+)\)\]\(([^)]+)\)").unwrap());
386
387pub static LINKED_IMAGE_REF_INLINE: LazyLock<Regex> =
389 LazyLock::new(|| Regex::new(r"\[!\[([^\]]*)\]\[([^\]]*)\]\]\(([^)]+)\)").unwrap());
390
391pub static LINKED_IMAGE_INLINE_REF: LazyLock<Regex> =
393 LazyLock::new(|| Regex::new(r"\[!\[([^\]]*)\]\(([^)]+)\)\]\[([^\]]*)\]").unwrap());
394
395pub static LINKED_IMAGE_REF_REF: LazyLock<Regex> =
397 LazyLock::new(|| Regex::new(r"\[!\[([^\]]*)\]\[([^\]]*)\]\]\[([^\]]*)\]").unwrap());
398
399pub static REF_IMAGE_REGEX: LazyLock<Regex> =
401 LazyLock::new(|| Regex::new(r"!\[((?:[^\[\]\\]|\\.|\[[^\]]*\])*)\]\[([^\]]*)\]").unwrap());
402
403pub static FOOTNOTE_REF_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\[\^([^\]]+)\]").unwrap());
405
406pub static WIKI_LINK_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\[\[([^\]]+)\]\]").unwrap());
408
409pub static INLINE_MATH_REGEX: LazyLock<FancyRegex> =
411 LazyLock::new(|| FancyRegex::new(r"(?<!\$)\$(?!\$)([^\$]+)\$(?!\$)").unwrap());
412pub static DISPLAY_MATH_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\$\$([^\$]+)\$\$").unwrap());
413
414pub static EMOJI_SHORTCODE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r":([a-zA-Z0-9_+-]+):").unwrap());
416
417pub static HTML_TAG_PATTERN: LazyLock<Regex> =
419 LazyLock::new(|| Regex::new(r"</?[a-zA-Z][^>]*>|<[a-zA-Z][^>]*/\s*>").unwrap());
420
421pub static HTML_ENTITY_REGEX: LazyLock<Regex> =
423 LazyLock::new(|| Regex::new(r"&[a-zA-Z][a-zA-Z0-9]*;|&#\d+;|&#x[0-9a-fA-F]+;").unwrap());
424
425pub static HUGO_SHORTCODE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\{\{[<%][\s\S]*?[%>]\}\}").unwrap());
429
430pub static HTML_HEADING_PATTERN: LazyLock<FancyRegex> =
433 LazyLock::new(|| FancyRegex::new(r"^\s*<h([1-6])(?:\s[^>]*)?>.*</h\1>\s*$").unwrap());
434
435pub fn escape_regex(s: &str) -> String {
437 let mut result = String::with_capacity(s.len() * 2);
438
439 for c in s.chars() {
440 if matches!(
442 c,
443 '.' | '+' | '*' | '?' | '^' | '$' | '(' | ')' | '[' | ']' | '{' | '}' | '|' | '\\'
444 ) {
445 result.push('\\');
446 }
447 result.push(c);
448 }
449
450 result
451}
452
453#[cfg(test)]
454mod tests {
455 use super::*;
456
457 #[test]
458 fn test_regex_cache_new() {
459 let cache = RegexCache::new();
460 assert!(cache.cache.is_empty());
461 assert!(cache.usage_stats.is_empty());
462 }
463
464 #[test]
465 fn test_regex_cache_default() {
466 let cache = RegexCache::default();
467 assert!(cache.cache.is_empty());
468 assert!(cache.usage_stats.is_empty());
469 }
470
471 #[test]
472 fn test_get_regex_compilation() {
473 let mut cache = RegexCache::new();
474
475 let regex1 = cache.get_regex(r"\d+").unwrap();
477 assert_eq!(cache.cache.len(), 1);
478 assert_eq!(cache.usage_stats.get(r"\d+"), Some(&1));
479
480 let regex2 = cache.get_regex(r"\d+").unwrap();
482 assert_eq!(cache.cache.len(), 1);
483 assert_eq!(cache.usage_stats.get(r"\d+"), Some(&2));
484
485 assert!(Arc::ptr_eq(®ex1, ®ex2));
487 }
488
489 #[test]
490 fn test_get_regex_invalid_pattern() {
491 let mut cache = RegexCache::new();
492 let result = cache.get_regex(r"[unterminated");
493 assert!(result.is_err());
494 assert!(cache.cache.is_empty());
495 }
496
497 #[test]
498 fn test_get_stats() {
499 let mut cache = RegexCache::new();
500
501 let _ = cache.get_regex(r"\d+").unwrap();
503 let _ = cache.get_regex(r"\d+").unwrap();
504 let _ = cache.get_regex(r"\w+").unwrap();
505
506 let stats = cache.get_stats();
507 assert_eq!(stats.get(r"\d+"), Some(&2));
508 assert_eq!(stats.get(r"\w+"), Some(&1));
509 }
510
511 #[test]
512 fn test_clear_cache() {
513 let mut cache = RegexCache::new();
514
515 let _ = cache.get_regex(r"\d+").unwrap();
517
518 assert!(!cache.cache.is_empty());
519 assert!(!cache.usage_stats.is_empty());
520
521 cache.clear();
523
524 assert!(cache.cache.is_empty());
525 assert!(cache.usage_stats.is_empty());
526 }
527
528 #[test]
529 fn test_global_cache_functions() {
530 let regex1 = get_cached_regex(r"\d{3}").unwrap();
532 let regex2 = get_cached_regex(r"\d{3}").unwrap();
533 assert!(Arc::ptr_eq(®ex1, ®ex2));
534
535 let stats = get_cache_stats();
537 assert!(stats.contains_key(r"\d{3}"));
538 }
539
540 #[test]
541 fn test_regex_lazy_macro() {
542 let re = regex_lazy!(r"^test.*end$");
543 assert!(re.is_match("test something end"));
544 assert!(!re.is_match("test something"));
545
546 let re2 = regex_lazy!(r"^start.*finish$");
550 assert!(re2.is_match("start and finish"));
551 assert!(!re2.is_match("start without end"));
552 }
553
554 #[test]
555 fn test_escape_regex() {
556 assert_eq!(escape_regex("a.b"), "a\\.b");
557 assert_eq!(escape_regex("a+b*c"), "a\\+b\\*c");
558 assert_eq!(escape_regex("(test)"), "\\(test\\)");
559 assert_eq!(escape_regex("[a-z]"), "\\[a-z\\]");
560 assert_eq!(escape_regex("normal text"), "normal text");
561
562 assert_eq!(escape_regex(".$^{[(|)*+?\\"), "\\.\\$\\^\\{\\[\\(\\|\\)\\*\\+\\?\\\\");
564
565 assert_eq!(escape_regex(""), "");
567
568 assert_eq!(escape_regex("test.com/path?query=1"), "test\\.com/path\\?query=1");
570 }
571
572 #[test]
573 fn test_static_regex_patterns() {
574 assert!(URL_SIMPLE_REGEX.is_match("https://example.com"));
576 assert!(URL_SIMPLE_REGEX.is_match("http://test.org/path"));
577 assert!(URL_SIMPLE_REGEX.is_match("ftp://files.com"));
578 assert!(!URL_SIMPLE_REGEX.is_match("not a url"));
579
580 assert!(ATX_HEADING_REGEX.is_match("# Heading"));
582 assert!(ATX_HEADING_REGEX.is_match(" ## Indented"));
583 assert!(ATX_HEADING_REGEX.is_match("### "));
584 assert!(!ATX_HEADING_REGEX.is_match("Not a heading"));
585
586 assert!(UNORDERED_LIST_MARKER_REGEX.is_match("* Item"));
588 assert!(UNORDERED_LIST_MARKER_REGEX.is_match("- Item"));
589 assert!(UNORDERED_LIST_MARKER_REGEX.is_match("+ Item"));
590 assert!(ORDERED_LIST_MARKER_REGEX.is_match("1. Item"));
591 assert!(ORDERED_LIST_MARKER_REGEX.is_match("99. Item"));
592
593 assert!(HTML_TAG_REGEX.is_match("<div>"));
595 assert!(HTML_TAG_REGEX.is_match("<span class='test'>"));
596
597 assert!(BLOCKQUOTE_PREFIX_RE.is_match("> Quote"));
599 assert!(BLOCKQUOTE_PREFIX_RE.is_match(" > Indented quote"));
600 assert!(BLOCKQUOTE_PREFIX_RE.is_match(">> Nested"));
601 }
602
603 #[test]
604 fn test_thread_safety() {
605 use std::thread;
606
607 let handles: Vec<_> = (0..10)
608 .map(|i| {
609 thread::spawn(move || {
610 let pattern = format!(r"\d{{{i}}}");
611 let regex = get_cached_regex(&pattern).unwrap();
612 assert!(regex.is_match(&"1".repeat(i)));
613 })
614 })
615 .collect();
616
617 for handle in handles {
618 handle.join().unwrap();
619 }
620 }
621
622 #[test]
627 fn test_url_standard_basic() {
628 assert!(URL_STANDARD_REGEX.is_match("https://example.com"));
630 assert!(URL_STANDARD_REGEX.is_match("http://example.com"));
631 assert!(URL_STANDARD_REGEX.is_match("https://example.com/"));
632 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path"));
633 assert!(URL_STANDARD_REGEX.is_match("ftp://files.example.com"));
634 assert!(URL_STANDARD_REGEX.is_match("ftps://secure.example.com"));
635
636 assert!(!URL_STANDARD_REGEX.is_match("not a url"));
638 assert!(!URL_STANDARD_REGEX.is_match("example.com"));
639 assert!(!URL_STANDARD_REGEX.is_match("www.example.com"));
640 }
641
642 #[test]
643 fn test_url_standard_with_path() {
644 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path/to/page"));
645 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path/to/page.html"));
646 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path/to/page/"));
647 }
648
649 #[test]
650 fn test_url_standard_with_query() {
651 assert!(URL_STANDARD_REGEX.is_match("https://example.com?query=value"));
652 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path?query=value"));
653 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path?a=1&b=2"));
654 }
655
656 #[test]
657 fn test_url_standard_with_fragment() {
658 assert!(URL_STANDARD_REGEX.is_match("https://example.com#section"));
659 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path#section"));
660 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path?query=value#section"));
661 }
662
663 #[test]
664 fn test_url_standard_with_port() {
665 assert!(URL_STANDARD_REGEX.is_match("https://example.com:8080"));
666 assert!(URL_STANDARD_REGEX.is_match("https://example.com:443/path"));
667 assert!(URL_STANDARD_REGEX.is_match("http://localhost:3000"));
668 assert!(URL_STANDARD_REGEX.is_match("https://192.168.1.1:8080/path"));
669 }
670
671 #[test]
672 fn test_url_standard_wikipedia_style_parentheses() {
673 let url = "https://en.wikipedia.org/wiki/Rust_(programming_language)";
675 assert!(URL_STANDARD_REGEX.is_match(url));
676
677 let cap = URL_STANDARD_REGEX.find(url).unwrap();
679 assert_eq!(cap.as_str(), url);
680
681 let url2 = "https://example.com/path_(foo)_(bar)";
683 let cap2 = URL_STANDARD_REGEX.find(url2).unwrap();
684 assert_eq!(cap2.as_str(), url2);
685 }
686
687 #[test]
688 fn test_url_standard_ipv6() {
689 assert!(URL_STANDARD_REGEX.is_match("https://[::1]/path"));
691 assert!(URL_STANDARD_REGEX.is_match("https://[2001:db8::1]:8080/path"));
692 assert!(URL_STANDARD_REGEX.is_match("http://[fe80::1%eth0]/"));
693 }
694
695 #[test]
696 fn test_url_www_basic() {
697 assert!(URL_WWW_REGEX.is_match("www.example.com"));
699 assert!(URL_WWW_REGEX.is_match("www.example.co.uk"));
700 assert!(URL_WWW_REGEX.is_match("www.sub.example.com"));
701
702 assert!(!URL_WWW_REGEX.is_match("example.com"));
704
705 assert!(URL_WWW_REGEX.is_match("https://www.example.com"));
709 }
710
711 #[test]
712 fn test_url_www_with_path() {
713 assert!(URL_WWW_REGEX.is_match("www.example.com/path"));
714 assert!(URL_WWW_REGEX.is_match("www.example.com/path/to/page"));
715 assert!(URL_WWW_REGEX.is_match("www.example.com/path_(with_parens)"));
716 }
717
718 #[test]
719 fn test_url_ipv6_basic() {
720 assert!(URL_IPV6_REGEX.is_match("https://[::1]/"));
722 assert!(URL_IPV6_REGEX.is_match("http://[2001:db8::1]/path"));
723 assert!(URL_IPV6_REGEX.is_match("https://[fe80::1]:8080/path"));
724 assert!(URL_IPV6_REGEX.is_match("ftp://[::ffff:192.168.1.1]/file"));
725 }
726
727 #[test]
728 fn test_url_ipv6_with_zone_id() {
729 assert!(URL_IPV6_REGEX.is_match("https://[fe80::1%eth0]/path"));
731 assert!(URL_IPV6_REGEX.is_match("http://[fe80::1%25eth0]:8080/"));
732 }
733
734 #[test]
735 fn test_url_simple_detection() {
736 assert!(URL_SIMPLE_REGEX.is_match("https://example.com"));
738 assert!(URL_SIMPLE_REGEX.is_match("http://test.org/path"));
739 assert!(URL_SIMPLE_REGEX.is_match("ftp://files.com/file.zip"));
740 assert!(!URL_SIMPLE_REGEX.is_match("not a url"));
741 }
742
743 #[test]
744 fn test_url_quick_check() {
745 assert!(URL_QUICK_CHECK_REGEX.is_match("https://example.com"));
747 assert!(URL_QUICK_CHECK_REGEX.is_match("http://example.com"));
748 assert!(URL_QUICK_CHECK_REGEX.is_match("ftp://files.com"));
749 assert!(URL_QUICK_CHECK_REGEX.is_match("www.example.com"));
750 assert!(URL_QUICK_CHECK_REGEX.is_match("user@example.com"));
751 assert!(!URL_QUICK_CHECK_REGEX.is_match("just plain text"));
752 }
753
754 #[test]
755 fn test_url_edge_cases() {
756 let url = "https://example.com/path";
758 assert!(URL_STANDARD_REGEX.is_match(url));
759
760 let text = "Check https://example.com, it's great!";
763 let cap = URL_STANDARD_REGEX.find(text).unwrap();
764 assert!(cap.as_str().ends_with(','));
766
767 let text2 = "See <https://example.com> for more";
769 assert!(URL_STANDARD_REGEX.is_match(text2));
770
771 let cap2 = URL_STANDARD_REGEX.find(text2).unwrap();
773 assert!(!cap2.as_str().contains('>'));
774 }
775
776 #[test]
777 fn test_url_with_complex_paths() {
778 let urls = [
780 "https://github.com/owner/repo/blob/main/src/file.rs#L123",
781 "https://docs.example.com/api/v2/endpoint?format=json&page=1",
782 "https://cdn.example.com/assets/images/logo.png?v=2023",
783 "https://search.example.com/results?q=test+query&filter=all",
784 ];
785
786 for url in urls {
787 assert!(URL_STANDARD_REGEX.is_match(url), "Should match: {url}");
788 }
789 }
790
791 #[test]
792 fn test_url_pattern_strings_are_valid() {
793 assert!(URL_STANDARD_REGEX.is_match("https://example.com"));
795 assert!(URL_WWW_REGEX.is_match("www.example.com"));
796 assert!(URL_IPV6_REGEX.is_match("https://[::1]/"));
797 assert!(URL_QUICK_CHECK_REGEX.is_match("https://example.com"));
798 assert!(URL_SIMPLE_REGEX.is_match("https://example.com"));
799 }
800
801 #[test]
808 fn test_is_blank_in_blockquote_context_regular_blanks() {
809 assert!(is_blank_in_blockquote_context(""));
811 assert!(is_blank_in_blockquote_context(" "));
812 assert!(is_blank_in_blockquote_context("\t"));
813 assert!(is_blank_in_blockquote_context(" \t "));
814 }
815
816 #[test]
817 fn test_is_blank_in_blockquote_context_blockquote_blanks() {
818 assert!(is_blank_in_blockquote_context(">"));
820 assert!(is_blank_in_blockquote_context("> "));
821 assert!(is_blank_in_blockquote_context("> "));
822 assert!(is_blank_in_blockquote_context(">>"));
823 assert!(is_blank_in_blockquote_context(">> "));
824 assert!(is_blank_in_blockquote_context(">>>"));
825 assert!(is_blank_in_blockquote_context(">>> "));
826 }
827
828 #[test]
829 fn test_is_blank_in_blockquote_context_spaced_nested() {
830 assert!(is_blank_in_blockquote_context("> > "));
832 assert!(is_blank_in_blockquote_context("> > > "));
833 assert!(is_blank_in_blockquote_context("> > "));
834 }
835
836 #[test]
837 fn test_is_blank_in_blockquote_context_with_leading_space() {
838 assert!(is_blank_in_blockquote_context(" >"));
840 assert!(is_blank_in_blockquote_context(" > "));
841 assert!(is_blank_in_blockquote_context(" >>"));
842 }
843
844 #[test]
845 fn test_is_blank_in_blockquote_context_not_blank() {
846 assert!(!is_blank_in_blockquote_context("text"));
848 assert!(!is_blank_in_blockquote_context("> text"));
849 assert!(!is_blank_in_blockquote_context(">> text"));
850 assert!(!is_blank_in_blockquote_context("> | table |"));
851 assert!(!is_blank_in_blockquote_context("| table |"));
852 assert!(!is_blank_in_blockquote_context("> # Heading"));
853 assert!(!is_blank_in_blockquote_context(">text")); }
855
856 #[test]
857 fn test_is_blank_in_blockquote_context_edge_cases() {
858 assert!(!is_blank_in_blockquote_context(">a")); assert!(!is_blank_in_blockquote_context("> a")); assert!(is_blank_in_blockquote_context("> ")); assert!(!is_blank_in_blockquote_context("> text")); }
864}