From 70f35e75416468e31f537f33b248caaae29ee587 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 17 Oct 2008 03:49:54 +0000 Subject: [PATCH] optimize handling of package names git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@705452 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/taskdefs/cvslib/CvsTagDiff.java | 90 +++++++++++++------ .../taskdefs/cvs/repository/CVSROOT/history | 4 + 2 files changed, 65 insertions(+), 29 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/cvslib/CvsTagDiff.java b/src/main/org/apache/tools/ant/taskdefs/cvslib/CvsTagDiff.java index 98bbc1a4e..dded52cc4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/cvslib/CvsTagDiff.java +++ b/src/main/org/apache/tools/ant/taskdefs/cvslib/CvsTagDiff.java @@ -87,6 +87,10 @@ public class CvsTagDiff extends AbstractCvsTask { * Token to identify the word file in the rdiff log */ static final String FILE_STRING = "File "; + /** + * Length of token to identify the word file in the rdiff log + */ + static final int FILE_STRING_LENGTH = FILE_STRING.length(); /** * Token to identify the word file in the rdiff log */ @@ -150,6 +154,16 @@ public class CvsTagDiff extends AbstractCvsTask { */ private List packageNames = new ArrayList(); + /** + * temporary list of "File:" + package name + "/" for all packages. + */ + private String[] packageNamePrefixes = null; + + /** + * temporary list of length values for prefixes. + */ + private int[] packageNamePrefixLengths = null; + /** * The package/module to analyze. * @param p the name of the package to analyse @@ -246,20 +260,7 @@ public class CvsTagDiff extends AbstractCvsTask { setCommand(""); File tmpFile = null; try { - if (mypackage != null) { - // support multiple packages - StringTokenizer myTokenizer = new StringTokenizer(mypackage); - while (myTokenizer.hasMoreTokens()) { - String pack = myTokenizer.nextToken(); - packageNames.add(pack); - addCommandArgument(pack); - } - } - for (Iterator iter = getModules().iterator(); iter.hasNext();) { - AbstractCvsTask.Module m = (AbstractCvsTask.Module) iter.next(); - packageNames.add(m.getName()); - // will be added to command line in super.execute() - } + handlePackageNames(); tmpFile = FILE_UTILS.createTempFile("cvstagdiff", ".log", null, true, true); @@ -275,6 +276,8 @@ public class CvsTagDiff extends AbstractCvsTask { writeTagDiff(entries); } finally { + packageNamePrefixes = null; + packageNamePrefixLengths = null; packageNames.clear(); if (tmpFile != null) { tmpFile.delete(); @@ -316,7 +319,8 @@ public class CvsTagDiff extends AbstractCvsTask { String line = reader.readLine(); while (null != line) { - line = removePackageName(line, packageNames); + line = removePackageName(line, packageNamePrefixes, + packageNamePrefixLengths); if (line != null) { // use || in a perl like fashion boolean processed @@ -513,28 +517,56 @@ public class CvsTagDiff extends AbstractCvsTask { } } + /** + * collects package names from the package attribute and nested + * module elements. + */ + private void handlePackageNames() { + if (mypackage != null) { + // support multiple packages + StringTokenizer myTokenizer = new StringTokenizer(mypackage); + while (myTokenizer.hasMoreTokens()) { + String pack = myTokenizer.nextToken(); + packageNames.add(pack); + addCommandArgument(pack); + } + } + for (Iterator iter = getModules().iterator(); iter.hasNext();) { + AbstractCvsTask.Module m = (AbstractCvsTask.Module) iter.next(); + packageNames.add(m.getName()); + // will be added to command line in super.execute() + } + packageNamePrefixes = new String[packageNames.size()]; + packageNamePrefixLengths = new int[packageNames.size()]; + for (int i = 0; i < packageNamePrefixes.length; i++) { + packageNamePrefixes[i] = FILE_STRING + packageNames.get(i) + "/"; + packageNamePrefixLengths[i] = packageNamePrefixes[i].length(); + } + } + + /** * removes a "File: module/" prefix if present. * * @return null if the line was shorter than expected. */ - private static String removePackageName(String line, List packageNames) { + private static String removePackageName(String line, + String[] packagePrefixes, + int[] prefixLengths) { + if (line.length() < FILE_STRING_LENGTH) { + return null; + } boolean matched = false; - for (Iterator iter = packageNames.iterator(); iter.hasNext(); ) { - String toBeRemoved = FILE_STRING + iter.next() + "/"; - int len = toBeRemoved.length(); - if (line.length() > len) { - if (line.startsWith(toBeRemoved)) { - matched = true; - line = line.substring(len); - break; - } + for (int i = 0; i < packagePrefixes.length; i++) { + if (line.startsWith(packagePrefixes[i])) { + matched = true; + line = line.substring(prefixLengths[i]); + break; } } - if (!matched && line.length() > FILE_STRING.length()) { - line = line.substring(FILE_STRING.length()); - matched = true; + if (!matched) { + line = line.substring(FILE_STRING_LENGTH); } - return !matched ? null : line; + return line; } } diff --git a/src/tests/antunit/taskdefs/cvs/repository/CVSROOT/history b/src/tests/antunit/taskdefs/cvs/repository/CVSROOT/history index b99218a3d..8c9f1c5d2 100644 --- a/src/tests/antunit/taskdefs/cvs/repository/CVSROOT/history +++ b/src/tests/antunit/taskdefs/cvs/repository/CVSROOT/history @@ -16,3 +16,7 @@ O48f75279|stefan|/tmp/testoutput/*0|ant module 2||ant module 2 O48f75282|stefan|/tmp/testoutput/*0|ant module 2||ant module 2 O48f75d32|stefan|/tmp/testoutput/*0|ant module 2||ant module 2 O48f75d34|stefan|/tmp/testoutput/*0|antmodule1||antmodule1 +O48f80394|stefan|/tmp/testoutput/*0|ant module 2||ant module 2 +O48f80395|stefan|/tmp/testoutput/*0|antmodule1||antmodule1 +O48f80ad6|stefan|/tmp/testoutput/*0|ant module 2||ant module 2 +O48f80ad7|stefan|/tmp/testoutput/*0|antmodule1||antmodule1