mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 12:02:16 +02:00
Added external diff library stored in DiffEngine.java. Previous diff function has been removed. Now the diff computation is performed through DiffEngine. Results are now supposed to be exactly the same as in the desktop Anki.
This commit is contained in:
parent
570d24e516
commit
e48d75c69e
@ -6,6 +6,8 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import com.ichi2.utils.DiffEngine;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.BroadcastReceiver;
|
||||
@ -64,12 +66,6 @@ public class Ankidroid extends Activity implements Runnable
|
||||
private static final int MAX_FONT_SIZE = 14;
|
||||
private static final int MIN_FONT_SIZE = 3;
|
||||
|
||||
/**
|
||||
* Colors for right and wrong answer
|
||||
*/
|
||||
private static final String RIGHT_COLOR = "#c0ffc0";
|
||||
private static final String WRONG_COLOR = "#ffc0c0";
|
||||
|
||||
/**
|
||||
* Menus
|
||||
*/
|
||||
@ -751,15 +747,24 @@ public class Ankidroid extends Activity implements Runnable
|
||||
// If the user wrote an answer
|
||||
if(writeAnswers)
|
||||
{
|
||||
// Obtain the user answer and the correct answer
|
||||
String userAnswer = mAnswerField.getText().toString();
|
||||
String correctAnswer = (String) currentCard.answer.subSequence(
|
||||
currentCard.answer.indexOf(">")+1,
|
||||
currentCard.answer.lastIndexOf("<"));
|
||||
|
||||
// Obtain the diff and send it to updateCard
|
||||
String diff = diff(userAnswer, correctAnswer);
|
||||
updateCard(diff + "<br/>" + currentCard.answer);
|
||||
if(currentCard != null)
|
||||
{
|
||||
// Obtain the user answer and the correct answer
|
||||
String userAnswer = mAnswerField.getText().toString();
|
||||
String correctAnswer = (String) currentCard.answer.subSequence(
|
||||
currentCard.answer.indexOf(">")+1,
|
||||
currentCard.answer.lastIndexOf("<"));
|
||||
|
||||
// Obtain the diff and send it to updateCard
|
||||
DiffEngine diff = new DiffEngine();
|
||||
updateCard(diff.diff_prettyHtml(
|
||||
diff.diff_main(userAnswer, correctAnswer)) +
|
||||
"<br/>" + currentCard.answer);
|
||||
}
|
||||
else
|
||||
{
|
||||
updateCard("");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -767,142 +772,6 @@ public class Ankidroid extends Activity implements Runnable
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the diff between str1 and str2 with html tags to highlight equal and
|
||||
* non-equal parts of the string
|
||||
*/
|
||||
private String diff(String str1, String str2)
|
||||
{
|
||||
// Replace newlines with <br>
|
||||
str1 = str1.replace("\n", "<br>");
|
||||
String diff = "";
|
||||
|
||||
// If the strings are equal
|
||||
if(str1.equals(str2))
|
||||
{
|
||||
diff = "<span style=\"background-color:" + RIGHT_COLOR + "\">" + str1 + "</span>";
|
||||
}
|
||||
else
|
||||
{
|
||||
int str1Length = str1.length();
|
||||
int str2Length = str2.length();
|
||||
|
||||
// Look for prefixes
|
||||
int n = Math.min(str1Length, str2Length);
|
||||
int pre;
|
||||
for(pre = 0; pre < n; pre++)
|
||||
{
|
||||
if(str1.charAt(pre) != str2.charAt(pre))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the prefix in green
|
||||
if(pre > 0)
|
||||
diff = "<span style=\"background-color:" + RIGHT_COLOR + "\">" +
|
||||
str1.substring(0, pre) + "</span>";
|
||||
|
||||
// Look for suffixes
|
||||
int su;
|
||||
for(su = 1; su <= n - pre; su++)
|
||||
{
|
||||
if(str1.charAt(str1Length - su) != str2.charAt(str2Length - su))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Process the middle of the body of the strings
|
||||
String diffStr1 = str1.substring(pre, str1Length - su + 1);
|
||||
String diffStr2 = str2.substring(pre, str2Length - su + 1);
|
||||
int diffStr1Length = diffStr1.length();
|
||||
int diffStr2Length = diffStr2.length();
|
||||
int j = 0;
|
||||
int i = 0;
|
||||
int lastCorrectChar = 0;
|
||||
while(i < diffStr1Length - 1 && j < diffStr2Length - 1)
|
||||
{
|
||||
// Obtain two chars from string1
|
||||
int k = 2;
|
||||
String bitStr1 = diffStr1.substring(i, i+k);
|
||||
|
||||
// If the two characters are in the second string
|
||||
int index = diffStr2.substring(j).indexOf(bitStr1);
|
||||
if(index >= 0)
|
||||
{
|
||||
// Try to match more than two chars
|
||||
for(k++; i-1+k < diffStr1Length && j-1+index+k < diffStr2Length; k++)
|
||||
{
|
||||
String tryBitStr1 = diffStr1.substring(i, i+k);
|
||||
if(diffStr2.substring(j+index,j+index+k).equals(tryBitStr1))
|
||||
{
|
||||
bitStr1 = tryBitStr1;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the spaces needed
|
||||
String spaces = "";
|
||||
for(int m = 0; m < j + index - i; m++)
|
||||
{
|
||||
spaces += " ";
|
||||
}
|
||||
|
||||
// Print red spaces
|
||||
if(spaces != "")
|
||||
diff += "<span style=\"background-color:" + WRONG_COLOR +
|
||||
"\">" + spaces + "</span>";
|
||||
|
||||
// Print the k characters in green
|
||||
diff += "<span style=\"background-color:" + RIGHT_COLOR +
|
||||
"\">" + bitStr1 + "</span>";
|
||||
|
||||
j += index + k-1;
|
||||
i += k-1;
|
||||
lastCorrectChar = i;
|
||||
}
|
||||
// If they are not in the second string
|
||||
else
|
||||
{
|
||||
// Print in red the current char of string1
|
||||
diff += "<span style=\"background-color:" + WRONG_COLOR +
|
||||
"\">" + diffStr1.charAt(i) + "</span>";
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// If we got out of the last loop without looking at the last char
|
||||
if(i < diffStr1Length)
|
||||
{
|
||||
// Add in red the chars in the tail of str1
|
||||
diff += "<span style=\"background-color:" + WRONG_COLOR +
|
||||
"\">" + diffStr1.charAt(i) + "</span>";
|
||||
}
|
||||
|
||||
// Add as many red spaces as needed
|
||||
String aux = "";
|
||||
for(int m = 0; m < (diffStr2Length-j) - (diffStr1Length-lastCorrectChar); m++)
|
||||
{
|
||||
aux += " ";
|
||||
}
|
||||
if(aux != "")
|
||||
diff += "<span style=\"background-color:" + WRONG_COLOR + "\">" +
|
||||
aux + "</span>";
|
||||
|
||||
|
||||
// Add the suffix in green
|
||||
if(su > 1)
|
||||
diff += "<span style=\"background-color:" + RIGHT_COLOR + "\">" +
|
||||
str1.substring(str1Length - su + 1, str1Length) + "</span>";
|
||||
}
|
||||
|
||||
return diff;
|
||||
}
|
||||
|
||||
|
||||
private boolean writeToFile(InputStream source, String destination) throws IOException
|
||||
{
|
||||
|
1200
src/com/ichi2/utils/DiffEngine.java
Normal file
1200
src/com/ichi2/utils/DiffEngine.java
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user