From 247b7bf83234ac2509c477f1bc2b9190fe1da30e Mon Sep 17 00:00:00 2001 From: DeveloperC Date: Sun, 31 Oct 2021 12:24:53 +0000 Subject: [PATCH] feat: Regex::new takes Into Makes Regex::new more flexible and allowing the type Into to be used to create a regex. --- src/re_builder.rs | 4 ++-- src/re_bytes.rs | 26 +++++++++++++++++++++++++- src/re_unicode.rs | 26 +++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/re_builder.rs b/src/re_builder.rs index ee6383690..4a37f4544 100644 --- a/src/re_builder.rs +++ b/src/re_builder.rs @@ -55,9 +55,9 @@ macro_rules! define_builder { /// /// If the pattern is invalid, then an error will be returned when /// `build` is called. - pub fn new(pattern: &str) -> RegexBuilder { + pub fn new>(pattern: T) -> RegexBuilder { let mut builder = RegexBuilder(RegexOptions::default()); - builder.0.pats.push(pattern.to_owned()); + builder.0.pats.push(pattern.into()); builder } diff --git a/src/re_bytes.rs b/src/re_bytes.rs index ae55d6d25..c5f2a86e7 100644 --- a/src/re_bytes.rs +++ b/src/re_bytes.rs @@ -116,7 +116,31 @@ impl Regex { /// to search, split or replace text in a string. /// /// If an invalid expression is given, then an error is returned. - pub fn new(re: &str) -> Result { + /// + /// # Example + /// + /// Create a new Regex using a &str. + /// + /// ```rust + /// # use regex::bytes::Regex; + /// # fn main() { + /// let regex: &str = r"\b\w{13}\b"; + /// Regex::new(regex); + /// # } + /// ``` + /// + /// # Example + /// + /// Create a new Regex using a String. + /// + /// ```rust + /// # use regex::bytes::Regex; + /// # fn main() { + /// let regex: String = r"\b\w{13}\b".to_string(); + /// Regex::new(regex); + /// # } + /// ``` + pub fn new>(re: T) -> Result { RegexBuilder::new(re).build() } diff --git a/src/re_unicode.rs b/src/re_unicode.rs index 142c78fb1..249152f38 100644 --- a/src/re_unicode.rs +++ b/src/re_unicode.rs @@ -171,7 +171,31 @@ impl Regex { /// to search, split or replace text in a string. /// /// If an invalid expression is given, then an error is returned. - pub fn new(re: &str) -> Result { + /// + /// # Example + /// + /// Create a new Regex using a &str. + /// + /// ```rust + /// # use regex::bytes::Regex; + /// # fn main() { + /// let regex: &str = r"\b\w{13}\b"; + /// Regex::new(regex); + /// # } + /// ``` + /// + /// # Example + /// + /// Create a new Regex using a String. + /// + /// ```rust + /// # use regex::bytes::Regex; + /// # fn main() { + /// let regex: String = r"\b\w{13}\b".to_string(); + /// Regex::new(regex); + /// # } + /// ``` + pub fn new>(re: T) -> Result { RegexBuilder::new(re).build() }