Skip to content

Commit

Permalink
remake contentEqualsIgnoreEOL and add related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
XenoAmess committed Jun 9, 2020
1 parent 3da8a4f commit 125d490
Show file tree
Hide file tree
Showing 2 changed files with 366 additions and 30 deletions.
348 changes: 340 additions & 8 deletions src/main/java/org/apache/commons/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,12 @@ public static boolean contentEquals(final Reader input1, final Reader input2)
}
}

private enum LastState {
r,
normal,
newLine;
}

/**
* Compares the contents of two Readers to determine if they are equal or
* not, ignoring EOL characters.
Expand All @@ -836,16 +842,342 @@ public static boolean contentEqualsIgnoreEOL(final Reader input1, final Reader i
if (input1 == null ^ input2 == null) {
return false;
}
final BufferedReader br1 = toBufferedReader(input1);
final BufferedReader br2 = toBufferedReader(input2);

String line1 = br1.readLine();
String line2 = br2.readLine();
while (line1 != null && line1.equals(line2)) {
line1 = br1.readLine();
line2 = br2.readLine();
char[] charArray1 = new char[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE];
char[] charArray2 = new char[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE];
int nowPos1 = 0;
int nowPos2 = 0;
int nowRead1;
int nowRead2;
int nowCheck1 = 0;
int nowCheck2 = 0;
boolean readEnd1 = false;
boolean readEnd2 = false;
LastState lastState1 = LastState.newLine;
LastState lastState2 = LastState.newLine;
while (true) {
if (nowPos1 == nowCheck1) {
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos1 = nowCheck1 = 0;
}
do {
nowRead1 = input1.read(charArray1, nowPos1, CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
} while (nowRead1 == 0);
if (nowRead1 == -1) {
readEnd1 = true;
} else {
nowPos1 += nowRead1;
}
}
if (nowPos2 == nowCheck2) {
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos2 = nowCheck2 = 0;
}
do {
nowRead2 = input2.read(charArray2, nowPos2, CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
} while (nowRead2 == 0);
if (nowRead2 == -1) {
readEnd2 = true;
} else {
nowPos2 += nowRead2;
}
}
if (readEnd1) {
if (readEnd2) {
return true;
} else {
switch (lastState1) {
case r:
case newLine:
switch (lastState2) {
case r:
if (charArray2[nowCheck2] == '\n') {
nowCheck2++;
if (nowPos2 == nowCheck2) {
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos2 = nowCheck2 = 0;
}
do {
nowRead2 = input2.read(charArray2, nowPos2,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
} while (nowRead2 == 0);
if (nowRead2 == -1) {
readEnd2 = true;
} else {
nowPos2 += nowRead2;
}
}
return readEnd2;
}
return false;
default:
return false;
}
case normal:
switch (lastState2) {
case normal:
switch (charArray2[nowCheck2]) {
case '\r':
nowCheck2++;
if (nowPos2 == nowCheck2) {
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos2 = nowCheck2 = 0;
}
do {
nowRead2 = input2.read(charArray2, nowPos2,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
} while (nowRead2 == 0);
if (nowRead2 == -1) {
readEnd2 = true;
} else {
nowPos2 += nowRead2;
}
}
if (readEnd2) {
return true;
} else if (charArray2[nowCheck2] == '\n') {
nowCheck2++;
if (nowPos2 == nowCheck2) {
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos2 = nowCheck2 = 0;
}
do {
nowRead2 = input2.read(charArray2, nowPos2,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
} while (nowRead2 == 0);
if (nowRead2 == -1) {
readEnd2 = true;
} else {
nowPos2 += nowRead2;
}
}
return readEnd2;
}
return false;
case '\n':
nowCheck2++;
if (nowPos2 == nowCheck2) {
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos2 = nowCheck2 = 0;
}
do {
nowRead2 = input2.read(charArray2, nowPos2,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
} while (nowRead2 == 0);
if (nowRead2 == -1) {
readEnd2 = true;
} else {
nowPos2 += nowRead2;
}
}
return readEnd2;
default:
return false;
}
default:
return false;
}
default:
//shall never enter
}
}
} else if (readEnd2) {
switch (lastState2) {
case r:
case newLine:
switch (lastState1) {
case r:
if (charArray1[nowCheck1] == '\n') {
nowCheck1++;
if (nowPos1 == nowCheck1) {
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos1 = nowCheck1 = 0;
}
do {
nowRead1 = input1.read(charArray1, nowPos1,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
} while (nowRead1 == 0);
if (nowRead1 == -1) {
readEnd1 = true;
} else {
nowPos1 += nowRead1;
}
}
return readEnd1;
}
return false;
default:
return false;
}
case normal:
switch (lastState1) {
case normal:
switch (charArray1[nowCheck1]) {
case '\r':
nowCheck1++;
if (nowPos1 == nowCheck1) {
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos1 = nowCheck1 = 0;
}
do {
nowRead1 = input1.read(charArray1, nowPos1,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
} while (nowRead1 == 0);
if (nowRead1 == -1) {
readEnd1 = true;
} else {
nowPos1 += nowRead1;
}
}
if (readEnd1) {
return true;
} else if (charArray1[nowCheck1] == '\n') {
nowCheck1++;
if (nowPos1 == nowCheck1) {
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos1 = nowCheck1 = 0;
}
do {
nowRead1 = input1.read(charArray1, nowPos1,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
} while (nowRead1 == 0);
if (nowRead1 == -1) {
readEnd1 = true;
} else {
nowPos1 += nowRead1;
}
}
return readEnd1;
}
return false;
case '\n':
nowCheck1++;
if (nowPos1 == nowCheck1) {
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos1 = nowCheck1 = 0;
}
do {
nowRead1 = input1.read(charArray1, nowPos1,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
} while (nowRead1 == 0);
if (nowRead1 == -1) {
readEnd1 = true;
} else {
nowPos1 += nowRead1;
}
}
return readEnd1;
default:
return false;
}
default:
return false;
}
default:
//shall never enter
}
}

switch (charArray1[nowCheck1]) {
case '\r':
switch (charArray2[nowCheck2]) {
case '\r':
lastState1 = lastState2 = LastState.r;
nowCheck1++;
nowCheck2++;
continue;
case '\n':
nowCheck1++;
if (nowPos1 == nowCheck1) {
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos1 = nowCheck1 = 0;
}
do {
nowRead1 = input1.read(charArray1, nowPos1,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
} while (nowRead1 == 0);
if (nowRead1 == -1) {
readEnd1 = true;
} else {
nowPos1 += nowRead1;
}
}
lastState1 = lastState2 = LastState.newLine;
nowCheck2++;
if (readEnd1) {
continue;
}
if (charArray1[nowCheck1] == '\n') {
nowCheck1++;
}
continue;
default:
return false;
}
case '\n':
switch (charArray2[nowCheck2]) {
case '\n':
lastState1 = lastState2 = LastState.newLine;
nowCheck1++;
nowCheck2++;
continue;
case '\r':
nowCheck2++;
if (nowPos2 == nowCheck2) {
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
nowPos2 = nowCheck2 = 0;
}
do {
nowRead2 = input2.read(charArray2, nowPos2,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
} while (nowRead2 == 0);
if (nowRead2 == -1) {
readEnd2 = true;
} else {
nowPos2 += nowRead2;
}
}
lastState1 = lastState2 = LastState.newLine;
nowCheck1++;
if (readEnd2) {
continue;
}
if (charArray2[nowCheck2] == '\n') {
nowCheck2++;
}
continue;
default:
if (lastState1 == LastState.r) {
lastState1 = LastState.newLine;
nowCheck1++;
continue;
} else {
return false;
}
}
default:
switch (charArray2[nowCheck2]) {
case '\n':
if (lastState2 == LastState.r) {
lastState2 = LastState.newLine;
nowCheck2++;
continue;
} else {
return false;
}
case '\r':
return false;
default:
if (charArray1[nowCheck1] != charArray2[nowCheck2]) {
return false;
}
lastState1 = lastState2 = LastState.normal;
nowCheck1++;
nowCheck2++;
continue;
}
}
}
return Objects.equals(line1, line2);
}

/**
Expand Down
Loading

0 comments on commit 125d490

Please sign in to comment.