The silver search(ag)比ack-grep還快

來源:互聯網
上載者:User

今天用ag搜尋android4.1的代碼,發現總是被一個長得讓崩潰的.json檔案匹配(鍵入ag ::layout)搞得沒法健康地查看ag.el的結果。

一看ag的代碼才知道這長行列印壓制還是個未完成功能

src/options.h:48:30:    int print_long_lines; /* TODO: support this in print.c */

好在作者的C代碼簡潔明了,使得我可以很快寫出一個Patch,使用-M 參數來壓制長行輸出。patch郵件已經發送。我自己也backup一下:

diff --git a/src/options.c b/src/options.cindex b08903f..2f4b18e 100644--- a/src/options.c+++ b/src/options.c@@ -77,6 +77,8 @@ 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\+-M --max-printable-line-length NUM \n\+                        Skip print the matching lines that have a length bigger than NUM\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\@@ -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.hindex 5049ab5..b4d2468 100644--- a/src/options.h+++ b/src/options.h@@ -7,6 +7,7 @@ #include <pcre.h>  #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.cindex 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.\n", 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\n");+    } 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);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.