I can't tell what environment you might be using based off of your separators, but I have never seen a library like this before.
So using reflector and System.IO.Path
as a basis it isn't difficult to reinvent the wheel.
- Create an instance of this class
- Supply your separator characters in the CTor
- Optionally change the
InvalidPathChars
if needed.
This is pretty much the code that is used by the framework so it should be just as fast or only a negligible difference. May or may not be faster than RegEx, it is probably worth a test though.
class ArbitraryPath
{
private readonly char _directorySeparatorChar;
private readonly char _altDirectorySeparatorChar;
private readonly char _volumeSeparatorChar;
public ArbitraryPath(char directorySeparatorChar, char altDirectorySeparatorChar, char volumeSeparatorChar)
{
_directorySeparatorChar = directorySeparatorChar;
_altDirectorySeparatorChar = altDirectorySeparatorChar;
_volumeSeparatorChar = volumeSeparatorChar;
}
public string Combine(string path1, string path2)
{
if ((path1 == null) || (path2 == null))
{
throw new ArgumentNullException((path1 == null) ? "path1" : "path2");
}
CheckInvalidPathChars(path1);
CheckInvalidPathChars(path2);
if (path2.Length == 0)
{
return path1;
}
if (path1.Length == 0)
{
return path2;
}
if (IsPathRooted(path2))
{
return path2;
}
char ch = path1[path1.Length - 1];
if (ch != _directorySeparatorChar && ch != _altDirectorySeparatorChar && ch != _volumeSeparatorChar)
{
return (path1 + _directorySeparatorChar + path2);
}
return (path1 + path2);
}
public bool IsPathRooted(string path)
{
if (path != null)
{
CheckInvalidPathChars(path);
int length = path.Length;
if (length >= 1 && (path[0] == _directorySeparatorChar || path[0] == _altDirectorySeparatorChar) || length >= 2 && path[1] == _volumeSeparatorChar)
{
return true;
}
}
return false;
}
internal static void CheckInvalidPathChars(string path)
{
for (int i = 0; i < path.Length; i++)
{
int num2 = path[i];
if (num2 == 0x22 || num2 == 60 || num2 == 0x3e || num2 == 0x7c || num2 < 0x20)
{
throw new ArgumentException("Argument_InvalidPathChars");
}
}
}
}