removed unsafe code from Path

This commit is contained in:
Timerix22 2024-01-07 13:41:33 +06:00
parent 5438347bba
commit 7a495926bf
4 changed files with 36 additions and 59 deletions

View File

@ -27,7 +27,7 @@ public class FileLogger : ILogger
{}
public FileLogger(IOPath dir, IOPath programName, ILogFormat format)
: this($"{dir}{Path.Sep}{programName}_{DateTime.Now.ToString(MyTimeFormat.ForFileNames)}.log", format)
: this($"{Path.Concat(dir, programName)}_{DateTime.Now.ToString(MyTimeFormat.ForFileNames)}.log", format)
{}
public FileLogger(IOPath dir, IOPath programName) : this(dir, programName, new DefaultLogFormat())

View File

@ -17,7 +17,7 @@ public static class TestDtsodV23
public static void TestBaseTypes()
{
ColoredConsole.WriteLine("c", "-----[TestDtsodV23/TestBaseTypes]-----");
DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}base_types.dtsod"));
DtsodV23 dtsod = new(File.ReadAllText(Path.Concat("Dtsod","TestResources","DtsodV23", "base_types.dtsod")));
foreach (var pair in dtsod)
ColoredConsole.WriteLine("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString());
ColoredConsole.WriteLine("g", "test completed");
@ -25,7 +25,7 @@ public static class TestDtsodV23
public static void TestLists()
{
ColoredConsole.WriteLine("c", "-------[TestDtsodV23/TestLists]-------");
DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}lists.dtsod"));
DtsodV23 dtsod = new(File.ReadAllText(Path.Concat("Dtsod","TestResources","DtsodV23", "lists.dtsod")));
foreach (var pair in dtsod)
{
ColoredConsole.WriteLine("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c",
@ -39,7 +39,7 @@ public static class TestDtsodV23
public static void TestComplexes()
{
ColoredConsole.WriteLine("c", "-----[TestDtsodV23/TestComplexes]-----");
DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}complexes.dtsod"));
DtsodV23 dtsod = new(File.ReadAllText(Path.Concat("Dtsod","TestResources","DtsodV23", "complexes.dtsod")));
foreach (var complex in dtsod)
{
ColoredConsole.WriteLine("b", complex.Value.GetType().Name + ' ', "w", complex.Key,
@ -55,7 +55,7 @@ public static class TestDtsodV23
{
ColoredConsole.WriteLine("c", "--[TestDtsodV23/TestReSerialization]--");
var dtsod = new DtsodV23(new DtsodV23(new DtsodV23(
new DtsodV23(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}complexes.dtsod")).ToString()).ToString()).ToString());
new DtsodV23(File.ReadAllText(Path.Concat("Dtsod","TestResources","DtsodV23", "complexes.dtsod")))).ToString()).ToString());
ColoredConsole.WriteLine("y", dtsod.ToString());
ColoredConsole.WriteLine("g", "test completed");
}
@ -64,7 +64,7 @@ public static class TestDtsodV23
{
ColoredConsole.WriteLine("c", "-------[TestDtsodV23/TestSpeed]-------");
IDtsod dtsod=null;
string text = File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}messages.dtsod");
string text = File.ReadAllText(Path.Concat("Dtsod","TestResources","DtsodV23", "messages.dtsod"));
LogOperationTime("V21 deserialization",64,()=>dtsod=new DtsodV21(text));
LogOperationTime("V21 serialization", 64, () => _=dtsod.ToString());
LogOperationTime("V23 deserialization", 64, () => dtsod = new DtsodV23(text));
@ -75,7 +75,7 @@ public static class TestDtsodV23
public static void TestMemoryConsumption()
{
ColoredConsole.WriteLine("c", "----[TestDtsodV23/TestMemConsumpt]----");
string text = File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}messages.dtsod");
string text = File.ReadAllText(Path.Concat("Dtsod","TestResources","DtsodV23", "messages.dtsod"));
var a = GC.GetTotalMemory(true);
var dtsods = new DtsodV23[64];
for (int i = 0; i < dtsods.Length; i++)

View File

@ -19,7 +19,7 @@ public static class TestDtsodV24
public static void TestBaseTypes()
{
Logger.Log("c", "-----[TestDtsodV24/TestBaseTypes]-----");
DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV24{Path.Sep}base_types.dtsod"));
DtsodV24 dtsod = new(File.ReadAllText($"Path.Concat("Dtsod","TestResources","DtsodV24", "base_types.dtsod")));
foreach (var pair in dtsod)
Logger.Log("b", pair.ToString());
Logger.Log("g", "test completed");
@ -28,7 +28,7 @@ public static class TestDtsodV24
public static void TestComplexes()
{
Logger.Log("c", "-----[TestDtsodV24/TestComplexes]-----");
DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV24{Path.Sep}complexes.dtsod"));
DtsodV24 dtsod = new(File.ReadAllText($"Path.Concat("Dtsod","TestResources","DtsodV24", "complexes.dtsod")));
Logger.Log("h", dtsod.ToString());
Logger.Log("g", "test completed");
}
@ -36,7 +36,7 @@ public static class TestDtsodV24
public static void TestLists()
{
Logger.Log("c", "-------[TestDtsodV24/TestLists]-------");
DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV24{Path.Sep}lists.dtsod"));
DtsodV24 dtsod = new(File.ReadAllText($"Path.Concat("Dtsod","TestResources","DtsodV24", "lists.dtsod")));
foreach (KVPair pair in dtsod)
{
var list = new Autoarr<Unitype>(pair.value.VoidPtr, false);
@ -61,7 +61,7 @@ public static class TestDtsodV24
{
Logger.Log("c", "--[TestDtsodV24/TestReSerialization]--");
var dtsod = new DtsodV24(new DtsodV24(new DtsodV24(
new DtsodV24(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV24{Path.Sep}complexes.dtsod")).ToString()).ToString()).ToString());
new DtsodV24(File.ReadAllText($"Path.Concat("Dtsod","TestResources","DtsodV24", "complexes.dtsod"))).ToString()).ToString()).ToString()));
Logger.Log("h", dtsod.ToString());
Logger.Log("g", "test completed");
}
@ -70,12 +70,12 @@ public static class TestDtsodV24
{
Logger.Log("c", "-------[TestDtsodV24/TestSpeed]-------");
IDtsod dtsod=null;
string _text = File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}messages.dtsod");
string _text = File.ReadAllText(Path.Concat("Dtsod","TestResources","DtsodV23", "messages.dtsod"));
string text = "";
LogOperationTime( "V23 to V24 conversion", 32, ()=>
text = DtsodConverter.ConvertVersion(new DtsodV23(_text), DtsodVersion.V24).ToString()
);
File.WriteAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV24{Path.Sep}messages.dtsod",text);
File.WriteAllText($"Path.Concat("Dtsod","TestResources","DtsodV24", "messages.dtsod",text));
LogOperationTime("V24 deserialization", 64, () => dtsod = new DtsodV24(text));
LogOperationTime("V24 serialization", 64, () => text = dtsod.ToString());
Logger.Log("g", "test completed");

View File

@ -1,7 +1,3 @@
#if NETSTANDARD2_1 || NET6_0 || NET7_0 || NET8_0
#define USE_SPAN
#endif
using System.Runtime.CompilerServices;
namespace DTLib.Filesystem;
@ -54,59 +50,40 @@ public static class Path
}
#if !USE_SPAN
private static unsafe void CopyTo(this string s, char* b)
private static void CopyTo(this string s, char[] b, int startIndex)
{
for (int i = 0; i < s.Length; i++)
b[i] = s[i];
b[startIndex+i] = s[i];
}
#endif
public static IOPath Concat(params IOPath[] parts)
{
#if USE_SPAN
Span<bool>
#else
unsafe
var needSeparator = new bool[parts.Length-1];
int lengthSum = 0;
for (int i = 0; i < parts.Length-1; i++)
{
bool*
#endif
needSeparator = stackalloc bool[parts.Length-1];
int lengthSum = 0;
for (int i = 0; i < parts.Length-1; i++)
lengthSum += parts[i].Length;
if (!parts[i].Str.EndsWith(Sep) && !parts[i + 1].Str.StartsWith(Sep))
{
lengthSum += parts[i].Length;
if (!parts[i].Str.EndsWith(Sep) && !parts[i + 1].Str.StartsWith(Sep))
{
needSeparator[i] = true;
lengthSum++;
}
else needSeparator[i] = false;
needSeparator[i] = true;
lengthSum++;
}
lengthSum += parts[parts.Length-1].Length;
#if USE_SPAN
Span<char>
#else
char*
#endif
buffer = stackalloc char[lengthSum];
parts[0].Str.CopyTo(buffer);
int copiedChars = parts[0].Length;
for (int i = 1; i < parts.Length; i++)
{
if (needSeparator[i-1])
buffer[copiedChars++] = Sep;
#if USE_SPAN
parts[i].Str.CopyTo(buffer.Slice(copiedChars));
#else
parts[i].Str.CopyTo(buffer+copiedChars);
#endif
copiedChars += parts[i].Length;
}
return new IOPath(new string(buffer), true);
#if !USE_SPAN
else needSeparator[i] = false;
}
#endif
lengthSum += parts[parts.Length-1].Length;
var buffer = new char[lengthSum];
parts[0].Str.CopyTo(buffer, 0);
int copiedChars = parts[0].Length;
for (int i = 1; i < parts.Length; i++)
{
if (needSeparator[i-1])
buffer[copiedChars++] = Sep;
parts[i].Str.CopyTo(buffer, copiedChars);
copiedChars += parts[i].Length;
}
return new IOPath(new string(buffer), true);
}
/// returns just dir name or file name with extension