mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-26 02:15:31 +02:00
Normalize pathes without using replaceAll()
This commit is contained in:
parent
4d9a2ca002
commit
30597f0987
1 changed files with 54 additions and 42 deletions
|
@ -11,8 +11,8 @@
|
||||||
* Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
|
* Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* Martin Oberhuber (Wind River) - Fix for bug 161844 - regex matching backslashes
|
* Martin Oberhuber (Wind River) - Fix 161844 - regex matching backslashes
|
||||||
*
|
* Martin Oberhuber (Wind River) - Fix 162781 - normalize without replaceAll()
|
||||||
********************************************************************************/
|
********************************************************************************/
|
||||||
|
|
||||||
package org.eclipse.rse.services.clientserver;
|
package org.eclipse.rse.services.clientserver;
|
||||||
|
@ -35,26 +35,32 @@ public class PathUtility
|
||||||
if (path.indexOf("\\\\") != -1) containsDoubleSlashes = true; //$NON-NLS-1$
|
if (path.indexOf("\\\\") != -1) containsDoubleSlashes = true; //$NON-NLS-1$
|
||||||
if (path.endsWith("\\") || path.endsWith("/")) endsWithSlash = true; //$NON-NLS-1$ //$NON-NLS-2$
|
if (path.endsWith("\\") || path.endsWith("/")) endsWithSlash = true; //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
|
||||||
boolean needsNormalizing = containsForwardSlash || containsDoubleSlashes || endsWithSlash;
|
if (containsForwardSlash || containsDoubleSlashes) {
|
||||||
if (!needsNormalizing) return path;
|
//Replace /->\, then replace \\->\
|
||||||
|
StringBuffer buf = new StringBuffer(path.length());
|
||||||
if (containsForwardSlash)
|
boolean foundBackslash=false;
|
||||||
{
|
for (int i=0; i<path.length(); i++) {
|
||||||
path = path.replace('/', '\\');
|
char c = path.charAt(i);
|
||||||
containsDoubleSlashes = (path.indexOf("\\\\") != -1); //$NON-NLS-1$
|
if (c=='/') {
|
||||||
}
|
c='\\';
|
||||||
|
}
|
||||||
while (containsDoubleSlashes)
|
if (c=='\\') {
|
||||||
{
|
if (!foundBackslash) {
|
||||||
//TODO Improve performance by manually iterating over char array
|
foundBackslash=true;
|
||||||
//need to quote once for the string, then again for the regex
|
buf.append(c);
|
||||||
//Replace "\\" by "\": Regex matcher needs quoting twice in search, once in replacement
|
}
|
||||||
path = path.replaceAll("\\\\\\\\", "\\\\"); //$NON-NLS-1$ //$NON-NLS-2$
|
} else {
|
||||||
containsDoubleSlashes = (path.indexOf("\\\\") != -1); //$NON-NLS-1$
|
foundBackslash=false;
|
||||||
}
|
buf.append(c);
|
||||||
if (endsWithSlash)
|
}
|
||||||
{
|
}
|
||||||
if (!(path.length() == 3)) path = path.substring(0, path.length() - 1);
|
if (endsWithSlash && buf.length()!=3) {
|
||||||
|
buf.deleteCharAt(buf.length()-1);
|
||||||
|
}
|
||||||
|
path = buf.toString();
|
||||||
|
} else if (endsWithSlash && path.length()!=3) {
|
||||||
|
//remove trailing slash only
|
||||||
|
path = path.substring(0, path.length() - 1);
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
@ -71,28 +77,34 @@ public class PathUtility
|
||||||
if (path.indexOf("//") != -1) containsDoubleSlashes = true; //$NON-NLS-1$
|
if (path.indexOf("//") != -1) containsDoubleSlashes = true; //$NON-NLS-1$
|
||||||
if (path.endsWith("\\") || path.endsWith("/")) endsWithSlash = true; //$NON-NLS-1$ //$NON-NLS-2$
|
if (path.endsWith("\\") || path.endsWith("/")) endsWithSlash = true; //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
|
||||||
boolean needsNormalizing = containsBackSlash || containsDoubleSlashes || endsWithSlash;
|
if (containsBackSlash || containsDoubleSlashes) {
|
||||||
if (!needsNormalizing) return path;
|
//Replace \->/, then replace //->/
|
||||||
|
StringBuffer buf = new StringBuffer(path.length());
|
||||||
if (containsBackSlash)
|
boolean foundSlash=false;
|
||||||
{
|
for (int i=0; i<path.length(); i++) {
|
||||||
path = path.replace('\\', '/');
|
char c = path.charAt(i);
|
||||||
containsDoubleSlashes = (path.indexOf("//") != -1); //$NON-NLS-1$
|
if (c=='\\') {
|
||||||
}
|
c='/';
|
||||||
|
}
|
||||||
while (containsDoubleSlashes)
|
if (c=='/') {
|
||||||
{
|
if (!foundSlash) {
|
||||||
//TODO Improve performance by manually iterating over char array
|
foundSlash=true;
|
||||||
path = path.replaceAll("//", "/"); //$NON-NLS-1$ //$NON-NLS-2$
|
buf.append(c);
|
||||||
containsDoubleSlashes = (path.indexOf("//") != -1); //$NON-NLS-1$
|
}
|
||||||
}
|
} else {
|
||||||
|
foundSlash=false;
|
||||||
if (endsWithSlash)
|
buf.append(c);
|
||||||
{
|
}
|
||||||
if (!(path.length() == 1)) path = path.substring(0, path.length() - 1);
|
}
|
||||||
|
if (endsWithSlash && buf.length()!=1) {
|
||||||
|
buf.deleteCharAt(buf.length()-1);
|
||||||
|
}
|
||||||
|
path = buf.toString();
|
||||||
|
} else if (endsWithSlash && path.length()!=1) {
|
||||||
|
//remove trailing slash only
|
||||||
|
path = path.substring(0, path.length() - 1);
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String normalizeVirtualWindows(String path)
|
public static String normalizeVirtualWindows(String path)
|
||||||
|
|
Loading…
Add table
Reference in a new issue