From 0fc52dca5df749ca698b0a33c776b8654de2190b Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Thu, 19 Sep 2024 01:41:40 -0600 Subject: [PATCH 1/3] api: Add as_match method To simplify getting the whole match from Captures --- src/regex/bytes.rs | 9 +++++++++ src/regex/string.rs | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/regex/bytes.rs b/src/regex/bytes.rs index 39af6e71c..a8dd5a03e 100644 --- a/src/regex/bytes.rs +++ b/src/regex/bytes.rs @@ -1665,6 +1665,15 @@ impl<'h> Captures<'h> { .map(|sp| Match::new(self.haystack, sp.start, sp.end)) } + /// Return the overall match for the capture. + /// + /// This returns the match for index `0`. That is it is equivalent to + /// `get(0).unwrap()` + #[inline] + pub fn as_match(&self) -> Match { + self.get(0).unwrap() + } + /// Returns the `Match` associated with the capture group named `name`. If /// `name` isn't a valid capture group or it refers to a group that didn't /// match, then `None` is returned. diff --git a/src/regex/string.rs b/src/regex/string.rs index fab178a68..1bc4433b3 100644 --- a/src/regex/string.rs +++ b/src/regex/string.rs @@ -1675,6 +1675,15 @@ impl<'h> Captures<'h> { .map(|sp| Match::new(self.haystack, sp.start, sp.end)) } + /// Return the overall match for the capture. + /// + /// This returns the match for index `0`. That is it is equivalent to + /// `get(0).unwrap()` + #[inline] + pub fn as_match(&self) -> Match { + self.get(0).unwrap() + } + /// Returns the `Match` associated with the capture group named `name`. If /// `name` isn't a valid capture group or it refers to a group that didn't /// match, then `None` is returned. From 7311bfce9beb4e83a60aa11a51550e141bd242e6 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Fri, 20 Sep 2024 00:36:58 -0600 Subject: [PATCH 2/3] fixup! api: Add as_match method --- src/regex/bytes.rs | 11 ++++++++++- src/regex/string.rs | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/regex/bytes.rs b/src/regex/bytes.rs index a8dd5a03e..9741b28c3 100644 --- a/src/regex/bytes.rs +++ b/src/regex/bytes.rs @@ -1668,7 +1668,16 @@ impl<'h> Captures<'h> { /// Return the overall match for the capture. /// /// This returns the match for index `0`. That is it is equivalent to - /// `get(0).unwrap()` + /// `m.get(0).unwrap()` + /// + /// ``` + /// use regex::bytes::Regex; + /// + /// let re = Regex::new(r"[a-z]+([0-9]+)").unwrap(); + /// let caps = re.captures(b" abc123-def").unwrap(); + /// + /// assert_eq!(caps.as_match().as_bytes(), b"abc123"); + /// ``` #[inline] pub fn as_match(&self) -> Match { self.get(0).unwrap() diff --git a/src/regex/string.rs b/src/regex/string.rs index 1bc4433b3..acac4f1d4 100644 --- a/src/regex/string.rs +++ b/src/regex/string.rs @@ -1678,7 +1678,17 @@ impl<'h> Captures<'h> { /// Return the overall match for the capture. /// /// This returns the match for index `0`. That is it is equivalent to - /// `get(0).unwrap()` + /// `m.get(0).unwrap()` + /// + /// ``` + /// use regex::Regex; + /// + /// let re = Regex::new(r"[a-z]+([0-9]+)").unwrap(); + /// let caps = re.captures(" abc123-def").unwrap(); + /// + /// assert_eq!(caps.as_match().as_str(), "abc123"); + /// + /// ``` #[inline] pub fn as_match(&self) -> Match { self.get(0).unwrap() From cc735985f3d28367f2766d4f6a9438f501b648cc Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Mon, 30 Sep 2024 22:56:39 -0600 Subject: [PATCH 3/3] fixup! api: Add as_match method --- src/regex/bytes.rs | 6 ++++-- src/regex/string.rs | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/regex/bytes.rs b/src/regex/bytes.rs index 9741b28c3..807049834 100644 --- a/src/regex/bytes.rs +++ b/src/regex/bytes.rs @@ -1670,16 +1670,18 @@ impl<'h> Captures<'h> { /// This returns the match for index `0`. That is it is equivalent to /// `m.get(0).unwrap()` /// + /// # Example + /// /// ``` /// use regex::bytes::Regex; /// /// let re = Regex::new(r"[a-z]+([0-9]+)").unwrap(); /// let caps = re.captures(b" abc123-def").unwrap(); /// - /// assert_eq!(caps.as_match().as_bytes(), b"abc123"); + /// assert_eq!(caps.get_match().as_bytes(), b"abc123"); /// ``` #[inline] - pub fn as_match(&self) -> Match { + pub fn get_match(&self) -> Match { self.get(0).unwrap() } diff --git a/src/regex/string.rs b/src/regex/string.rs index acac4f1d4..da3524734 100644 --- a/src/regex/string.rs +++ b/src/regex/string.rs @@ -1680,17 +1680,19 @@ impl<'h> Captures<'h> { /// This returns the match for index `0`. That is it is equivalent to /// `m.get(0).unwrap()` /// + /// # Example + /// /// ``` /// use regex::Regex; /// /// let re = Regex::new(r"[a-z]+([0-9]+)").unwrap(); /// let caps = re.captures(" abc123-def").unwrap(); /// - /// assert_eq!(caps.as_match().as_str(), "abc123"); + /// assert_eq!(caps.get_match().as_str(), "abc123"); /// /// ``` #[inline] - pub fn as_match(&self) -> Match { + pub fn get_match(&self) -> Match { self.get(0).unwrap() }