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

Added -x/--line-regexp (match whole line) option #625

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/main.c
Expand Up @@ -108,6 +108,8 @@ int main(int argc, char **argv) {
generate_alpha_skip(opts.query, opts.query_len, alpha_skip_lookup, opts.casing == CASE_SENSITIVE);
find_skip_lookup = NULL;
generate_find_skip(opts.query, opts.query_len, &find_skip_lookup, opts.casing == CASE_SENSITIVE);

// line_regexp is handled in the actual search
if (opts.word_regexp) {
init_wordchar_table();
opts.literal_starts_wordchar = is_wordchar(opts.query[0]);
Expand All @@ -117,11 +119,17 @@ int main(int argc, char **argv) {
if (opts.casing == CASE_INSENSITIVE) {
pcre_opts |= PCRE_CASELESS;
}
if (opts.word_regexp) {
char *word_regexp_query;
ag_asprintf(&word_regexp_query, "\\b%s\\b", opts.query);
if (opts.word_regexp || opts.line_regexp) {
char *word_or_line_regexp_query;

if (opts.word_regexp) {
ag_asprintf(&word_or_line_regexp_query, "\\b%s\\b", opts.query);
} else {
ag_asprintf(&word_or_line_regexp_query, "^%s$", opts.query);
}

free(opts.query);
opts.query = word_regexp_query;
opts.query = word_or_line_regexp_query;
opts.query_len = strlen(opts.query);
}
compile_study(&opts.re, &opts.re_extra, opts.query, pcre_opts, study_opts);
Expand Down
9 changes: 8 additions & 1 deletion src/options.c
Expand Up @@ -94,6 +94,7 @@ Search Options:\n\
(.gitignore, .hgignore, .svnignore; still obey .agignore)\n\
-v --invert-match\n\
-w --word-regexp Only match whole words\n\
-x --line-regexp Only match whole lines\n\
-z --search-zip Search contents of compressed (e.g., gzip) files\n\
\n");
printf("File Types:\n\
Expand Down Expand Up @@ -224,6 +225,7 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
{ "invert-match", no_argument, NULL, 'v' },
/* deprecated for --numbers. Remove eventually. */
{ "line-numbers", no_argument, &opts.print_line_numbers, 2 },
{ "line-regexp", no_argument, NULL, 'x' },
{ "list-file-types", no_argument, &list_file_types, 1 },
{ "literal", no_argument, NULL, 'Q' },
{ "match", no_argument, &useless, 0 },
Expand Down Expand Up @@ -312,7 +314,7 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
}

int pcre_opts = 0;
while ((ch = getopt_long(argc, argv, "A:aB:C:cDG:g:FfHhiLlm:nop:QRrSsvVtuUwz0", longopts, &opt_index)) != -1) {
while ((ch = getopt_long(argc, argv, "A:aB:C:cDG:g:FfHhiLlm:nop:QRrSsvVtuUwxz0", longopts, &opt_index)) != -1) {
switch (ch) {
case 'A':
if (optarg) {
Expand Down Expand Up @@ -440,6 +442,11 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
break;
case 'w':
opts.word_regexp = 1;
opts.line_regexp = 0;
break;
case 'x':
opts.line_regexp = 1;
opts.word_regexp = 0;
break;
case 'z':
opts.search_zip_files = 1;
Expand Down
1 change: 1 addition & 0 deletions src/options.h
Expand Up @@ -45,6 +45,7 @@ typedef struct {
int context;
int follow_symlinks;
int invert_match;
int line_regexp;
int literal;
int literal_starts_wordchar;
int literal_ends_wordchar;
Expand Down
24 changes: 17 additions & 7 deletions src/search.c
Expand Up @@ -52,18 +52,28 @@ void search_buf(const char *buf, const size_t buf_len,
break;
}

if (opts.word_regexp) {
if (opts.word_regexp || opts.line_regexp) {
const char *start = match_ptr;
const char *end = match_ptr + opts.query_len;

/* Check whether both start and end of the match lie on a word
* boundary
/*
* In word_regexp mode we want the start and end of the match to lie
* either on the buffer start/end or a word boundary; in line_regexp
* mode we want the start and end of the match to lie either on the
* buffer start/end or a new line
*/
if ((start == buf ||
is_wordchar(*(start - 1)) != opts.literal_starts_wordchar) &&
if (opts.word_regexp &&
(start == buf ||
is_wordchar(*(start - 1)) != opts.literal_starts_wordchar) &&
(end == buf + buf_len ||
is_wordchar(*end) != opts.literal_ends_wordchar)) {
/* It's a match */
is_wordchar(*end) != opts.literal_ends_wordchar)
) {
/* It's a word match */
} else if (opts.line_regexp &&
(start == buf || *(start - 1) == '\n') &&
(end == buf + buf_len || *end == '\n')
) {
/* It's a line match */
} else {
/* It's not a match */
match_ptr += opts.query_len;
Expand Down
20 changes: 20 additions & 0 deletions tests/option_lineregexp.t
@@ -0,0 +1,20 @@
Setup:

$ . $TESTDIR/setup.sh
$ printf '%s\n' 'foo' 'foobar' 'foo bar' > ./option_lineregexp_test.txt

Line matching, literal:

$ ag -Qx 'foo' ./option_lineregexp_test.txt
1:foo
$ ag -Qx 'bar' ./option_lineregexp_test.txt
[1]

Line matching, non-literal:

$ ag -x 'f.+o' ./option_lineregexp_test.txt
1:foo
$ ag -x 'b.+r' ./option_lineregexp_test.txt
[1]


22 changes: 22 additions & 0 deletions tests/option_wordregexp.t
@@ -0,0 +1,22 @@
Setup:

$ . $TESTDIR/setup.sh
$ printf '%s\n' 'foo' 'foobar' 'foo bar' > ./option_wordregexp_test.txt

Word matching, literal:

$ ag -Qw 'foo' ./option_wordregexp_test.txt
1:foo
3:foo bar
$ ag -Qw 'bar' ./option_wordregexp_test.txt
3:foo bar

Word matching, non-literal:

$ ag -w 'f.+o' ./option_wordregexp_test.txt
1:foo
3:foo bar
$ ag -w 'b.+r' ./option_wordregexp_test.txt
3:foo bar