From 5c2638e2216d74fdb334163cd69528095f0a23bf Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 5 Dec 2022 21:17:27 +0600 Subject: [PATCH] Path.CorrectString --- DTLib/Filesystem/New/Directory.cs | 4 +-- DTLib/Filesystem/New/File.cs | 6 ++-- DTLib/Filesystem/New/Path.cs | 51 +++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/DTLib/Filesystem/New/Directory.cs b/DTLib/Filesystem/New/Directory.cs index bb2a280..9e92358 100644 --- a/DTLib/Filesystem/New/Directory.cs +++ b/DTLib/Filesystem/New/Directory.cs @@ -107,7 +107,7 @@ public static class Directory } // copies directory with symlinks instead of files - public static int SymCopy(string srcdir, string newdir) + /*public static int SymCopy(string srcdir, string newdir) { List files = GetAllFiles(srcdir); if (!srcdir.EndsWith(Путь.Разд)) srcdir += Путь.Разд; @@ -116,5 +116,5 @@ public static class Directory for (; i < files.Count; i++) File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir)); return i; - } + }*/ } diff --git a/DTLib/Filesystem/New/File.cs b/DTLib/Filesystem/New/File.cs index cd8274a..5aa4933 100644 --- a/DTLib/Filesystem/New/File.cs +++ b/DTLib/Filesystem/New/File.cs @@ -55,8 +55,10 @@ public class File public static void Delete(string path) => System.IO.File.Delete(path); - public void Delete(string path) + public static void Copy(string srcPath, string newPath, bool replace = false) { - + System.IO.File.Copy(srcPath, newPath, replace); } + + // public void Delete(string path) } \ No newline at end of file diff --git a/DTLib/Filesystem/New/Path.cs b/DTLib/Filesystem/New/Path.cs index 149892d..607e594 100644 --- a/DTLib/Filesystem/New/Path.cs +++ b/DTLib/Filesystem/New/Path.cs @@ -36,4 +36,55 @@ public static class Path if (path.Contains("..")) throw new Exception($"path <{path}> uses <..>, that's not allowed"); } + + /// Replaces restricted characters in string + public static string CorrectString(string str) + { +#if NETSTANDARD2_1 || NET6_0 || NET7_0 || NET8_0 + var a = str.AsSpan(); +#else + var a = str.ToArray(); +#endif + char[] r = new char[a.Length]; + for (int i = 0; i < a.Length; i++) + { + switch (a[i]) + { + case '/': + case '\\': + case ':': + case ';': + r[i] = '-'; + break; + case '\n': + case '\r': + case ' ': + case '#': + case '%': + case '&': + case '{': + case '}': + case '<': + case '>': + case '*': + case '?': + case '$': + case '!': + case '\'': + case '"': + case '@': + case '+': + case '`': + case '|': + case '=': + r[i] = '_'; + break; + default: + r[i] = a[i]; + break; + } + } + + return new string(r); + } } \ No newline at end of file