diffprint.java
来自「JAVA 语言的函数式编程扩展」· Java 代码 · 共 608 行 · 第 1/2 页
JAVA
608 行
outfile.println(mark + ' ' + inf.getPath()); } public void print_header(String filea,String fileb) { print_context_label ("***", new File(filea), filea); print_context_label ("---", new File(fileb), fileb); } /** If function_regexp defined, search for start of function. */ private String find_function(Object[] lines, int start) { return null; } protected void print_function(Object[] file,int start) { String function = find_function (file0, first0); if (function != null) { outfile.print(" "); outfile.print( (function.length() < 40) ? function : function.substring(0,40) ); } } protected void print_hunk(Diff.change hunk) { /* Determine range of line numbers involved in each file. */ analyze_hunk (hunk); if (deletes == 0 && inserts == 0) return; /* Include a context's width before and after. */ first0 = Math.max(first0 - context, 0); first1 = Math.max(first1 - context, 0); last0 = Math.min(last0 + context, file0.length - 1); last1 = Math.min(last1 + context, file1.length - 1); outfile.print("***************"); /* If we looked for and found a function this is part of, include its name in the header of the diff section. */ print_function (file0, first0); outfile.println(); outfile.print("*** "); print_number_range (',', first0, last0); outfile.println(" ****"); if (deletes != 0) { Diff.change next = hunk; for (int i = first0; i <= last0; i++) { /* Skip past changes that apply (in file 0) only to lines before line I. */ while (next != null && next.line0 + next.deleted <= i) next = next.link; /* Compute the marking for line I. */ String prefix = " "; if (next != null && next.line0 <= i) /* The change NEXT covers this line. If lines were inserted here in file 1, this is "changed". Otherwise it is "deleted". */ prefix = (next.inserted > 0) ? "!" : "-"; print_1_line (prefix, file0[i]); } } outfile.print("--- "); print_number_range (',', first1, last1); outfile.println(" ----"); if (inserts != 0) { Diff.change next = hunk; for (int i = first1; i <= last1; i++) { /* Skip past changes that apply (in file 1) only to lines before line I. */ while (next != null && next.line1 + next.inserted <= i) next = next.link; /* Compute the marking for line I. */ String prefix = " "; if (next != null && next.line1 <= i) /* The change NEXT covers this line. If lines were deleted here in file 0, this is "changed". Otherwise it is "inserted". */ prefix = (next.deleted > 0) ? "!" : "+"; print_1_line (prefix, file1[i]); } } } } /** Prints an edit script in context diff format. This and its 'unified' variation is used for source code patches. */ public static class UnifiedPrint extends ContextPrint { public UnifiedPrint(Object[] a,Object[] b, Writer w) { super(a,b,w); } public void print_header(String filea,String fileb) { print_context_label ("---", new File(filea), filea); print_context_label ("+++", new File(fileb), fileb); } private void print_number_range (int a, int b) { //translate_range (file, a, b, &trans_a, &trans_b); /* Note: we can have B < A in the case of a range of no lines. In this case, we should print the line number before the range, which is B. */ if (b < a) outfile.print(b + ",0"); else super.print_number_range(',',a,b); } protected void print_hunk(Diff.change hunk) { /* Determine range of line numbers involved in each file. */ analyze_hunk (hunk); if (deletes == 0 && inserts == 0) return; /* Include a context's width before and after. */ first0 = Math.max(first0 - context, 0); first1 = Math.max(first1 - context, 0); last0 = Math.min(last0 + context, file0.length - 1); last1 = Math.min(last1 + context, file1.length - 1); outfile.print("@@ -"); print_number_range (first0, last0); outfile.print(" +"); print_number_range (first1, last1); outfile.print(" @@"); /* If we looked for and found a function this is part of, include its name in the header of the diff section. */ print_function(file0,first0); outfile.println(); Diff.change next = hunk; int i = first0; int j = first1; while (i <= last0 || j <= last1) { /* If the line isn't a difference, output the context from file 0. */ if (next == null || i < next.line0) { outfile.print(' '); print_1_line ("", file0[i++]); j++; } else { /* For each difference, first output the deleted part. */ int k = next.deleted; while (k-- > 0) { outfile.print('-'); print_1_line ("", file0[i++]); } /* Then output the inserted part. */ k = next.inserted; while (k-- > 0) { outfile.print('+'); print_1_line ("", file1[j++]); } /* We're done with this hunk, so on to the next! */ next = next.link; } } } } /** Read a text file into an array of String. This provides basic diff functionality. A more advanced diff utility will use specialized objects to represent the text lines, with options to, for example, convert sequences of whitespace to a single space for comparison purposes. */ static String[] slurp(String file) throws IOException { BufferedReader rdr = new BufferedReader(new FileReader(file)); Vector s = new Vector(); for (;;) { String line = rdr.readLine(); if (line == null) break; s.addElement(line); } String[] a = new String[s.size()]; s.copyInto(a); return a; } public static void main(String[] argv) throws IOException { String filea = argv[argv.length - 2]; String fileb = argv[argv.length - 1]; String[] a = slurp(filea); String[] b = slurp(fileb); Diff d = new Diff(a,b); char style = 'n'; for (int i = 0; i < argv.length - 2; ++i) { String f = argv[i]; if (f.startsWith("-")) { for (int j = 1; j < f.length(); ++j) { switch (f.charAt(j)) { case 'e': // Ed style style = 'e'; break; case 'c': // Context diff style = 'c'; break; case 'u': style = 'u'; break; } } } } boolean reverse = style == 'e'; Diff.change script = d.diff_2(reverse); if (script == null) System.err.println("No differences"); else { Base p; Writer w = new OutputStreamWriter(System.out); switch (style) { case 'e': p = new EdPrint(a,b,w); break; case 'c': p = new ContextPrint(a,b,w); break; case 'u': p = new UnifiedPrint(a,b,w); break; default: p = new NormalPrint(a,b,w); } p.print_header(filea,fileb); p.print_script(script); } } public static void doDiff(String[] argv, Writer w) throws IOException { String filea = argv[argv.length - 2]; String fileb = argv[argv.length - 1]; String[] a = slurp(filea); String[] b = slurp(fileb); Diff d = new Diff(a,b); char style = 'n'; for (int i = 0; i < argv.length - 2; ++i) { String f = argv[i]; if (f.startsWith("-")) { for (int j = 1; j < f.length(); ++j) { switch (f.charAt(j)) { case 'e': // Ed style style = 'e'; break; case 'c': // Context diff style = 'c'; break; case 'u': style = 'u'; break; } } } } boolean reverse = style == 'e'; Diff.change script = d.diff_2(reverse); if (script == null) w.write("No differences\n"); else { Base p; switch (style) { case 'e': p = new EdPrint(a,b,w); break; case 'c': p = new ContextPrint(a,b,w); break; case 'u': p = new UnifiedPrint(a,b,w); break; default: p = new NormalPrint(a,b,w); } p.print_header(filea,fileb); p.print_script(script); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?