internal static String PreProcessForExtendedPathRemoval(String url, bool isFileUrl)
{
// This is the modified URL that we will return
StringBuilder modifiedUrl = new StringBuilder(url);
// ITEM 1 - remove extended path characters.
{
// Keep track of where we are in both the comparison and altered strings.
int curCmpIdx = 0;
int curModIdx = 0;
// If all the '\' have already been converted to '/', just check for //?/ or //./
if ((url.Length - curCmpIdx) >= 4 &&
(String.Compare(url, curCmpIdx, "//?/", 0, 4, StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(url, curCmpIdx, "//./", 0, 4, StringComparison.OrdinalIgnoreCase) == 0))
{
modifiedUrl.Remove(curModIdx, 4);
curCmpIdx += 4;
}
else
{
if (isFileUrl) {
// We need to handle an indefinite number of leading front slashes for file URLs since we could
// get something like:
// file://\\?\
// file:/\\?\
// file:\\?\
// etc...
while (url[curCmpIdx] == '/')
{
curCmpIdx++;
curModIdx++;
}
}
// Remove the extended path characters
if ((url.Length - curCmpIdx) >= 4 &&
(String.Compare(url, curCmpIdx, "\\\\?\\", 0, 4, StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(url, curCmpIdx, "\\\\?/", 0, 4, StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(url, curCmpIdx, "\\\\.\\", 0, 4, StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(url, curCmpIdx, "\\\\./", 0, 4, StringComparison.OrdinalIgnoreCase) == 0))
{
modifiedUrl.Remove(curModIdx, 4);
curCmpIdx += 4;
}
}
}
// ITEM 2 - convert all slashes to forward slashes, and strip leading slashes.
if (isFileUrl)
{
modifiedUrl.Replace('\\', '/');
int slashCount = 0;
while (slashCount < modifiedUrl.Length && modifiedUrl[slashCount] == '/')
{
slashCount++;
}
modifiedUrl.Remove(0, slashCount);
}
// ITEM 3 - If the path is greater than or equal (due to terminating NULL in windows) MAX_PATH, we throw.
if (modifiedUrl.Length >= Path.MAX_PATH)
{
throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
}
// Create the result string from the StringBuilder
return modifiedUrl.ToString();
}