Example usage for java.lang StringIndexOutOfBoundsException toString

List of usage examples for java.lang StringIndexOutOfBoundsException toString

Introduction

In this page you can find the example usage for java.lang StringIndexOutOfBoundsException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:com.searchcode.app.service.CodeMatcher.java

/**
 * If changing anything in here be wary of performance issues as it is the slowest method by a long shot.
 * Be especially careful of branch prediction issues which is why this method has been re-written several times
 * just to avoid those issues even though the result was a LONGER method
 * TODO wring more performance out of this method where possible
 *///from  w w w  .  j  ava2s . c  om
public List<CodeMatchResult> findMatchingLines(List<String> code, List<String> matchTerms,
        boolean highlightLine) {
    List<CodeMatchResult> resultLines = new LinkedList<>();

    int codesize = code.size();
    int searchThrough = codesize > this.MAXLINEDEPTH ? this.MAXLINEDEPTH : codesize;
    int matching = 0;

    // Go through each line finding matching lines
    for (int i = 0; i < searchThrough; i++) {
        String matchRes = code.get(i).toLowerCase().replaceAll("\\s+", " ");
        matching = 0;

        for (String matchTerm : matchTerms) {
            if (matchRes.contains(matchTerm.replace("*", ""))) {
                matching++;
            }
        }

        if (matching != 0) {
            resultLines.add(new CodeMatchResult(code.get(i), true, false, matching, i));
        }
    }

    // Get the adjacent lines
    List<CodeMatchResult> adajacentLines = new LinkedList<>();
    for (CodeMatchResult cmr : resultLines) {
        int linenumber = cmr.getLineNumber();
        int previouslinenumber = linenumber - 1;
        int nextlinenumber = linenumber + 1;

        if (previouslinenumber >= 0 && !this.resultExists(resultLines, previouslinenumber)) {
            adajacentLines.add(
                    new CodeMatchResult(code.get(previouslinenumber), false, false, 0, previouslinenumber));
        }

        if (nextlinenumber < codesize && !this.resultExists(resultLines, nextlinenumber)) {
            adajacentLines.add(new CodeMatchResult(code.get(nextlinenumber), false, false, 0, nextlinenumber));
        }
    }

    resultLines.addAll(adajacentLines);

    // If not matching we probably matched on the filename or past 10000
    if (resultLines.size() == 0) {
        searchThrough = codesize > MATCHLINES ? MATCHLINES : codesize;

        for (int i = 0; i < searchThrough; i++) {
            resultLines.add(new CodeMatchResult(code.get(i), false, false, 0, i));
        }
    }

    // Highlight the lines if required but always escape everything
    if (highlightLine) {
        for (CodeMatchResult cmr : resultLines) {
            if (cmr.isMatching()) {
                String line = Values.EMPTYSTRING;
                try {
                    line = this.highlightLine(cmr.getLine(), matchTerms);
                } catch (StringIndexOutOfBoundsException ex) {
                    Singleton.getLogger().severe("Unable to highlightLine " + cmr.getLine() + " using terms "
                            + String.join(",", matchTerms) + " " + ex.toString());
                }
                cmr.setLine(line);
            } else {
                cmr.setLine(StringEscapeUtils.escapeHtml4(cmr.getLine()));
            }
        }
    } else {
        for (CodeMatchResult cmr : resultLines) {
            cmr.setLine(StringEscapeUtils.escapeHtml4(cmr.getLine()));
        }
    }

    return resultLines;
}