Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd2355a8

Browse files
committed
1 parent55e4867 commitd2355a8

File tree

3 files changed

+77
-59
lines changed

3 files changed

+77
-59
lines changed

‎src/main/java/com/github/difflib/unifieddiff/UnifiedDiff.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
publicclassUnifiedDiff {
2727

2828
privateStringheader;
29+
privateStringtail;
2930
privatefinalList<UnifiedDiffFile>files =newArrayList<>();
3031

3132
publicStringgetHeader() {
@@ -44,4 +45,11 @@ public List<UnifiedDiffFile> getFiles() {
4445
returnCollections.unmodifiableList(files);
4546
}
4647

48+
voidsetTailTxt(StringtailTxt) {
49+
this.tail =tailTxt;
50+
}
51+
52+
publicStringgetTail() {
53+
returntail;
54+
}
4755
}

‎src/main/java/com/github/difflib/unifieddiff/UnifiedDiffParser.java‎

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public final class UnifiedDiffParser {
4646
newUnifiedDiffLine(true,"^index\\s[\\da-zA-Z]+\\.\\.[\\da-zA-Z]+(\\s(\\d+))?$",this::processIndex),
4747
newUnifiedDiffLine(true,"^---\\s",this::processFromFile),
4848
newUnifiedDiffLine(true,"^\\+\\+\\+\\s",this::processToFile),
49-
newUnifiedDiffLine(true,UNIFIED_DIFF_CHUNK_REGEXP,this::processChunk)
49+
newUnifiedDiffLine(false,UNIFIED_DIFF_CHUNK_REGEXP,this::processChunk),
50+
newUnifiedDiffLine("^\\s+",this::processNormalLine),
51+
newUnifiedDiffLine("^-",this::processDelLine),
52+
newUnifiedDiffLine("^+",this::processAddLine)
5053
};
5154

5255
privateUnifiedDiffFileactualFile;
@@ -62,20 +65,33 @@ public final class UnifiedDiffParser {
6265
privateUnifiedDiffparse()throwsIOException,UnifiedDiffParserException {
6366
booleanheader =true;
6467
StringheaderTxt ="";
68+
StringtailTxt ="";
6569
while (READER.ready()) {
6670
Stringline =READER.readLine();
67-
LOG.log(Level.INFO,"parsing line {0}",line);
68-
if (processLine(header,line) ==false) {
69-
if (header) {
70-
headerTxt +=line +"\n";
71+
if (line.matches("^\\-\\-\\s+")) {
72+
break;
73+
}else {
74+
LOG.log(Level.INFO,"parsing line {0}",line);
75+
if (processLine(header,line) ==false) {
76+
if (header) {
77+
headerTxt +=line +"\n";
78+
}else {
79+
break;
80+
}
7181
}else {
72-
break;
82+
header =false;
83+
data.setHeader(headerTxt);
7384
}
74-
}else {
75-
header =false;
76-
data.setHeader(headerTxt);
7785
}
7886
}
87+
88+
finalizeChunk();
89+
90+
while (READER.ready()) {
91+
tailTxt +=READER.readLine() +"\n";
92+
}
93+
data.setTailTxt(tailTxt);
94+
7995
returndata;
8096
}
8197

@@ -106,6 +122,10 @@ private boolean processLine(boolean header, String line) throws UnifiedDiffParse
106122
}
107123

108124
privatevoidinitFileIfNecessary() {
125+
if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) {
126+
finalizeChunk();
127+
actualFile =null;
128+
}
109129
if (actualFile ==null) {
110130
actualFile =newUnifiedDiffFile();
111131
data.addFile(actualFile);
@@ -121,60 +141,48 @@ public void processDiff(MatchResult match, String line) {
121141
actualFile.setDiffCommand(line);
122142
}
123143

124-
publicvoidprocessChunk(MatchResult_match,StringchunkStart) {
125-
MatchResultmatch =_match;
126-
try {
127-
128-
while (true) {
129-
130-
List<String>originalTxt =newArrayList<>();
131-
List<String>revisedTxt =newArrayList<>();
132-
133-
intold_ln =match.group(1) ==null ?1 :Integer.parseInt(match.group(1));
134-
intnew_ln =match.group(3) ==null ?1 :Integer.parseInt(match.group(3));
135-
if (old_ln ==0) {
136-
old_ln =1;
137-
}
138-
if (new_ln ==0) {
139-
new_ln =1;
140-
}
141-
142-
while (this.READER.ready()) {
143-
Stringline =READER.readLine();
144-
LOG.log(Level.INFO,"processing chunk line {0}",line);
144+
privateList<String>originalTxt =newArrayList<>();
145+
privateList<String>revisedTxt =newArrayList<>();
146+
privateintold_ln;
147+
privateintnew_ln;
148+
149+
privatevoidfinalizeChunk() {
150+
if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) {
151+
actualFile.getPatch().addDelta(newChangeDelta<>(newChunk<>(
152+
old_ln -1,originalTxt),newChunk<>(
153+
new_ln -1,revisedTxt)));
154+
old_ln =0;
155+
new_ln =0;
156+
originalTxt.clear();
157+
revisedTxt.clear();
158+
}
159+
}
145160

146-
if (line.startsWith(" ") ||line.startsWith("+")) {
147-
revisedTxt.add(line.substring(1));
148-
}
149-
if (line.startsWith(" ") ||line.startsWith("-")) {
150-
originalTxt.add(line.substring(1));
151-
}
152-
if (line.equals("") ||line.startsWith("@@") ||line.startsWith("--")) {
153-
break;
154-
}
155-
}
161+
publicvoidprocessNormalLine(MatchResultmatch,Stringline) {
162+
Stringcline =line.substring(1);
163+
originalTxt.add(cline);
164+
revisedTxt.add(cline);
165+
}
156166

157-
actualFile.getPatch().addDelta(newChangeDelta<>(newChunk<>(
158-
old_ln -1,originalTxt),newChunk<>(
159-
new_ln -1,revisedTxt)));
167+
publicvoidprocessAddLine(MatchResultmatch,Stringline) {
168+
Stringcline =line.substring(1);
169+
revisedTxt.add(cline);
170+
}
160171

161-
if (READER.lastLine().equals("")
162-
||READER.lastLine().startsWith("--")
163-
|| !READER.lastLine().startsWith("@@")) {
164-
break;
165-
}else {
166-
Matcherm =UNIFIED_DIFF_CHUNK_REGEXP.matcher(READER.lastLine());
167-
if (m.find()) {
168-
match =m.toMatchResult();
169-
}else {
170-
break;
171-
}
172-
}
173-
}
172+
publicvoidprocessDelLine(MatchResultmatch,Stringline) {
173+
Stringcline =line.substring(1);
174+
originalTxt.add(cline);
175+
}
174176

175-
}catch (IOExceptionex) {
176-
Logger.getLogger(UnifiedDiffParser.class.getName()).log(Level.SEVERE,null,ex);
177-
thrownewUnifiedDiffParserException(ex);
177+
publicvoidprocessChunk(MatchResultmatch,StringchunkStart) {
178+
finalizeChunk();
179+
old_ln =match.group(1) ==null ?1 :Integer.parseInt(match.group(1));
180+
new_ln =match.group(3) ==null ?1 :Integer.parseInt(match.group(3));
181+
if (old_ln ==0) {
182+
old_ln =1;
183+
}
184+
if (new_ln ==0) {
185+
new_ln =1;
178186
}
179187
}
180188

‎src/test/java/com/github/difflib/unifieddiff/UnifiedDiffParserTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public void testSimpleParse() throws IOException {
6363
UnifiedDiffFilefile1 =diff.getFiles().get(0);
6464
assertThat(file1.getFromFile()).isEqualTo("src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt");
6565
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(3);
66+
67+
assertThat(diff.getTail()).isEqualTo("2.17.1.windows.2\n\n");
6668
}
6769

6870
@Test

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp