From ddf61d4c4af9e507eabae620b7a79550f94553e8 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Mon, 6 Mar 2023 10:35:01 -0500 Subject: [PATCH] api: add Match::{is_empty, len} Adding these methods has almost no cost and they can be convenient to have in some cases. Closes #810 --- src/re_bytes.rs | 12 ++++++++++++ src/re_unicode.rs | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/re_bytes.rs b/src/re_bytes.rs index b86973d0b..64c09a725 100644 --- a/src/re_bytes.rs +++ b/src/re_bytes.rs @@ -37,6 +37,18 @@ impl<'t> Match<'t> { self.end } + /// Returns true if and only if this match has a length of zero. + #[inline] + pub fn is_empty(&self) -> bool { + self.start == self.end + } + + /// Returns the length, in bytes, of this match. + #[inline] + pub fn len(&self) -> usize { + self.end - self.start + } + /// Returns the range over the starting and ending byte offsets of the /// match in the haystack. #[inline] diff --git a/src/re_unicode.rs b/src/re_unicode.rs index 41bd8ac09..bee365e8d 100644 --- a/src/re_unicode.rs +++ b/src/re_unicode.rs @@ -45,6 +45,18 @@ impl<'t> Match<'t> { self.end } + /// Returns true if and only if this match has a length of zero. + #[inline] + pub fn is_empty(&self) -> bool { + self.start == self.end + } + + /// Returns the length, in bytes, of this match. + #[inline] + pub fn len(&self) -> usize { + self.end - self.start + } + /// Returns the range over the starting and ending byte offsets of the /// match in the haystack. #[inline]