Skip to content

Commit

Permalink
optimize the parse of diff source, use StringBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirtyDegreesRay committed Nov 11, 2017
1 parent 562dc13 commit 5585812
Showing 1 changed file with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.orhanobut.logger.Logger;
import com.thirtydegreesray.openhub.mvp.model.GitHubName;
import com.thirtydegreesray.openhub.util.GitHubHelper;
import com.thirtydegreesray.openhub.util.StringUtils;
Expand Down Expand Up @@ -171,13 +172,13 @@ private static String generateDiffHtml(@NonNull String diffSource, @NonNull Stri
"<meta name=\"viewport\" content=\"width=device-width; initial-scale=1.0;\"/>" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"./" + skin + "\">\n" +
"<style>" +
"body {background: " + backgroundColor + ";}" +
".pre {" +
"background: " + backgroundColor + "; " +
" word-wrap: " + (wrap ? "break-word" : "normal") + "; " +
" white-space: " + (wrap ? "pre-wrap" : "pre") + "; " +
"}" +
"</style>" +
"body {background: " + backgroundColor + ";}\n" +
".pre {\n" +
"background: " + backgroundColor + ";\n" +
" word-wrap: " + (wrap ? "break-word" : "normal") + ";\n" +
" white-space: " + (wrap ? "pre-wrap" : "pre") + ";\n" +
"}\n" +
"</style>\n" +
"</head>\n" +

"<body>\n" +
Expand All @@ -189,7 +190,7 @@ private static String generateDiffHtml(@NonNull String diffSource, @NonNull Stri
}

private static String parseDiffSource(@NonNull String diffSource, boolean wrap) {
String source = "";
StringBuilder source = new StringBuilder();
String[] lines = diffSource.split("\\n");

int addStartLine = -1;
Expand Down Expand Up @@ -221,30 +222,32 @@ private static String parseDiffSource(@NonNull String diffSource, boolean wrap)
addLineNum = 0;
removeLineNum = 0;
normalLineNum = 0;
} else {
} else if (!line.startsWith("\\")) {
curAddNumber = addStartLine + normalLineNum + addLineNum;
curRemoveNumber = removeStartLine + normalLineNum + removeLineNum;
normalLineNum++;
}
lineNumberStr = getDiffLineNumber(curRemoveNumber == -1 ? "" : String.valueOf(curRemoveNumber),
curAddNumber == -1 ? "" : String.valueOf(curAddNumber));


String lineHtml = "<div " + classStr + ">"
+ (wrap ? "" : lineNumberStr + getBlank(1))
+ line
+ "</div>";
source += "\n" + lineHtml;
source.append("\n")
.append("<div ").append(classStr).append(">")
.append(wrap ? "" : lineNumberStr + getBlank(1))
.append(line)
.append("</div>");
}

return source;
return source.toString();
}


private static String getDiffLineNumber(String removeNumber, String addNumber){
int minLength = 4;
return getBlank(minLength - removeNumber.length()) + removeNumber +
getBlank(1) +
getBlank(minLength - addNumber.length()) + addNumber;
return new StringBuilder().append(getBlank(minLength - removeNumber.length()))
.append(removeNumber)
.append(getBlank(1))
.append(getBlank(minLength - addNumber.length()))
.append(addNumber)
.toString();
}

private static String getBlank(int num){
Expand Down

0 comments on commit 5585812

Please sign in to comment.