1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-09 01:55:24 +02:00

Improve performance by using pre-compiled regex patterns

This commit is contained in:
Martin Oberhuber 2006-10-31 14:11:25 +00:00
parent 30597f0987
commit f8b92c4393

View file

@ -13,29 +13,28 @@
* Contributors: * Contributors:
* Martin Oberhuber (Wind River) - Fix 161844 - regex matching backslashes * Martin Oberhuber (Wind River) - Fix 161844 - regex matching backslashes
* Martin Oberhuber (Wind River) - Fix 162781 - normalize without replaceAll() * Martin Oberhuber (Wind River) - Fix 162781 - normalize without replaceAll()
* Martin Oberhuber (Wind River) - Use pre-compiled regex Pattern
********************************************************************************/ ********************************************************************************/
package org.eclipse.rse.services.clientserver; package org.eclipse.rse.services.clientserver;
import java.util.regex.Pattern;
import org.eclipse.rse.services.clientserver.archiveutils.AbsoluteVirtualPath; import org.eclipse.rse.services.clientserver.archiveutils.AbsoluteVirtualPath;
import org.eclipse.rse.services.clientserver.archiveutils.ArchiveHandlerManager; import org.eclipse.rse.services.clientserver.archiveutils.ArchiveHandlerManager;
public class PathUtility public class PathUtility
{ {
//Regex pattern: / or \\
private static Pattern badSlashPatternWin=Pattern.compile("/|\\\\\\\\"); //$NON-NLS-1$
public static String normalizeWindows(String path) public static String normalizeWindows(String path)
{ {
if (path == null || path.length() < 2) return path; if (path == null || path.length() < 2) {
boolean containsForwardSlash = false; return path;
boolean containsDoubleSlashes = false; }
boolean endsWithSlash = false; boolean endsWithSlash = (path.endsWith("\\") || path.endsWith("/"));
if (badSlashPatternWin.matcher(path).find()) {
//TODO Improve performance by using a pre-compiled Regex Pattern
if (path.indexOf('/') != -1) containsForwardSlash = true;
if (path.indexOf("\\\\") != -1) containsDoubleSlashes = true; //$NON-NLS-1$
if (path.endsWith("\\") || path.endsWith("/")) endsWithSlash = true; //$NON-NLS-1$ //$NON-NLS-2$
if (containsForwardSlash || containsDoubleSlashes) {
//Replace /->\, then replace \\->\ //Replace /->\, then replace \\->\
StringBuffer buf = new StringBuffer(path.length()); StringBuffer buf = new StringBuffer(path.length());
boolean foundBackslash=false; boolean foundBackslash=false;
@ -64,20 +63,17 @@ public class PathUtility
} }
return path; return path;
} }
//Regex pattern: \ or //
private static Pattern badSlashPatternUnix=Pattern.compile("\\\\|//"); //$NON-NLS-1$
public static String normalizeUnix(String path) public static String normalizeUnix(String path)
{ {
if (path == null || path.length() < 2) return path; if (path == null || path.length() < 2) {
boolean containsBackSlash = false; return path;
boolean containsDoubleSlashes = false; }
boolean endsWithSlash = false; boolean endsWithSlash = (path.endsWith("\\") || path.endsWith("/"));
if (badSlashPatternUnix.matcher(path).find()) {
//TODO Improve performance by using a pre-compiled Regex Pattern
if (path.indexOf('\\') != -1) containsBackSlash = true;
if (path.indexOf("//") != -1) containsDoubleSlashes = true; //$NON-NLS-1$
if (path.endsWith("\\") || path.endsWith("/")) endsWithSlash = true; //$NON-NLS-1$ //$NON-NLS-2$
if (containsBackSlash || containsDoubleSlashes) {
//Replace \->/, then replace //->/ //Replace \->/, then replace //->/
StringBuffer buf = new StringBuffer(path.length()); StringBuffer buf = new StringBuffer(path.length());
boolean foundSlash=false; boolean foundSlash=false;