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

Commit55e4867

Browse files
committed
1 parent253a94b commit55e4867

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

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

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
packagecom.github.difflib.unifieddiff;
1717

18+
importcom.github.difflib.patch.ChangeDelta;
19+
importcom.github.difflib.patch.Chunk;
1820
importjava.io.BufferedReader;
1921
importjava.io.IOException;
2022
importjava.io.InputStream;
@@ -35,7 +37,7 @@
3537
*/
3638
publicfinalclassUnifiedDiffParser {
3739

38-
privatestaticfinalStringUNIFIED_DIFF_CHUNK_REGEXP ="^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@$";
40+
staticfinalPatternUNIFIED_DIFF_CHUNK_REGEXP =Pattern.compile("^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@");
3941

4042
privatefinalUnifiedDiffReaderREADER;
4143
privatefinalUnifiedDiffdata =newUnifiedDiff();
@@ -62,6 +64,7 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
6264
StringheaderTxt ="";
6365
while (READER.ready()) {
6466
Stringline =READER.readLine();
67+
LOG.log(Level.INFO,"parsing line {0}",line);
6568
if (processLine(header,line) ==false) {
6669
if (header) {
6770
headerTxt +=line +"\n";
@@ -118,25 +121,54 @@ public void processDiff(MatchResult match, String line) {
118121
actualFile.setDiffCommand(line);
119122
}
120123

121-
publicvoidprocessChunk(MatchResultmatch,StringchunkStart) {
124+
publicvoidprocessChunk(MatchResult_match,StringchunkStart) {
125+
MatchResultmatch =_match;
122126
try {
123-
List<String>originalTxt =newArrayList<>();
124-
List<String>revisedTxt =newArrayList<>();
125127

126-
intold_ln =match.group(1) ==null ?1 :Integer.parseInt(match.group(1));
127-
intnew_ln =match.group(3) ==null ?1 :Integer.parseInt(match.group(3));
128+
while (true) {
128129

129-
while (this.READER.ready()) {
130-
Stringline =READER.readLine();
130+
List<String>originalTxt =newArrayList<>();
131+
List<String>revisedTxt =newArrayList<>();
131132

132-
if (line.startsWith(" ") ||line.startsWith("+")) {
133-
revisedTxt.add(line.substring(1));
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;
134137
}
135-
if (line.startsWith(" ") ||line.startsWith("-")) {
136-
originalTxt.add(line.substring(1));
138+
if (new_ln ==0) {
139+
new_ln =1;
137140
}
138-
if (line.equals("")) {
141+
142+
while (this.READER.ready()) {
143+
Stringline =READER.readLine();
144+
LOG.log(Level.INFO,"processing chunk line {0}",line);
145+
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+
}
156+
157+
actualFile.getPatch().addDelta(newChangeDelta<>(newChunk<>(
158+
old_ln -1,originalTxt),newChunk<>(
159+
new_ln -1,revisedTxt)));
160+
161+
if (READER.lastLine().equals("")
162+
||READER.lastLine().startsWith("--")
163+
|| !READER.lastLine().startsWith("@@")) {
139164
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+
}
140172
}
141173
}
142174

@@ -182,6 +214,12 @@ public UnifiedDiffLine(boolean stopsHeaderParsing, String pattern, BiConsumer<Ma
182214
this.stopsHeaderParsing =stopsHeaderParsing;
183215
}
184216

217+
publicUnifiedDiffLine(booleanstopsHeaderParsing,Patternpattern,BiConsumer<MatchResult,String>command) {
218+
this.pattern =pattern;
219+
this.command =command;
220+
this.stopsHeaderParsing =stopsHeaderParsing;
221+
}
222+
185223
publicbooleanprocessLine(Stringline)throwsUnifiedDiffParserException {
186224
Matcherm =pattern.matcher(line);
187225
if (m.find()) {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
packagecom.github.difflib.unifieddiff;
1717

1818
importjava.io.IOException;
19+
importjava.util.regex.Matcher;
20+
importjava.util.regex.Pattern;
1921
importstaticorg.assertj.core.api.Java6Assertions.assertThat;
2022
importorg.junit.After;
2123
importorg.junit.AfterClass;
24+
importstaticorg.junit.Assert.assertEquals;
25+
importstaticorg.junit.Assert.assertTrue;
2226
importorg.junit.Before;
2327
importorg.junit.BeforeClass;
2428
importorg.junit.Test;
@@ -53,6 +57,12 @@ public void testSimpleParse() throws IOException {
5357
UnifiedDiffdiff =UnifiedDiffParser.parseUnifiedDiff(UnifiedDiffParserTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));
5458

5559
System.out.println(diff);
60+
61+
assertThat(diff.getFiles().size()).isEqualTo(2);
62+
63+
UnifiedDiffFilefile1 =diff.getFiles().get(0);
64+
assertThat(file1.getFromFile()).isEqualTo("src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt");
65+
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(3);
5666
}
5767

5868
@Test
@@ -61,4 +71,25 @@ public void testParseDiffBlock() {
6171
assertThat(files).containsExactly("src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java","src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java");
6272
}
6373

74+
@Test
75+
publicvoidtestChunkHeaderParsing() {
76+
Patternpattern =UnifiedDiffParser.UNIFIED_DIFF_CHUNK_REGEXP;
77+
Matchermatcher =pattern.matcher("@@ -189,6 +189,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */");
78+
79+
assertTrue(matcher.find());
80+
assertEquals("189",matcher.group(1));
81+
assertEquals("189",matcher.group(3));
82+
}
83+
84+
@Test
85+
publicvoidtestChunkHeaderParsing2() {
86+
//"^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@.*$"
87+
Patternpattern =UnifiedDiffParser.UNIFIED_DIFF_CHUNK_REGEXP;
88+
Matchermatcher =pattern.matcher("@@ -189,6 +189,7 @@");
89+
90+
assertTrue(matcher.find());
91+
assertEquals("189",matcher.group(1));
92+
assertEquals("189",matcher.group(3));
93+
}
94+
6495
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp