comparison src/goodjava/util/LineDiff.java @ 1809:90187946d1a4

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 12 May 2024 17:15:33 -0600
parents 69623c62aa34
children
comparison
equal deleted inserted replaced
1808:69623c62aa34 1809:90187946d1a4
11 public static final char DELETE = '<'; 11 public static final char DELETE = '<';
12 public static final char INSERT = '>'; 12 public static final char INSERT = '>';
13 13
14 public static class Diff { 14 public static class Diff {
15 public final char operation; 15 public final char operation;
16 public final int line;
17 public final String text; 16 public final String text;
17 public final int lineOld;
18 public final int lineNew;
18 19
19 private Diff(char operation,String[] lines,int i) { 20 private Diff(char operation,String line,int iOld,int iNew) {
20 this.operation = operation; 21 this.operation = operation;
21 this.line = i+1; 22 this.text = line;
22 this.text = lines[i]; 23 this.lineOld = iOld + 1;
24 this.lineNew = iNew + 1;
23 } 25 }
24 } 26 }
25 27
26 public static String[] stringToLines(String s) { 28 public static String[] stringToLines(String s) {
27 List<String> list = new ArrayList<String>(); 29 List<String> list = new ArrayList<String>();
65 while (iOld < nOld && iNew < nNew) { 67 while (iOld < nOld && iNew < nNew) {
66 if (oldLines[iOld].equals(newLines[iNew])) { 68 if (oldLines[iOld].equals(newLines[iNew])) {
67 iOld++; 69 iOld++;
68 iNew++; 70 iNew++;
69 } else if (opt[iOld+1][iNew] >= opt[iOld][iNew+1]) { 71 } else if (opt[iOld+1][iNew] >= opt[iOld][iNew+1]) {
70 diffs.add( new Diff( DELETE, oldLines, iOld++ ) ); 72 diffs.add( new Diff( DELETE, oldLines[iOld], iOld, iNew ) );
73 iOld++;
71 } else { 74 } else {
72 diffs.add( new Diff( INSERT, newLines, iNew++ ) ); 75 diffs.add( new Diff( INSERT, newLines[iNew], iOld, iNew ) );
76 iNew++;
73 } 77 }
74 } 78 }
75 79
76 // dump out one remainder of one string if the other is exhausted 80 // dump out one remainder of one string if the other is exhausted
77 while( iOld < nOld ) { 81 while( iOld < nOld ) {
78 diffs.add( new Diff( DELETE, oldLines, iOld++ ) ); 82 diffs.add( new Diff( DELETE, oldLines[iOld], iOld, iNew ) );
83 iOld++;
79 } 84 }
80 while( iNew < nNew ) { 85 while( iNew < nNew ) {
81 diffs.add( new Diff( INSERT, newLines, iNew++ ) ); 86 diffs.add( new Diff( INSERT, newLines[iNew], iOld, iNew ) );
87 iNew++;
82 } 88 }
83 89
84 return diffs; 90 return diffs;
85 } 91 }
86 } 92 }