diff --git a/DTLib/DTLib.csproj b/DTLib/DTLib.csproj index 22fa3ba..e346cd4 100644 --- a/DTLib/DTLib.csproj +++ b/DTLib/DTLib.csproj @@ -2,7 +2,7 @@ DTLib - 1.6.4 + 1.6.5 Timerix Library for all my C# projects GIT diff --git a/DTLib/Filesystem/IOPath.cs b/DTLib/Filesystem/IOPath.cs index 15ebf35..5523223 100644 --- a/DTLib/Filesystem/IOPath.cs +++ b/DTLib/Filesystem/IOPath.cs @@ -15,29 +15,40 @@ public readonly struct IOPath public IOPath(char[] path, bool separatorsFixed=false) { + if (path.Length == 0) + throw new Exception("path is null or empty"); Str = separatorsFixed ? new string(path) : FixSeparators(path); } - public IOPath(string path, bool separatorsFixed=false) + public IOPath(string path, bool separatorsFixed=false) : this(path.ToCharArray(), separatorsFixed) { - if (path.IsNullOrEmpty()) - throw new Exception("path is null or empty"); - Str = separatorsFixed ? path : FixSeparators(path.ToCharArray()); } static string FixSeparators(char[] path) { int length = path.Length; - if (path[length-1] == Path.Sep || path[length-1] == Path.NotSep) + char lastChar = path[path.Length - 1]; + if (lastChar == Path.Sep || lastChar == Path.NotSep) length--; // removing trailing sep - char[] fixed_path = new char[length]; + StringBuilder sb = new StringBuilder(); + bool prevWasSeparator = false; for (int i = 0; i < length; i++) { if (path[i] == Path.NotSep) - fixed_path[i] = Path.Sep; - else fixed_path[i] = path[i]; + { + // prevent double separators like this "a//b" + if(!prevWasSeparator) + sb.Append(Path.Sep); + prevWasSeparator = true; + } + else + { + sb.Append(path[i]); + prevWasSeparator = false; + } } - return new string(fixed_path); + + return sb.ToString(); } public static IOPath[] ArrayCast(string[] a, bool separatorsFixed=false) diff --git a/DTLib/Filesystem/Path.cs b/DTLib/Filesystem/Path.cs index e91004d..4d71aac 100644 --- a/DTLib/Filesystem/Path.cs +++ b/DTLib/Filesystem/Path.cs @@ -96,10 +96,13 @@ public static class Path /// returns just dir name or file name with extension public static IOPath LastName(this IOPath path) { + if(path.Length < 2) + return path; int i = path.LastIndexOf(Sep); if (i == path.Length - 1) // ends with separator i = path.LastIndexOf(Sep, i - 1); - if (i == -1) return path; + if (i == -1) + return path; return path.Substring(i + 1); }