From 1ca93bdf66b630e58d979ad2aa40f02e678db5cf Mon Sep 17 00:00:00 2001 From: Vladimir Sitnikov Date: Thu, 30 Oct 2014 22:18:10 +0300 Subject: [PATCH] preformance fix for RegexpCheck --- .../tools/checkstyle/checks/RegexpCheck.java | 35 ++++++------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/RegexpCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/RegexpCheck.java index 9a84aef12..2d9b6b976 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/RegexpCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/RegexpCheck.java @@ -18,10 +18,11 @@ //////////////////////////////////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.checks; -import com.google.common.collect.Lists; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.FileContents; -import java.util.List; +import com.puppycrawl.tools.checkstyle.api.FileText; +import com.puppycrawl.tools.checkstyle.api.LineColumn; + import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -86,9 +87,6 @@ public class RegexpCheck extends AbstractFormatCheck /** Tracks number of errors */ private int mErrorCount; - /** Relates StringBuffer positions to line # and column */ - private final List mCharacters = Lists.newArrayList(); - /** The mMatcher */ private Matcher mMatcher; @@ -168,18 +166,8 @@ public class RegexpCheck extends AbstractFormatCheck @Override public void beginTree(DetailAST aRootAST) { - mCharacters.clear(); final Pattern pattern = getRegexp(); - final String[] lines = getLines(); - final StringBuffer sb = new StringBuffer(); - for (int i = 0; i < lines.length; i++) { - sb.append(lines[i]); - sb.append('\n'); - for (int j = 0; j < (lines[i].length() + 1); j++) { - mCharacters.add(new Integer[] {i + 1, j}); - } - } - mMatcher = pattern.matcher(sb.toString()); + mMatcher = pattern.matcher(getFileContents().getText().getFullText()); mMatchCount = 0; mErrorCount = 0; findMatch(); @@ -200,14 +188,13 @@ public class RegexpCheck extends AbstractFormatCheck logMessage(0); } else if (foundMatch) { - startLine = (mCharacters.get(mMatcher.start()))[0]. - intValue(); - startColumn = (mCharacters.get(mMatcher.start()))[1]. - intValue(); - endLine = (mCharacters.get(mMatcher.end() - 1))[0]. - intValue(); - endColumn = (mCharacters.get(mMatcher.end() - 1))[1]. - intValue(); + final FileText text = getFileContents().getText(); + final LineColumn start = text.lineColumn(mMatcher.start()); + final LineColumn end = text.lineColumn(mMatcher.end() - 1); + startLine = start.getLine(); + startColumn = start.getColumn(); + endLine = end.getLine(); + endColumn = end.getColumn(); if (mIgnoreComments) { final FileContents theFileContents = getFileContents(); ignore = theFileContents.hasIntersectionWithComment(startLine,