From 1388959ad29d2bfb2ad210afd4bba0d16bb8adcd Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sat, 18 Mar 2023 05:09:50 +0600 Subject: [PATCH] IOPath trailing sep removal --- DTLib/Filesystem/IOPath.cs | 34 +++++++++++++++++++++------------- DTLib/Filesystem/Path.cs | 1 - 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/DTLib/Filesystem/IOPath.cs b/DTLib/Filesystem/IOPath.cs index ffd3a7e..eff25f9 100644 --- a/DTLib/Filesystem/IOPath.cs +++ b/DTLib/Filesystem/IOPath.cs @@ -6,24 +6,33 @@ using System.Runtime.CompilerServices; namespace DTLib.Filesystem; +/// +/// represents filesystem path with proper dir separators and without trailing separator +/// public readonly struct IOPath { public readonly string Str; + public IOPath(char[] path, bool separatorsFixed=false) + { + Str = separatorsFixed ? new string(path) : FixSeparators(path); + } + public IOPath(string path, bool separatorsFixed=false) { - if (!separatorsFixed) - { - var chars = path.ToCharArray(); - for(int i=0; i a) + public static IOPath[] ListCast(IList a) { IOPath[] b = new IOPath[a.Count]; for (int i = 0; i < a.Count; i++) @@ -41,14 +50,13 @@ public readonly struct IOPath } - // public static IOPath operator +(IOPath a, IOPath b) => Concat(a, b); + // public static IOPath operator +(IOPath a, IOPath b) => Path.Concat(a, b); public static bool operator ==(IOPath a, IOPath b) => a.Str==b.Str; public static bool operator !=(IOPath a, IOPath b) => a.Str!=b.Str; public static implicit operator IOPath(string s) => new(s); public static explicit operator string(IOPath p) => p.Str; public override string ToString() => Str; public override bool Equals(object obj) => Str.Equals(obj); - public override int GetHashCode() => Str.GetHashCode(); public char this[int i] => Str[i]; diff --git a/DTLib/Filesystem/Path.cs b/DTLib/Filesystem/Path.cs index 19abc3b..1c4dff6 100644 --- a/DTLib/Filesystem/Path.cs +++ b/DTLib/Filesystem/Path.cs @@ -139,7 +139,6 @@ public static class Path public static IOPath RemoveBase(this IOPath path, IOPath baseDir) { - if (!path.StartsWith(baseDir)) throw new Exception($"path <{path}> doesnt starts with <{baseDir}"); return path.Substring(baseDir.Length);