The silver search (ag) 가 ack - grep 보다 빨 라 요.

5553 단어
오늘 은 ag 로 안 드 로 이 드 4.1 코드 를 검색 하 였 는데, 항상 무 너 질 정도 로 생 긴. json 파일 에 일치 하 는 것 을 발견 하 였 습 니 다. (ag: layot 입력) 때문에 ag. el 의 결 과 를 건강 하 게 볼 수 없습니다.
ag 의 코드 를 보고 나 서 야 이 긴 줄 의 인쇄 억제 가 아직 완성 되 지 않 았 다 는 것 을 알 게 되 었 다.
src/options.h:48:30:    int print_long_lines; /* TODO: support this in print.c */
다행히 작가 의 C 코드 가 간단명료 해서 나 는 패 치 를 빨리 쓸 수 있 고 - M 인 자 를 사용 하여 긴 줄 의 출력 을 억제 할 수 있다.패 치 메 일이 발송 되 었 습 니 다.나 도 백업 해 볼 게.
diff --git a/src/options.c b/src/options.c
index b08903f..2f4b18e 100644
--- a/src/options.c
+++ b/src/options.c
@@ -77,6 +77,8 @@ Search options:
\ -p --path-to-agignore STRING
\ Use .agignore file at STRING
\ --print-long-lines Print matches on very long lines (Default: >2k characters)
\ +-M --max-printable-line-length NUM
\ + Skip print the matching lines that have a length bigger than NUM
\ -Q --literal Don't parse PATTERN as a regular expression
\ -s --case-sensitive Match case sensitively (Enabled by default)
\ -S --smart-case Match case insensitively unless PATTERN contains
\ @@ -115,6 +117,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.max_printable_line_length = DEFAULT_MAX_PRINTABLE_LINE_LENGTH; } void cleanup_options() { @@ -221,6 +224,7 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) { { "version", no_argument, &version, 1 }, { "word-regexp", no_argument, NULL, 'w' }, { "workers", required_argument, NULL, 0 }, + { "max-printable-line-length", required_argument, NULL, 'M' }, { NULL, 0, NULL, 0 } }; @@ -253,7 +257,7 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) { opts.stdout_inode = statbuf.st_ino; } - while ((ch = getopt_long(argc, argv, "A:aB:C:DG:g:fhiLlm:np:QRrSsvVtuUwz", longopts, &opt_index)) != -1) { + while ((ch = getopt_long(argc, argv, "A:aB:C:DG:g:fhiLlm:M:np:QRrSsvVtuUwz", longopts, &opt_index)) != -1) { switch (ch) { case 'A': opts.after = atoi(optarg); @@ -305,6 +309,9 @@ void parse_options(int argc, char **argv, char **base_paths[], char **paths[]) { case 'm': opts.max_matches_per_file = atoi(optarg); break; + case 'M': + opts.max_printable_line_length = atoi(optarg); + break; case 'n': opts.recurse_dirs = 0; break; diff --git a/src/options.h b/src/options.h index 5049ab5..b4d2468 100644 --- a/src/options.h +++ b/src/options.h @@ -7,6 +7,7 @@ #include #define DEFAULT_CONTEXT_LEN 2 +#define DEFAULT_MAX_PRINTABLE_LINE_LENGTH 2000 enum case_behavior { CASE_SENSITIVE, @@ -45,6 +46,7 @@ typedef struct { int print_heading; int print_line_numbers; int print_long_lines; /* TODO: support this in print.c */ + int max_printable_line_length; pcre *re; pcre_extra *re_extra; int recurse_dirs; diff --git a/src/print.c b/src/print.c index dc11594..4be9874 100644 --- a/src/print.c +++ b/src/print.c @@ -34,6 +34,15 @@ void print_binary_file_matches(const char* path) { fprintf(out_fd, "Binary file %s matches.
", path); } +static void check_printable(int len, int *printable) { + if (len > opts.max_printable_line_length) { + *printable = FALSE; + fprintf(out_fd, "+EVIL+MARK+VERY+LONG+LINES+HERE
"); + } else { + *printable = TRUE; + } +} + void print_file_matches(const char* path, const char* buf, const int buf_len, const match matches[], const int matches_len) { int line = 1; char **context_prev_lines = NULL; @@ -49,6 +58,7 @@ void print_file_matches(const char* path, const char* buf, const int buf_len, co int i, j; int in_a_match = FALSE; int printing_a_match = FALSE; + int printable = TRUE; if (opts.ackmate) { sep = ':'; @@ -129,7 +139,8 @@ void print_file_matches(const char* path, const char* buf, const int buf_len, co } j = prev_line_offset; /* print up to current char */ - for (; j <= i; j++) { + check_printable(i - prev_line_offset, &printable); + for (; j <= i && printable; j++) { fputc(buf[j], out_fd); } } else { @@ -141,7 +152,8 @@ void print_file_matches(const char* path, const char* buf, const int buf_len, co if (printing_a_match && opts.color) { fprintf(out_fd, "%s", opts.color_match); } - for (j = prev_line_offset; j <= i; j++) { + check_printable(i - prev_line_offset, &printable); + for (j = prev_line_offset; j <= i && printable; j++) { if (j == matches[last_printed_match].end && last_printed_match < matches_len) { if (opts.color) { fprintf(out_fd, "%s", color_reset);

좋은 웹페이지 즐겨찾기