Skip to content

Commit 3213bc5

Browse files
committed
[grep] solve mac-specific problem
1 parent 97c9ce1 commit 3213bc5

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

text/src/grep.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ impl Args {
145145
.iter()
146146
.flat_map(|pattern| pattern.split('\n').map(String::from))
147147
.collect();
148+
self.regexp.sort_by_key(|r| r.len());
149+
self.regexp.dedup();
148150

149151
if self.input_files.is_empty() {
150152
self.input_files.push(String::from("-"))
@@ -254,8 +256,22 @@ impl Patterns {
254256
if ignore_case {
255257
cflags |= REG_ICASE;
256258
}
257-
for p in patterns {
258-
let pattern = if line_regexp { format!("^{p}$") } else { p };
259+
for mut pattern in patterns {
260+
/// macOS version of [regcomp](regcomp) from `libc` provides additional check for empty regex. In this case, an error [REG_EMPTY](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/regcomp.3.html) will be returned.
261+
/// Therefore, an empty pattern is replaced with ".*".
262+
#[cfg(target_os = "macos")]
263+
{
264+
pattern = if pattern == "" {
265+
String::from(".*")
266+
} else {
267+
pattern
268+
};
269+
}
270+
pattern = if line_regexp {
271+
format!("^{pattern}$")
272+
} else {
273+
pattern
274+
};
259275

260276
let c_pattern = CString::new(pattern).map_err(|err| err.to_string())?;
261277
let mut regex = unsafe { std::mem::zeroed::<regex_t>() };
@@ -315,7 +331,7 @@ impl Drop for Patterns {
315331
Patterns::Fixed(_, _, _) => {}
316332
Patterns::Regex(regexes) => {
317333
for regex in regexes {
318-
unsafe { regfree(regex as *const _ as *mut _) }
334+
unsafe { regfree(regex as *const regex_t as *mut regex_t) }
319335
}
320336
}
321337
}

text/tests/grep/mod.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use plib::testing::{run_test, TestPlan};
1212

1313
const LINES_INPUT: &str =
1414
"line_{1}\np_line_{2}_s\n line_{3} \nLINE_{4}\np_LINE_{5}_s\nl_{6}\nline_{70}\n";
15+
const EMPTY_LINES_INPUT: &str = "\n\n\n";
1516
const BAD_INPUT: &str = "(some text)\n";
1617

1718
const INPUT_FILE_1: &str = "tests/grep/f_1";
@@ -1191,26 +1192,31 @@ fn test_empty_basic_regexp_01() {
11911192

11921193
#[test]
11931194
fn test_empty_basic_regexp_02() {
1194-
grep_test(&["-e", ""], LINES_INPUT, LINES_INPUT, "", 0);
1195+
grep_test(&[""], EMPTY_LINES_INPUT, EMPTY_LINES_INPUT, "", 0);
11951196
}
11961197

11971198
#[test]
11981199
fn test_empty_basic_regexp_03() {
1199-
grep_test(&["-f", EMPTY_PATTERN_FILE], LINES_INPUT, LINES_INPUT, "", 0);
1200+
grep_test(&["-e", ""], LINES_INPUT, LINES_INPUT, "", 0);
12001201
}
12011202

12021203
#[test]
12031204
fn test_empty_basic_regexp_04() {
1204-
grep_test(&[""], "", "", "", 1);
1205+
grep_test(&["-f", EMPTY_PATTERN_FILE], LINES_INPUT, LINES_INPUT, "", 0);
12051206
}
12061207

12071208
#[test]
12081209
fn test_empty_basic_regexp_05() {
1209-
grep_test(&["-e", ""], "", "", "", 1);
1210+
grep_test(&[""], "", "", "", 1);
12101211
}
12111212

12121213
#[test]
12131214
fn test_empty_basic_regexp_06() {
1215+
grep_test(&["-e", ""], "", "", "", 1);
1216+
}
1217+
1218+
#[test]
1219+
fn test_empty_basic_regexp_07() {
12141220
grep_test(&["-f", EMPTY_PATTERN_FILE], "", "", "", 1);
12151221
}
12161222

@@ -1221,11 +1227,16 @@ fn test_empty_extended_regexp_01() {
12211227

12221228
#[test]
12231229
fn test_empty_extended_regexp_02() {
1224-
grep_test(&["-E", "-e", ""], LINES_INPUT, LINES_INPUT, "", 0);
1230+
grep_test(&["-E", ""], EMPTY_LINES_INPUT, EMPTY_LINES_INPUT, "", 0);
12251231
}
12261232

12271233
#[test]
12281234
fn test_empty_extended_regexp_03() {
1235+
grep_test(&["-E", "-e", ""], LINES_INPUT, LINES_INPUT, "", 0);
1236+
}
1237+
1238+
#[test]
1239+
fn test_empty_extended_regexp_04() {
12291240
grep_test(
12301241
&["-E", "-f", EMPTY_PATTERN_FILE],
12311242
LINES_INPUT,
@@ -1236,17 +1247,17 @@ fn test_empty_extended_regexp_03() {
12361247
}
12371248

12381249
#[test]
1239-
fn test_empty_extended_regexp_04() {
1250+
fn test_empty_extended_regexp_05() {
12401251
grep_test(&["-E", ""], "", "", "", 1);
12411252
}
12421253

12431254
#[test]
1244-
fn test_empty_extended_regexp_05() {
1255+
fn test_empty_extended_regexp_06() {
12451256
grep_test(&["-E", "-e", ""], "", "", "", 1);
12461257
}
12471258

12481259
#[test]
1249-
fn test_empty_extended_regexp_06() {
1260+
fn test_empty_extended_regexp_07() {
12501261
grep_test(&["-E", "-f", EMPTY_PATTERN_FILE], "", "", "", 1);
12511262
}
12521263

@@ -1257,11 +1268,16 @@ fn test_empty_fixed_strings_01() {
12571268

12581269
#[test]
12591270
fn test_empty_fixed_strings_02() {
1260-
grep_test(&["-F", "-e", ""], LINES_INPUT, LINES_INPUT, "", 0);
1271+
grep_test(&["-E", ""], EMPTY_LINES_INPUT, EMPTY_LINES_INPUT, "", 0);
12611272
}
12621273

12631274
#[test]
12641275
fn test_empty_fixed_strings_03() {
1276+
grep_test(&["-F", "-e", ""], LINES_INPUT, LINES_INPUT, "", 0);
1277+
}
1278+
1279+
#[test]
1280+
fn test_empty_fixed_strings_04() {
12651281
grep_test(
12661282
&["-F", "-f", EMPTY_PATTERN_FILE],
12671283
LINES_INPUT,
@@ -1272,17 +1288,17 @@ fn test_empty_fixed_strings_03() {
12721288
}
12731289

12741290
#[test]
1275-
fn test_empty_fixed_strings_04() {
1291+
fn test_empty_fixed_strings_05() {
12761292
grep_test(&["-F", ""], "", "", "", 1);
12771293
}
12781294

12791295
#[test]
1280-
fn test_empty_fixed_strings_05() {
1296+
fn test_empty_fixed_strings_06() {
12811297
grep_test(&["-F", "-e", ""], "", "", "", 1);
12821298
}
12831299

12841300
#[test]
1285-
fn test_empty_fixed_strings_06() {
1301+
fn test_empty_fixed_strings_07() {
12861302
grep_test(&["-F", "-f", EMPTY_PATTERN_FILE], "", "", "", 1);
12871303
}
12881304

0 commit comments

Comments
 (0)