summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authordana <dana@dana.is>2017-08-09 05:53:35 -0500
committerAndrew Gallant <jamslam@gmail.com>2017-08-09 06:53:35 -0400
commit40bacbcd7ce3247cf778e52e9367e2d675e0f153 (patch)
tree9863dc3849fc2cd129df03f353342ec17434af5f /src
parentb3a9c34515ec087bb7325db81f60ce26aeee3fd9 (diff)
Add -x/--line-regexp (#520)
add -x/--line-regexp flag
Diffstat (limited to 'src')
-rw-r--r--src/app.rs7
-rw-r--r--src/args.rs21
2 files changed, 23 insertions, 5 deletions
diff --git a/src/app.rs b/src/app.rs
index bb1e89c4..43c03015 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -109,7 +109,8 @@ pub fn app() -> App<'static, 'static> {
.arg(flag("unrestricted").short("u")
.multiple(true))
.arg(flag("invert-match").short("v"))
- .arg(flag("word-regexp").short("w"))
+ .arg(flag("word-regexp").short("w").overrides_with("line-regexp"))
+ .arg(flag("line-regexp").short("x"))
// Third, set up less common flags.
.arg(flag("after-context").short("A")
.value_name("NUM").takes_value(true)
@@ -348,6 +349,10 @@ lazy_static! {
"Only show matches surrounded by word boundaries. This is \
equivalent to putting \\b before and after all of the search \
patterns.");
+ doc!(h, "line-regexp",
+ "Only show matches surrounded by line boundaries.",
+ "Only show matches surrounded by line boundaries. This is \
+ equivalent to putting ^...$ around all of the search patterns.");
doc!(h, "after-context",
"Show NUM lines after each match.");
diff --git a/src/args.rs b/src/args.rs
index fb37c715..ff636d17 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -427,7 +427,8 @@ impl<'a> ArgMatches<'a> {
///
/// Note that if -F/--fixed-strings is set, then all patterns will be
/// escaped. Similarly, if -w/--word-regexp is set, then all patterns
- /// are surrounded by `\b`.
+ /// are surrounded by `\b`, and if -x/--line-regexp is set, then all
+ /// patterns are surrounded by `^...$`.
///
/// If any pattern is invalid UTF-8, then an error is returned.
fn patterns(&self) -> Result<Vec<String>> {
@@ -470,7 +471,7 @@ impl<'a> ArgMatches<'a> {
Ok(pats)
}
- /// Converts an OsStr pattern to a String pattern, including word
+ /// Converts an OsStr pattern to a String pattern, including line/word
/// boundaries or escapes if applicable.
///
/// If the pattern is not valid UTF-8, then an error is returned.
@@ -479,10 +480,12 @@ impl<'a> ArgMatches<'a> {
Ok(self.str_pattern(s))
}
- /// Converts a &str pattern to a String pattern, including word
+ /// Converts a &str pattern to a String pattern, including line/word
/// boundaries or escapes if applicable.
fn str_pattern(&self, pat: &str) -> String {
- let s = self.word_pattern(self.literal_pattern(pat.to_string()));
+ let litpat = self.literal_pattern(pat.to_string());
+ let s = self.line_pattern(self.word_pattern(litpat));
+
if s.is_empty() {
self.empty_pattern()
} else {
@@ -511,6 +514,16 @@ impl<'a> ArgMatches<'a> {
}
}
+ /// Returns the given pattern as a line pattern if the -x/--line-regexp
+ /// flag is set. Otherwise, the pattern is returned unchanged.
+ fn line_pattern(&self, pat: String) -> String {
+ if self.is_present("line-regexp") {
+ format!(r"^(?:{})$", pat)
+ } else {
+ pat
+ }
+ }
+
/// Empty pattern returns a pattern that is guaranteed to produce an empty
/// regular expression that is valid in any position.
fn empty_pattern(&self) -> String {