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

Commit73263b4

Browse files
committed
fixes#46
1 parentfb3a842 commit73263b4

File tree

4 files changed

+83
-28
lines changed

4 files changed

+83
-28
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public final class UnifiedDiffFile {
2525

2626
privateStringdiffCommand;
2727
privateStringfromFile;
28+
privateStringfromTimestamp;
2829
privateStringtoFile;
30+
privateStringtoTimestamp;
2931
privateStringindex;
3032
privatePatch<String>patch =newPatch<>();
3133

@@ -65,6 +67,24 @@ public Patch<String> getPatch() {
6567
returnpatch;
6668
}
6769

70+
publicStringgetFromTimestamp() {
71+
returnfromTimestamp;
72+
}
73+
74+
publicvoidsetFromTimestamp(StringfromTimestamp) {
75+
this.fromTimestamp =fromTimestamp;
76+
}
77+
78+
publicStringgetToTimestamp() {
79+
returntoTimestamp;
80+
}
81+
82+
publicvoidsetToTimestamp(StringtoTimestamp) {
83+
this.toTimestamp =toTimestamp;
84+
}
85+
86+
87+
6888
publicstaticUnifiedDiffFilefrom(StringfromFile,StringtoFile,Patch<String>patch) {
6989
UnifiedDiffFilefile =newUnifiedDiffFile();
7090
file.setFromFile(fromFile);

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

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
publicfinalclassUnifiedDiffReader {
4040

4141
staticfinalPatternUNIFIED_DIFF_CHUNK_REGEXP =Pattern.compile("^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@");
42+
staticfinalPatternTIMESTAMP_REGEXP =Pattern.compile("(\\d{4}-\\d{2}-\\d{2}[T ]\\d{2}:\\d{2}:\\d{2}\\.\\d{3,})");
4243

4344
privatefinalInternalUnifiedDiffReaderREADER;
4445
privatefinalUnifiedDiffdata =newUnifiedDiff();
@@ -49,9 +50,9 @@ public final class UnifiedDiffReader {
4950
privatefinalUnifiedDiffLineTO_FILE =newUnifiedDiffLine(true,"^\\+\\+\\+\\s",this::processToFile);
5051

5152
privatefinalUnifiedDiffLineCHUNK =newUnifiedDiffLine(false,UNIFIED_DIFF_CHUNK_REGEXP,this::processChunk);
52-
privatefinalUnifiedDiffLineLINE_NORMAL =newUnifiedDiffLine("^\\s+",this::processNormalLine);
53+
privatefinalUnifiedDiffLineLINE_NORMAL =newUnifiedDiffLine("^\\s",this::processNormalLine);
5354
privatefinalUnifiedDiffLineLINE_DEL =newUnifiedDiffLine("^-",this::processDelLine);
54-
privatefinalUnifiedDiffLineLINE_ADD =newUnifiedDiffLine("^+",this::processAddLine);
55+
privatefinalUnifiedDiffLineLINE_ADD =newUnifiedDiffLine("^\\+",this::processAddLine);
5556

5657
privateUnifiedDiffFileactualFile;
5758

@@ -81,20 +82,37 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
8182

8283
while (line !=null) {
8384
if (!CHUNK.validLine(line)) {
84-
processLine(line,DIFF_COMMAND,INDEX,FROM_FILE,TO_FILE);
85-
}else {
86-
processLine(line,CHUNK);
85+
initFileIfNecessary();
86+
while (!CHUNK.validLine(line)) {
87+
if (processLine(line,DIFF_COMMAND,INDEX,FROM_FILE,TO_FILE) ==false) {
88+
thrownewUnifiedDiffParserException("expected file start line not found");
89+
}
90+
line =READER.readLine();
91+
}
92+
}
93+
processLine(line,CHUNK);
94+
while ((line =READER.readLine()) !=null) {
95+
if (processLine(line,LINE_NORMAL,LINE_ADD,LINE_DEL) ==false) {
96+
thrownewUnifiedDiffParserException("expected data line not found");
97+
}
98+
if (originalTxt.size() ==old_size &&revisedTxt.size() ==new_size) {
99+
finalizeChunk();
100+
break;
101+
}
87102
}
88103
line =READER.readLine();
104+
if (line ==null ||line.startsWith("--")) {
105+
break;
106+
}
89107
}
90108

91-
finalizeChunk();
92-
93-
StringtailTxt ="";
94-
while (READER.ready()) {
95-
tailTxt +=READER.readLine() +"\n";
109+
if (READER.ready()) {
110+
StringtailTxt ="";
111+
while (READER.ready()) {
112+
tailTxt +=READER.readLine() +"\n";
113+
}
114+
data.setTailTxt(tailTxt);
96115
}
97-
data.setTailTxt(tailTxt);
98116

99117
returndata;
100118
}
@@ -114,30 +132,31 @@ public static UnifiedDiff parseUnifiedDiff(InputStream stream) throws IOExceptio
114132
returnparser.parse();
115133
}
116134

117-
privatevoidprocessLine(Stringline,UnifiedDiffLine...rules)throwsUnifiedDiffParserException {
135+
privatebooleanprocessLine(Stringline,UnifiedDiffLine...rules)throwsUnifiedDiffParserException {
118136
for (UnifiedDiffLinerule :rules) {
119137
if (rule.processLine(line)) {
120138
LOG.info(" >>> processed rule " +rule.toString());
121-
return;
139+
returntrue;
122140
}
123141
}
124142
LOG.info(" >>> no rule matched " +line);
125-
thrownewUnifiedDiffParserException("parsing error at line " +line);
143+
returnfalse;
144+
//throw new UnifiedDiffParserException("parsing error at line " + line);
126145
}
127146

128147
privatevoidinitFileIfNecessary() {
129148
if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) {
130-
finalizeChunk();
131-
actualFile =null;
149+
thrownewIllegalStateException();
132150
}
151+
actualFile =null;
133152
if (actualFile ==null) {
134153
actualFile =newUnifiedDiffFile();
135154
data.addFile(actualFile);
136155
}
137156
}
138157

139158
privatevoidprocessDiff(MatchResultmatch,Stringline) {
140-
initFileIfNecessary();
159+
//initFileIfNecessary();
141160
LOG.log(Level.INFO,"start {0}",line);
142161
String[]fromTo =parseFileNames(READER.lastLine());
143162
actualFile.setFromFile(fromTo[0]);
@@ -181,7 +200,7 @@ private void processDelLine(MatchResult match, String line) {
181200
}
182201

183202
privatevoidprocessChunk(MatchResultmatch,StringchunkStart) {
184-
finalizeChunk();
203+
//finalizeChunk();
185204
old_ln =toInteger(match,1,1);
186205
old_size =toInteger(match,2,0);
187206
new_ln =toInteger(match,3,1);
@@ -195,27 +214,43 @@ private void processChunk(MatchResult match, String chunkStart) {
195214
}
196215

197216
privatestaticIntegertoInteger(MatchResultmatch,intgroup,intdefValue)throwsNumberFormatException {
198-
returnInteger.valueOf(Objects.toString(match.group(group),"" +defValue));
217+
returnInteger.valueOf(Objects.toString(match.group(group),"" +defValue));
199218
}
200219

201220
privatevoidprocessIndex(MatchResultmatch,Stringline) {
202-
initFileIfNecessary();
221+
//initFileIfNecessary();
203222
LOG.log(Level.INFO,"index {0}",line);
204223
actualFile.setIndex(line.substring(6));
205224
}
206225

207226
privatevoidprocessFromFile(MatchResultmatch,Stringline) {
208-
initFileIfNecessary();
227+
//initFileIfNecessary();
209228
actualFile.setFromFile(extractFileName(line));
229+
actualFile.setFromTimestamp(extractTimestamp(line));
210230
}
211231

212232
privatevoidprocessToFile(MatchResultmatch,Stringline) {
213-
initFileIfNecessary();
233+
//initFileIfNecessary();
214234
actualFile.setToFile(extractFileName(line));
235+
actualFile.setToTimestamp(extractTimestamp(line));
236+
}
237+
238+
privateStringextractFileName(String_line) {
239+
Matchermatcher =TIMESTAMP_REGEXP.matcher(_line);
240+
Stringline =_line;
241+
if (matcher.find()) {
242+
line =line.substring(1,matcher.start());
243+
}
244+
returnline.substring(4).replaceFirst("^(a|b)\\/","")
245+
.replace(TIMESTAMP_REGEXP.toString(),"").trim();
215246
}
216247

217-
privateStringextractFileName(Stringline) {
218-
returnline.substring(4).replaceFirst("^(a|b)\\/","");
248+
privateStringextractTimestamp(Stringline) {
249+
Matchermatcher =TIMESTAMP_REGEXP.matcher(line);
250+
if (matcher.find()) {
251+
returnmatcher.group();
252+
}
253+
returnnull;
219254
}
220255

221256
finalclassUnifiedDiffLine {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public void testParseIssue46() throws IOException {
121121
assertThat(diff.getFiles().size()).isEqualTo(1);
122122

123123
UnifiedDiffFilefile1 =diff.getFiles().get(0);
124-
assertThat(file1.getFromFile()).isEqualTo("src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt");
125-
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(3);
124+
assertThat(file1.getFromFile()).isEqualTo(".vhd");
125+
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(1);
126126

127-
assertThat(diff.getTail()).isEqualTo("2.17.1.windows.2\n\n");
127+
assertThat(diff.getTail()).isNull();
128128
}
129129
}

‎src/test/resources/com/github/difflib/unifieddiff/problem_diff_issue46.diff‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--- a.vhd2019-04-18 13:49:39.516149751 +0200
22
+++ b.vhd2019-04-18 11:33:08.372563078 +0200
3-
@@ -2819,3 +2819,3 @@
3+
@@ -2819,3 +2819,2 @@
44
--- some comment
55
-bla
66
-bla

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp