@Test public void diffline() { String[][] tests = { { "if (a < b && foo < bar && c > d)", "if (a < b && foo > bar && c > d)", "if (a &lt; b &amp;&amp; foo <span class=\"d\">&lt;</span> bar &amp;&amp; c &gt; d)", "if (a &lt; b &amp;&amp; foo <span class=\"a\">&gt;</span> bar &amp;&amp; c &gt; d)" }, { "foo << 1", "foo >> 1", "foo <span class=\"d\">&lt;&lt;</span> 1", "foo <span class=\"a\">&gt;&gt;</span> 1" }, { "\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, " + "cur, ps_id, ret_url, d_req_time, d_req_mil, h_resp_time, " + "h_resp_mil) \"", "\"(ses_id, mer_id, pass_id, \" + refCol +\" , mer_ref, amnt, " + "cur, ps_id, ret_url, exp_url, d_req_time, d_req_mil, " + "h_resp_time, h_resp_mil) \"", "&quot;(ses_id, mer_id, pass_id, &quot; + refCol +&quot; , mer_ref, amnt, " + "cur, ps_id, ret_url, d_req_time, d_req_mil, h_resp_time, " + "h_resp_mil) &quot;", "&quot;(ses_id, mer_id, pass_id, &quot; + refCol +&quot; , mer_ref, amnt, " + "cur, ps_id, ret_url, <span class=\"a\">exp_url, " + "</span>d_req_time, d_req_mil, h_resp_time, h_resp_mil) &quot;" }, { "\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);", "\"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\", values);", "&quot;VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)&quot;, values);", "&quot;VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?<span " + "class=\"a\">, ?</span>)&quot;, values);" }, { "char *config_list = NULL;", "char **config_list = NULL;", "char *config_list = NULL;", "char *<span class=\"a\">*</span>config_list = NULL;" }, { "char **config_list = NULL;", "char *config_list = NULL;", "char *<span class=\"d\">*</span>config_list = NULL;", "char *config_list = NULL;" }, { "* An error occured or there is non-numeric stuff at the end", "* An error occurred or there is non-numeric stuff at the end", "* An error occured or there is non-numeric stuff at the end", "* An error occur<span class=\"a\">r</span>ed or there is " + "non-numeric stuff at the end" } }; for (int i = 0; i < tests.length; i++) { String[] strings = Util.diffline( new StringBuilder(tests[i][0]), new StringBuilder(tests[i][1])); assertEquals("" + i + "," + 0, tests[i][2], strings[0]); assertEquals("" + i + "," + 1, tests[i][3], strings[1]); } }
public static String[] diffline(StringBuilder line1, StringBuilder line2) { String[] ret = new String[2]; int s = 0; int m = line1.length() - 1; int n = line2.length() - 1; while (s <= m && s <= n && (line1.charAt(s) == line2.charAt(s))) { s++; } while (s <= m && s <= n && (line1.charAt(m) == line2.charAt(n))) { m--; n--; } if (s <= m) { StringBuilder sb = new StringBuilder(); sb.append(Util.htmlize(line1.substring(0, s))); sb.append(HtmlConsts.SPAN_D); sb.append(Util.htmlize(line1.substring(s, m + 1))); sb.append(HtmlConsts.ZSPAN); sb.append(Util.htmlize(line1.substring(m + 1, line1.length()))); ret[0] = sb.toString(); } else { ret[0] = Util.htmlize(line1.toString()); } if (s <= n) { StringBuilder sb = new StringBuilder(); sb.append(Util.htmlize(line2.substring(0, s))); sb.append(HtmlConsts.SPAN_A); sb.append(Util.htmlize(line2.substring(s, n + 1))); sb.append(HtmlConsts.ZSPAN); sb.append(Util.htmlize(line2.substring(n + 1, line2.length()))); ret[1] = sb.toString(); } else { ret[1] = Util.htmlize(line2.toString()); } return ret; }
Util { public static String[] diffline(StringBuilder line1, StringBuilder line2) { String[] ret = new String[2]; int s = 0; int m = line1.length() - 1; int n = line2.length() - 1; while (s <= m && s <= n && (line1.charAt(s) == line2.charAt(s))) { s++; } while (s <= m && s <= n && (line1.charAt(m) == line2.charAt(n))) { m--; n--; } if (s <= m) { StringBuilder sb = new StringBuilder(); sb.append(Util.htmlize(line1.substring(0, s))); sb.append(HtmlConsts.SPAN_D); sb.append(Util.htmlize(line1.substring(s, m + 1))); sb.append(HtmlConsts.ZSPAN); sb.append(Util.htmlize(line1.substring(m + 1, line1.length()))); ret[0] = sb.toString(); } else { ret[0] = Util.htmlize(line1.toString()); } if (s <= n) { StringBuilder sb = new StringBuilder(); sb.append(Util.htmlize(line2.substring(0, s))); sb.append(HtmlConsts.SPAN_A); sb.append(Util.htmlize(line2.substring(s, n + 1))); sb.append(HtmlConsts.ZSPAN); sb.append(Util.htmlize(line2.substring(n + 1, line2.length()))); ret[1] = sb.toString(); } else { ret[1] = Util.htmlize(line2.toString()); } return ret; } }
Util { public static String[] diffline(StringBuilder line1, StringBuilder line2) { String[] ret = new String[2]; int s = 0; int m = line1.length() - 1; int n = line2.length() - 1; while (s <= m && s <= n && (line1.charAt(s) == line2.charAt(s))) { s++; } while (s <= m && s <= n && (line1.charAt(m) == line2.charAt(n))) { m--; n--; } if (s <= m) { StringBuilder sb = new StringBuilder(); sb.append(Util.htmlize(line1.substring(0, s))); sb.append(HtmlConsts.SPAN_D); sb.append(Util.htmlize(line1.substring(s, m + 1))); sb.append(HtmlConsts.ZSPAN); sb.append(Util.htmlize(line1.substring(m + 1, line1.length()))); ret[0] = sb.toString(); } else { ret[0] = Util.htmlize(line1.toString()); } if (s <= n) { StringBuilder sb = new StringBuilder(); sb.append(Util.htmlize(line2.substring(0, s))); sb.append(HtmlConsts.SPAN_A); sb.append(Util.htmlize(line2.substring(s, n + 1))); sb.append(HtmlConsts.ZSPAN); sb.append(Util.htmlize(line2.substring(n + 1, line2.length()))); ret[1] = sb.toString(); } else { ret[1] = Util.htmlize(line2.toString()); } return ret; } private  Util(); }
Util { public static String[] diffline(StringBuilder line1, StringBuilder line2) { String[] ret = new String[2]; int s = 0; int m = line1.length() - 1; int n = line2.length() - 1; while (s <= m && s <= n && (line1.charAt(s) == line2.charAt(s))) { s++; } while (s <= m && s <= n && (line1.charAt(m) == line2.charAt(n))) { m--; n--; } if (s <= m) { StringBuilder sb = new StringBuilder(); sb.append(Util.htmlize(line1.substring(0, s))); sb.append(HtmlConsts.SPAN_D); sb.append(Util.htmlize(line1.substring(s, m + 1))); sb.append(HtmlConsts.ZSPAN); sb.append(Util.htmlize(line1.substring(m + 1, line1.length()))); ret[0] = sb.toString(); } else { ret[0] = Util.htmlize(line1.toString()); } if (s <= n) { StringBuilder sb = new StringBuilder(); sb.append(Util.htmlize(line2.substring(0, s))); sb.append(HtmlConsts.SPAN_A); sb.append(Util.htmlize(line2.substring(s, n + 1))); sb.append(HtmlConsts.ZSPAN); sb.append(Util.htmlize(line2.substring(n + 1, line2.length()))); ret[1] = sb.toString(); } else { ret[1] = Util.htmlize(line2.toString()); } return ret; } private  Util(); static String prehtmlize(String s); static String prehtmlize(CharSequence q); static void qurlencode(String str, Appendable dest); static String htmlize(String s); static String htmlize(CharSequence q); static void htmlize(CharSequence q, Appendable dest, boolean pre); static void htmlize(CharSequence q, Appendable dest); static void htmlize(char[] cs, int length, Appendable dest); static String breadcrumbPath(String urlPrefix, String path); static String breadcrumbPath(String urlPrefix, String path, char sep); static String breadcrumbPath(String urlPrefix, String path,
            char sep, String urlPostfix, boolean compact); static String breadcrumbPath(String urlPrefix, String path,
            char sep, String urlPostfix, boolean compact, boolean isDir); static String getCanonicalPath(String path, char sep); static String getEmail(String author); static String readableSize(long num); static String readableCount(long count); static String encode(String s); static void encode(String s, Appendable dest); static String encodeURL(String urlStr); static void readableLine(int num, Writer out, Annotation annotation,
            String userPageLink, String userPageSuffix, String project); static void readableLine(int num, Writer out, Annotation annotation, String userPageLink,
            String userPageSuffix, String project, boolean skipNewline); static String path2uid(String path, String date); static String uid2url(String uid); static String fixPathIfWindows(String path); static void writeHAD(Writer out, String ctxE, String entry,
            boolean is_dir); static String URIEncode(String q); static void URIEncode(String str, Appendable dest); static void appendQuery(StringBuilder buf, String key,
            String value); static String URIEncodePath(String path); static String formQuoteEscape(String q); static String[] diffline(StringBuilder line1, StringBuilder line2); @SuppressWarnings("boxing") static void dumpConfiguration(Appendable out); static void dump(Writer out, Reader in); static boolean dump(Writer out, File dir, String filename,
            boolean compressed); static boolean dump(Writer out, File file, boolean compressed); static boolean dumpXref(Writer out, File file, boolean compressed,
            String contextPath); static void dumpXref(Writer out, Reader in, String contextPath); static String jsStringLiteral(String str); static String stripPathPrefix(String prefix, String fullPath); static String createSlider(int offset, int limit, int size); static String createSlider(int offset, int limit, long size, HttpServletRequest request); static boolean isHttpUri(String string); static String redactUrl(String path); static String linkify(String url); static String linkify(String url, boolean newTab); static String buildLink(String name, Map<String, String> attrs); static String buildLink(String name, String url); static String buildLink(String name, String url, boolean newTab); static String linkifyPattern(String text, Pattern pattern, String name, String url); static String completeUrl(String url, HttpServletRequest req); static Map<String, List<String>> getQueryParams(final URL url); }
Util { public static String[] diffline(StringBuilder line1, StringBuilder line2) { String[] ret = new String[2]; int s = 0; int m = line1.length() - 1; int n = line2.length() - 1; while (s <= m && s <= n && (line1.charAt(s) == line2.charAt(s))) { s++; } while (s <= m && s <= n && (line1.charAt(m) == line2.charAt(n))) { m--; n--; } if (s <= m) { StringBuilder sb = new StringBuilder(); sb.append(Util.htmlize(line1.substring(0, s))); sb.append(HtmlConsts.SPAN_D); sb.append(Util.htmlize(line1.substring(s, m + 1))); sb.append(HtmlConsts.ZSPAN); sb.append(Util.htmlize(line1.substring(m + 1, line1.length()))); ret[0] = sb.toString(); } else { ret[0] = Util.htmlize(line1.toString()); } if (s <= n) { StringBuilder sb = new StringBuilder(); sb.append(Util.htmlize(line2.substring(0, s))); sb.append(HtmlConsts.SPAN_A); sb.append(Util.htmlize(line2.substring(s, n + 1))); sb.append(HtmlConsts.ZSPAN); sb.append(Util.htmlize(line2.substring(n + 1, line2.length()))); ret[1] = sb.toString(); } else { ret[1] = Util.htmlize(line2.toString()); } return ret; } private  Util(); static String prehtmlize(String s); static String prehtmlize(CharSequence q); static void qurlencode(String str, Appendable dest); static String htmlize(String s); static String htmlize(CharSequence q); static void htmlize(CharSequence q, Appendable dest, boolean pre); static void htmlize(CharSequence q, Appendable dest); static void htmlize(char[] cs, int length, Appendable dest); static String breadcrumbPath(String urlPrefix, String path); static String breadcrumbPath(String urlPrefix, String path, char sep); static String breadcrumbPath(String urlPrefix, String path,
            char sep, String urlPostfix, boolean compact); static String breadcrumbPath(String urlPrefix, String path,
            char sep, String urlPostfix, boolean compact, boolean isDir); static String getCanonicalPath(String path, char sep); static String getEmail(String author); static String readableSize(long num); static String readableCount(long count); static String encode(String s); static void encode(String s, Appendable dest); static String encodeURL(String urlStr); static void readableLine(int num, Writer out, Annotation annotation,
            String userPageLink, String userPageSuffix, String project); static void readableLine(int num, Writer out, Annotation annotation, String userPageLink,
            String userPageSuffix, String project, boolean skipNewline); static String path2uid(String path, String date); static String uid2url(String uid); static String fixPathIfWindows(String path); static void writeHAD(Writer out, String ctxE, String entry,
            boolean is_dir); static String URIEncode(String q); static void URIEncode(String str, Appendable dest); static void appendQuery(StringBuilder buf, String key,
            String value); static String URIEncodePath(String path); static String formQuoteEscape(String q); static String[] diffline(StringBuilder line1, StringBuilder line2); @SuppressWarnings("boxing") static void dumpConfiguration(Appendable out); static void dump(Writer out, Reader in); static boolean dump(Writer out, File dir, String filename,
            boolean compressed); static boolean dump(Writer out, File file, boolean compressed); static boolean dumpXref(Writer out, File file, boolean compressed,
            String contextPath); static void dumpXref(Writer out, Reader in, String contextPath); static String jsStringLiteral(String str); static String stripPathPrefix(String prefix, String fullPath); static String createSlider(int offset, int limit, int size); static String createSlider(int offset, int limit, long size, HttpServletRequest request); static boolean isHttpUri(String string); static String redactUrl(String path); static String linkify(String url); static String linkify(String url, boolean newTab); static String buildLink(String name, Map<String, String> attrs); static String buildLink(String name, String url); static String buildLink(String name, String url, boolean newTab); static String linkifyPattern(String text, Pattern pattern, String name, String url); static String completeUrl(String url, HttpServletRequest req); static Map<String, List<String>> getQueryParams(final URL url);  }