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 committed Jun 12, 2013
1 parent ca160b2 commit 47d039c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
6 changes: 6 additions & 0 deletions doc/ag.1
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ Recursively search for PATTERN in PATH\. Like grep or ack, but faster\.
\~\~\~\~ Print matches on very long lines (> 2k characters by default)
.
.P
\fB\-\-long\-line\-length\fR:
.
.br
\~\~\~\~ Length limit for long lines (for \-\-print\-long\-lines)
.
.P
\fB\-Q \-\-literal\fR:
.
.br
Expand Down
2 changes: 2 additions & 0 deletions doc/ag.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Recursively search for PATTERN in PATH. Like grep or ack, but faster.
Use a pager such as less. Use `--nopager` to override. This option is also ignored if output is piped to another program.
* `--print-long-lines`:
Print matches on very long lines (> 2k characters by default)
* `--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.
* `-s --case-sensitive`:
Expand Down
6 changes: 6 additions & 0 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Search options:\n\
-p --path-to-agignore STRING\n\
Use .agignore file at STRING\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\
-Q --literal Don't parse PATTERN as a regular expression\n\
-s --case-sensitive Match case sensitively (Enabled by default)\n\
-S --smart-case Match case insensitively unless PATTERN contains\n\
Expand Down Expand Up @@ -117,6 +118,7 @@ void init_options() {
opts.color_path = ag_strdup(color_path);
opts.color_match = ag_strdup(color_match);
opts.color_line_number = ag_strdup(color_line_number);
opts.long_line_length = 2000;
}

void cleanup_options() {
Expand Down Expand Up @@ -197,6 +199,7 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
{ "invert-match", no_argument, &opts.invert_match, 1 },
{ "line-numbers", no_argument, &opts.print_line_numbers, 2 },
{ "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-numbers", no_argument, NULL, 0 },
Expand Down Expand Up @@ -382,6 +385,9 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) {
} else if (strcmp(longopts[opt_index].name, "ignore") == 0) {
add_ignore_pattern(root_ignores, optarg);
break;
} 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;
opts.pager = NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ typedef struct {
int print_filename_only;
int print_heading;
int print_line_numbers;
int print_long_lines; /* TODO: support this in print.c */
int print_long_lines;
int long_line_length;
pcre *re;
pcre_extra *re_extra;
int recurse_dirs;
Expand Down
23 changes: 22 additions & 1 deletion 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 int 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 @@ -100,6 +111,14 @@ void search_buf(const char *buf, const int buf_len,
log_debug("Regex match found. File %s, offset %i bytes.", dir_full_path, offset_vector[0]);
buf_offset = offset_vector[1];

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 ((size_t)matches_len + matches_spare >= matches_size) {
matches_size = matches ? matches_size * 2 : 100;
Expand Down Expand Up @@ -159,7 +178,9 @@ void search_stream(FILE *stream, const char *path) {
size_t line_cap = 0;

while ((line_len = getline(&line, &line_cap, stream)) > 0) {
search_buf(line, line_len, path);
if (opts.print_long_lines || line_len < opts.long_line_length) {
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 @@ -470,3 +470,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 @@ -96,3 +96,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 47d039c

Please sign in to comment.