Skip to content

Commit

Permalink
Skip long lines (fixes ggreer#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
novalis authored and amfarrell committed Jun 30, 2015
1 parent 07f8336 commit 86d546e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 4 deletions.
4 changes: 4 additions & 0 deletions doc/ag.1
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ When searching a stream, print all lines even if they don\'t match\.
Do not parse PATTERN as a regular expression\. Try to match it literally\.
.
.TP
\fB\-\-long\-line\-length\fR:
Length limit for long lines (for \-\-print\-long\-lines)
.
.TP
\fB\-s \-\-case\-sensitive\fR
Match case\-sensitively\.
.
Expand Down
5 changes: 4 additions & 1 deletion doc/ag.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Recursively search for PATTERN in PATH. Like grep or ack, but faster.

* `-o --only-matching`:
Print only the matching part of the lines.

* `-p --path-to-agignore STRING`:
Provide a path to a specific .agignore file.

Expand All @@ -124,6 +124,9 @@ Recursively search for PATTERN in PATH. Like grep or ack, but faster.
* `--passthrough`:
When searching a stream, print all lines even if they don't match.

* `--long-line-length`:
Length limit for long lines (for --print-long-lines)

* `-Q --literal`:
Do not parse PATTERN as a regular expression. Try to match it literally.

Expand Down
5 changes: 5 additions & 0 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Output Options:\n\
when searching streams\n\
-o --only-matching Prints only the matching part of the lines\n\
--print-long-lines Print matches on very long lines (Default: >2k characters)\n\
--long-line-length Length limit for long lines (for --print-long-lines)\n\
--passthrough When searching a stream, print all lines even if they\n\
don't match\n\
--silent Suppress all log messages, including errors\n\
Expand Down Expand Up @@ -150,6 +151,7 @@ void init_options(void) {
opts.color_match = ag_strdup(color_match);
opts.color_line_number = ag_strdup(color_line_number);
opts.use_thread_affinity = TRUE;
opts.long_line_length = 2000;
}

void cleanup_options(void) {
Expand Down Expand Up @@ -249,6 +251,7 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
{ "line-numbers", no_argument, &opts.print_line_numbers, 2 },
{ "list-file-types", no_argument, &list_file_types, 1 },
{ "literal", no_argument, NULL, 'Q' },
{ "long-line-length", required_argument, &opts.long_line_length, 0},
{ "match", no_argument, &useless, 0 },
{ "max-count", required_argument, NULL, 'm' },
/* "no-" is deprecated. Remove these eventually. */
Expand Down Expand Up @@ -490,6 +493,8 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
} else if (strcmp(longopts[opt_index].name, "nofilename") == 0) {
opts.print_path = PATH_PRINT_NOTHING;
opts.print_line_numbers = FALSE;
} else if (strcmp(longopts[opt_index].name, "long-line-length") == 0) {
opts.long_line_length = atoi(optarg);
break;
} else if (strcmp(longopts[opt_index].name, "nopager") == 0) {
out_fd = stdout;
Expand Down
3 changes: 2 additions & 1 deletion src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ typedef struct {
int print_filename_only;
int print_path;
int print_line_numbers;
int print_long_lines; /* TODO: support this in print.c */
int print_long_lines;
int long_line_length;
int passthrough;
pcre *re;
pcre_extra *re_extra;
Expand Down
25 changes: 23 additions & 2 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ void search_buf(const char *buf, const size_t buf_len,
break;
}

if (!opts.print_long_lines) {
int line_length = get_line_length(buf, buf_len, match_ptr - buf, opts.query_len + match_ptr - buf);
if (line_length >= opts.long_line_length) {
log_debug("Too long line: %d > %d.\n", line_length, opts.long_line_length);
const char *end = match_ptr + opts.query_len;
buf_offset = end - buf;
match_ptr += opts.query_len;
continue;
}
}

if (opts.word_regexp) {
const char *start = match_ptr;
const char *end = match_ptr + opts.query_len;
Expand Down Expand Up @@ -101,6 +112,14 @@ void search_buf(const char *buf, const size_t buf_len,
log_debug("Regex match is of length zero. Advancing offset one byte.");
}

if (!opts.print_long_lines) {
int line_length = get_line_length(buf, buf_len, offset_vector[0], offset_vector[1]);
if (line_length >= opts.long_line_length) {
log_debug("Too long line: %d > %d.\n", line_length, opts.long_line_length);
continue;
}
}

/* TODO: copy-pasted from above. FIXME */
if (matches_len + matches_spare >= matches_size) {
matches_size = matches ? matches_size * 2 : 100;
Expand Down Expand Up @@ -179,8 +198,10 @@ void search_stream(FILE *stream, const char *path) {
size_t i;

for (i = 1; (line_len = getline(&line, &line_cap, stream)) > 0; i++) {
opts.stream_line_num = i;
search_buf(line, line_len, path);
if (opts.print_long_lines || line_len < opts.long_line_length) {
opts.stream_line_num = i;
search_buf(line, line_len, path);
}
}

free(line);
Expand Down
22 changes: 22 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,25 @@ int vasprintf(char **ret, const char *fmt, va_list args) {
return rv;
}
#endif

int get_line_length(const char* buf, const int buf_len,
const int match_start, const int match_end) {
/* count the number of characters before and after the match */
int line_length = match_end - match_start;
const char* c;
for(c = buf + match_start - 1; c >= buf; --c) {
if (*c == '\n') {
break;
}
line_length ++;
}

for(c = buf + match_end; c < buf + buf_len; ++c) {
if (*c == '\n') {
break;
}
line_length ++;
}

return line_length;
}
3 changes: 3 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,6 @@ int vasprintf(char **ret, const char *fmt, va_list args);
#endif

#endif

int get_line_length(const char* buf, const int buf_len,
const int match_start, const int match_end);

0 comments on commit 86d546e

Please sign in to comment.