Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: Add as_match method #1227

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/regex/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,24 @@ impl<'h> Captures<'h> {
.map(|sp| Match::new(self.haystack, sp.start, sp.end))
}

/// Return the overall match for the capture.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an example. I try to include an example for every public API item.

///
/// This returns the match for index `0`. That is it is equivalent to
/// `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");
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is not formatted in the same way as all other examples.

Please look at other public API items and format this one in the same way. It should have an # Example heading, for example.

#[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.
Expand Down
19 changes: 19 additions & 0 deletions src/regex/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,25 @@ 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
/// `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()
}

/// 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.
Expand Down