Skip to content

Commit 2f41da0

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 2f41da0

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/uu/pgrep/src/process_matcher.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,14 @@ 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.regex.as_str().trim_matches('^').trim_matches('$').len() > 15
156+
} else {
157+
settings.regex.as_str().len() > 15
158+
};
159+
154160
if pids.is_empty() {
155-
if !settings.full && settings.regex.as_str().len() > 15 {
161+
if !settings.full && is_long_match {
156162
let msg = format!("pattern that searches for process name longer than 15 characters will result in zero matches\n\
157163
Try `{} -f' option to match against the complete command line.", uucore::util_name());
158164
return Err(USimpleError::new(1, msg));

0 commit comments

Comments
 (0)