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_COMMENT_PATTERN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"<!--[\s\S]*?-->").unwrap());
432
433pub static HTML_HEADING_PATTERN: LazyLock<FancyRegex> =
436 LazyLock::new(|| FancyRegex::new(r"^\s*<h([1-6])(?:\s[^>]*)?>.*</h\1>\s*$").unwrap());
437
438pub fn escape_regex(s: &str) -> String {
440 let mut result = String::with_capacity(s.len() * 2);
441
442 for c in s.chars() {
443 if matches!(
445 c,
446 '.' | '+' | '*' | '?' | '^' | '$' | '(' | ')' | '[' | ']' | '{' | '}' | '|' | '\\'
447 ) {
448 result.push('\\');
449 }
450 result.push(c);
451 }
452
453 result
454}
455
456#[cfg(test)]
457mod tests {
458 use super::*;
459
460 #[test]
461 fn test_regex_cache_new() {
462 let cache = RegexCache::new();
463 assert!(cache.cache.is_empty());
464 assert!(cache.usage_stats.is_empty());
465 }
466
467 #[test]
468 fn test_regex_cache_default() {
469 let cache = RegexCache::default();
470 assert!(cache.cache.is_empty());
471 assert!(cache.usage_stats.is_empty());
472 }
473
474 #[test]
475 fn test_get_regex_compilation() {
476 let mut cache = RegexCache::new();
477
478 let regex1 = cache.get_regex(r"\d+").unwrap();
480 assert_eq!(cache.cache.len(), 1);
481 assert_eq!(cache.usage_stats.get(r"\d+"), Some(&1));
482
483 let regex2 = cache.get_regex(r"\d+").unwrap();
485 assert_eq!(cache.cache.len(), 1);
486 assert_eq!(cache.usage_stats.get(r"\d+"), Some(&2));
487
488 assert!(Arc::ptr_eq(®ex1, ®ex2));
490 }
491
492 #[test]
493 fn test_get_regex_invalid_pattern() {
494 let mut cache = RegexCache::new();
495 let result = cache.get_regex(r"[unterminated");
496 assert!(result.is_err());
497 assert!(cache.cache.is_empty());
498 }
499
500 #[test]
501 fn test_get_stats() {
502 let mut cache = RegexCache::new();
503
504 let _ = cache.get_regex(r"\d+").unwrap();
506 let _ = cache.get_regex(r"\d+").unwrap();
507 let _ = cache.get_regex(r"\w+").unwrap();
508
509 let stats = cache.get_stats();
510 assert_eq!(stats.get(r"\d+"), Some(&2));
511 assert_eq!(stats.get(r"\w+"), Some(&1));
512 }
513
514 #[test]
515 fn test_clear_cache() {
516 let mut cache = RegexCache::new();
517
518 let _ = cache.get_regex(r"\d+").unwrap();
520
521 assert!(!cache.cache.is_empty());
522 assert!(!cache.usage_stats.is_empty());
523
524 cache.clear();
526
527 assert!(cache.cache.is_empty());
528 assert!(cache.usage_stats.is_empty());
529 }
530
531 #[test]
532 fn test_global_cache_functions() {
533 let regex1 = get_cached_regex(r"\d{3}").unwrap();
535 let regex2 = get_cached_regex(r"\d{3}").unwrap();
536 assert!(Arc::ptr_eq(®ex1, ®ex2));
537
538 let stats = get_cache_stats();
540 assert!(stats.contains_key(r"\d{3}"));
541 }
542
543 #[test]
544 fn test_regex_lazy_macro() {
545 let re = regex_lazy!(r"^test.*end$");
546 assert!(re.is_match("test something end"));
547 assert!(!re.is_match("test something"));
548
549 let re2 = regex_lazy!(r"^start.*finish$");
553 assert!(re2.is_match("start and finish"));
554 assert!(!re2.is_match("start without end"));
555 }
556
557 #[test]
558 fn test_escape_regex() {
559 assert_eq!(escape_regex("a.b"), "a\\.b");
560 assert_eq!(escape_regex("a+b*c"), "a\\+b\\*c");
561 assert_eq!(escape_regex("(test)"), "\\(test\\)");
562 assert_eq!(escape_regex("[a-z]"), "\\[a-z\\]");
563 assert_eq!(escape_regex("normal text"), "normal text");
564
565 assert_eq!(escape_regex(".$^{[(|)*+?\\"), "\\.\\$\\^\\{\\[\\(\\|\\)\\*\\+\\?\\\\");
567
568 assert_eq!(escape_regex(""), "");
570
571 assert_eq!(escape_regex("test.com/path?query=1"), "test\\.com/path\\?query=1");
573 }
574
575 #[test]
576 fn test_static_regex_patterns() {
577 assert!(URL_SIMPLE_REGEX.is_match("https://example.com"));
579 assert!(URL_SIMPLE_REGEX.is_match("http://test.org/path"));
580 assert!(URL_SIMPLE_REGEX.is_match("ftp://files.com"));
581 assert!(!URL_SIMPLE_REGEX.is_match("not a url"));
582
583 assert!(ATX_HEADING_REGEX.is_match("# Heading"));
585 assert!(ATX_HEADING_REGEX.is_match(" ## Indented"));
586 assert!(ATX_HEADING_REGEX.is_match("### "));
587 assert!(!ATX_HEADING_REGEX.is_match("Not a heading"));
588
589 assert!(UNORDERED_LIST_MARKER_REGEX.is_match("* Item"));
591 assert!(UNORDERED_LIST_MARKER_REGEX.is_match("- Item"));
592 assert!(UNORDERED_LIST_MARKER_REGEX.is_match("+ Item"));
593 assert!(ORDERED_LIST_MARKER_REGEX.is_match("1. Item"));
594 assert!(ORDERED_LIST_MARKER_REGEX.is_match("99. Item"));
595
596 assert!(HTML_TAG_REGEX.is_match("<div>"));
598 assert!(HTML_TAG_REGEX.is_match("<span class='test'>"));
599
600 assert!(BLOCKQUOTE_PREFIX_RE.is_match("> Quote"));
602 assert!(BLOCKQUOTE_PREFIX_RE.is_match(" > Indented quote"));
603 assert!(BLOCKQUOTE_PREFIX_RE.is_match(">> Nested"));
604 }
605
606 #[test]
607 fn test_thread_safety() {
608 use std::thread;
609
610 let handles: Vec<_> = (0..10)
611 .map(|i| {
612 thread::spawn(move || {
613 let pattern = format!(r"\d{{{i}}}");
614 let regex = get_cached_regex(&pattern).unwrap();
615 assert!(regex.is_match(&"1".repeat(i)));
616 })
617 })
618 .collect();
619
620 for handle in handles {
621 handle.join().unwrap();
622 }
623 }
624
625 #[test]
630 fn test_url_standard_basic() {
631 assert!(URL_STANDARD_REGEX.is_match("https://example.com"));
633 assert!(URL_STANDARD_REGEX.is_match("http://example.com"));
634 assert!(URL_STANDARD_REGEX.is_match("https://example.com/"));
635 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path"));
636 assert!(URL_STANDARD_REGEX.is_match("ftp://files.example.com"));
637 assert!(URL_STANDARD_REGEX.is_match("ftps://secure.example.com"));
638
639 assert!(!URL_STANDARD_REGEX.is_match("not a url"));
641 assert!(!URL_STANDARD_REGEX.is_match("example.com"));
642 assert!(!URL_STANDARD_REGEX.is_match("www.example.com"));
643 }
644
645 #[test]
646 fn test_url_standard_with_path() {
647 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path/to/page"));
648 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path/to/page.html"));
649 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path/to/page/"));
650 }
651
652 #[test]
653 fn test_url_standard_with_query() {
654 assert!(URL_STANDARD_REGEX.is_match("https://example.com?query=value"));
655 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path?query=value"));
656 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path?a=1&b=2"));
657 }
658
659 #[test]
660 fn test_url_standard_with_fragment() {
661 assert!(URL_STANDARD_REGEX.is_match("https://example.com#section"));
662 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path#section"));
663 assert!(URL_STANDARD_REGEX.is_match("https://example.com/path?query=value#section"));
664 }
665
666 #[test]
667 fn test_url_standard_with_port() {
668 assert!(URL_STANDARD_REGEX.is_match("https://example.com:8080"));
669 assert!(URL_STANDARD_REGEX.is_match("https://example.com:443/path"));
670 assert!(URL_STANDARD_REGEX.is_match("http://localhost:3000"));
671 assert!(URL_STANDARD_REGEX.is_match("https://192.168.1.1:8080/path"));
672 }
673
674 #[test]
675 fn test_url_standard_wikipedia_style_parentheses() {
676 let url = "https://en.wikipedia.org/wiki/Rust_(programming_language)";
678 assert!(URL_STANDARD_REGEX.is_match(url));
679
680 let cap = URL_STANDARD_REGEX.find(url).unwrap();
682 assert_eq!(cap.as_str(), url);
683
684 let url2 = "https://example.com/path_(foo)_(bar)";
686 let cap2 = URL_STANDARD_REGEX.find(url2).unwrap();
687 assert_eq!(cap2.as_str(), url2);
688 }
689
690 #[test]
691 fn test_url_standard_ipv6() {
692 assert!(URL_STANDARD_REGEX.is_match("https://[::1]/path"));
694 assert!(URL_STANDARD_REGEX.is_match("https://[2001:db8::1]:8080/path"));
695 assert!(URL_STANDARD_REGEX.is_match("http://[fe80::1%eth0]/"));
696 }
697
698 #[test]
699 fn test_url_www_basic() {
700 assert!(URL_WWW_REGEX.is_match("www.example.com"));
702 assert!(URL_WWW_REGEX.is_match("www.example.co.uk"));
703 assert!(URL_WWW_REGEX.is_match("www.sub.example.com"));
704
705 assert!(!URL_WWW_REGEX.is_match("example.com"));
707
708 assert!(URL_WWW_REGEX.is_match("https://www.example.com"));
712 }
713
714 #[test]
715 fn test_url_www_with_path() {
716 assert!(URL_WWW_REGEX.is_match("www.example.com/path"));
717 assert!(URL_WWW_REGEX.is_match("www.example.com/path/to/page"));
718 assert!(URL_WWW_REGEX.is_match("www.example.com/path_(with_parens)"));
719 }
720
721 #[test]
722 fn test_url_ipv6_basic() {
723 assert!(URL_IPV6_REGEX.is_match("https://[::1]/"));
725 assert!(URL_IPV6_REGEX.is_match("http://[2001:db8::1]/path"));
726 assert!(URL_IPV6_REGEX.is_match("https://[fe80::1]:8080/path"));
727 assert!(URL_IPV6_REGEX.is_match("ftp://[::ffff:192.168.1.1]/file"));
728 }
729
730 #[test]
731 fn test_url_ipv6_with_zone_id() {
732 assert!(URL_IPV6_REGEX.is_match("https://[fe80::1%eth0]/path"));
734 assert!(URL_IPV6_REGEX.is_match("http://[fe80::1%25eth0]:8080/"));
735 }
736
737 #[test]
738 fn test_url_simple_detection() {
739 assert!(URL_SIMPLE_REGEX.is_match("https://example.com"));
741 assert!(URL_SIMPLE_REGEX.is_match("http://test.org/path"));
742 assert!(URL_SIMPLE_REGEX.is_match("ftp://files.com/file.zip"));
743 assert!(!URL_SIMPLE_REGEX.is_match("not a url"));
744 }
745
746 #[test]
747 fn test_url_quick_check() {
748 assert!(URL_QUICK_CHECK_REGEX.is_match("https://example.com"));
750 assert!(URL_QUICK_CHECK_REGEX.is_match("http://example.com"));
751 assert!(URL_QUICK_CHECK_REGEX.is_match("ftp://files.com"));
752 assert!(URL_QUICK_CHECK_REGEX.is_match("www.example.com"));
753 assert!(URL_QUICK_CHECK_REGEX.is_match("user@example.com"));
754 assert!(!URL_QUICK_CHECK_REGEX.is_match("just plain text"));
755 }
756
757 #[test]
758 fn test_url_edge_cases() {
759 let url = "https://example.com/path";
761 assert!(URL_STANDARD_REGEX.is_match(url));
762
763 let text = "Check https://example.com, it's great!";
766 let cap = URL_STANDARD_REGEX.find(text).unwrap();
767 assert!(cap.as_str().ends_with(','));
769
770 let text2 = "See <https://example.com> for more";
772 assert!(URL_STANDARD_REGEX.is_match(text2));
773
774 let cap2 = URL_STANDARD_REGEX.find(text2).unwrap();
776 assert!(!cap2.as_str().contains('>'));
777 }
778
779 #[test]
780 fn test_url_with_complex_paths() {
781 let urls = [
783 "https://github.com/owner/repo/blob/main/src/file.rs#L123",
784 "https://docs.example.com/api/v2/endpoint?format=json&page=1",
785 "https://cdn.example.com/assets/images/logo.png?v=2023",
786 "https://search.example.com/results?q=test+query&filter=all",
787 ];
788
789 for url in urls {
790 assert!(URL_STANDARD_REGEX.is_match(url), "Should match: {url}");
791 }
792 }
793
794 #[test]
795 fn test_url_pattern_strings_are_valid() {
796 assert!(URL_STANDARD_REGEX.is_match("https://example.com"));
798 assert!(URL_WWW_REGEX.is_match("www.example.com"));
799 assert!(URL_IPV6_REGEX.is_match("https://[::1]/"));
800 assert!(URL_QUICK_CHECK_REGEX.is_match("https://example.com"));
801 assert!(URL_SIMPLE_REGEX.is_match("https://example.com"));
802 }
803
804 #[test]
811 fn test_is_blank_in_blockquote_context_regular_blanks() {
812 assert!(is_blank_in_blockquote_context(""));
814 assert!(is_blank_in_blockquote_context(" "));
815 assert!(is_blank_in_blockquote_context("\t"));
816 assert!(is_blank_in_blockquote_context(" \t "));
817 }
818
819 #[test]
820 fn test_is_blank_in_blockquote_context_blockquote_blanks() {
821 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 assert!(is_blank_in_blockquote_context(">> "));
827 assert!(is_blank_in_blockquote_context(">>>"));
828 assert!(is_blank_in_blockquote_context(">>> "));
829 }
830
831 #[test]
832 fn test_is_blank_in_blockquote_context_spaced_nested() {
833 assert!(is_blank_in_blockquote_context("> > "));
835 assert!(is_blank_in_blockquote_context("> > > "));
836 assert!(is_blank_in_blockquote_context("> > "));
837 }
838
839 #[test]
840 fn test_is_blank_in_blockquote_context_with_leading_space() {
841 assert!(is_blank_in_blockquote_context(" >"));
843 assert!(is_blank_in_blockquote_context(" > "));
844 assert!(is_blank_in_blockquote_context(" >>"));
845 }
846
847 #[test]
848 fn test_is_blank_in_blockquote_context_not_blank() {
849 assert!(!is_blank_in_blockquote_context("text"));
851 assert!(!is_blank_in_blockquote_context("> text"));
852 assert!(!is_blank_in_blockquote_context(">> text"));
853 assert!(!is_blank_in_blockquote_context("> | table |"));
854 assert!(!is_blank_in_blockquote_context("| table |"));
855 assert!(!is_blank_in_blockquote_context("> # Heading"));
856 assert!(!is_blank_in_blockquote_context(">text")); }
858
859 #[test]
860 fn test_is_blank_in_blockquote_context_edge_cases() {
861 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")); }
867}