Skip to content

Commit 961f167

Browse files
committed
pgrep: optimize length detection
When calculating the length, the ^$ character needs to be removed if the -x parameter is present. Closes: #635
1 parent 99e1421 commit 961f167

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/uu/pgrep/src/process_matcher.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,20 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
151151
pub fn find_matching_pids(settings: &Settings) -> UResult<Vec<ProcessInformation>> {
152152
let mut pids = collect_matched_pids(settings)?;
153153

154+
let is_long_match = if settings.exact {
155+
settings
156+
.regex
157+
.as_str()
158+
.trim_matches('^')
159+
.trim_matches('$')
160+
.len()
161+
> 15
162+
} else {
163+
settings.regex.as_str().len() > 15
164+
};
165+
154166
if pids.is_empty() {
155-
if !settings.full && settings.regex.as_str().len() > 15 {
167+
if !settings.full && is_long_match {
156168
let msg = format!("pattern that searches for process name longer than 15 characters will result in zero matches\n\
157169
Try `{} -f' option to match against the complete command line.", uucore::util_name());
158170
return Err(USimpleError::new(1, msg));

0 commit comments

Comments
 (0)