From f4366f8b2e52c185ee269810937dbed031e2dd0a Mon Sep 17 00:00:00 2001 From: Mathieu Duponchelle Date: Thu, 14 Mar 2024 13:08:39 +0100 Subject: [PATCH] gstregex: add support for switches exposed by RegexBuilder The builder allows for instance for switching off case-sensitiveness for the entire pattern, instead of having to do so inline with `(?i)`. All the options exposed by the builder at can now be passed as fields of invidual commands, snake-cased. Part-of: --- text/regex/src/gstregex/imp.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/text/regex/src/gstregex/imp.rs b/text/regex/src/gstregex/imp.rs index 1b373654..10ec9504 100644 --- a/text/regex/src/gstregex/imp.rs +++ b/text/regex/src/gstregex/imp.rs @@ -10,7 +10,7 @@ use gst::glib; use gst::prelude::*; use gst::subclass::prelude::*; -use regex::Regex; +use regex::{Regex, RegexBuilder}; use std::default::Default; use std::sync::Mutex; @@ -193,7 +193,33 @@ impl ObjectImpl for RegEx { } }; - let regex = match Regex::new(&pattern) { + let mut builder = RegexBuilder::new(&pattern); + builder + .unicode(s.get::("unicode").unwrap_or(true)) + .case_insensitive(s.get::("case-insensitive").unwrap_or(false)) + .multi_line(s.get::("multi-line").unwrap_or(false)) + .dot_matches_new_line( + s.get::("dot-matches-new-line").unwrap_or(false), + ) + .crlf(s.get::("crlf").unwrap_or(false)) + .line_terminator(s.get::("line-terminator").unwrap_or(b'\n')) + .swap_greed(s.get::("swap-greed").unwrap_or(false)) + .ignore_whitespace(s.get::("ignore-whitespace").unwrap_or(false)) + .octal(s.get::("octal").unwrap_or(false)); + + if let Ok(limit) = s.get::("size-limit") { + builder.size_limit(limit as usize); + } + + if let Ok(limit) = s.get::("dfa-size-limit") { + builder.dfa_size_limit(limit as usize); + } + + if let Ok(limit) = s.get::("nest-limit") { + builder.nest_limit(limit); + } + + let regex = match builder.build() { Ok(regex) => regex, Err(err) => { gst::error!(CAT, imp: self, "Failed to compile regex: {:?}", err);