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

View File

@ -19,7 +19,7 @@ public static class TestDtsodV24
public static void TestBaseTypes() public static void TestBaseTypes()
{ {
Logger.Log("c", "-----[TestDtsodV24/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) foreach (var pair in dtsod)
Logger.Log("b", pair.ToString()); Logger.Log("b", pair.ToString());
Logger.Log("g", "test completed"); Logger.Log("g", "test completed");
@ -28,7 +28,7 @@ public static class TestDtsodV24
public static void TestComplexes() public static void TestComplexes()
{ {
Logger.Log("c", "-----[TestDtsodV24/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("h", dtsod.ToString());
Logger.Log("g", "test completed"); Logger.Log("g", "test completed");
} }
@ -36,7 +36,7 @@ public static class TestDtsodV24
public static void TestLists() public static void TestLists()
{ {
Logger.Log("c", "-------[TestDtsodV24/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) foreach (KVPair pair in dtsod)
{ {
var list = new Autoarr<Unitype>(pair.value.VoidPtr, false); var list = new Autoarr<Unitype>(pair.value.VoidPtr, false);
@ -61,7 +61,7 @@ public static class TestDtsodV24
{ {
Logger.Log("c", "--[TestDtsodV24/TestReSerialization]--"); Logger.Log("c", "--[TestDtsodV24/TestReSerialization]--");
var dtsod = new DtsodV24(new DtsodV24(new DtsodV24( 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("h", dtsod.ToString());
Logger.Log("g", "test completed"); Logger.Log("g", "test completed");
} }
@ -70,12 +70,12 @@ public static class TestDtsodV24
{ {
Logger.Log("c", "-------[TestDtsodV24/TestSpeed]-------"); Logger.Log("c", "-------[TestDtsodV24/TestSpeed]-------");
IDtsod dtsod=null; 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 = ""; string text = "";
LogOperationTime( "V23 to V24 conversion", 32, ()=> LogOperationTime( "V23 to V24 conversion", 32, ()=>
text = DtsodConverter.ConvertVersion(new DtsodV23(_text), DtsodVersion.V24).ToString() 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 deserialization", 64, () => dtsod = new DtsodV24(text));
LogOperationTime("V24 serialization", 64, () => text = dtsod.ToString()); LogOperationTime("V24 serialization", 64, () => text = dtsod.ToString());
Logger.Log("g", "test completed"); 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; using System.Runtime.CompilerServices;
namespace DTLib.Filesystem; namespace DTLib.Filesystem;
@ -54,59 +50,40 @@ public static class Path
} }
#if !USE_SPAN #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++) for (int i = 0; i < s.Length; i++)
b[i] = s[i]; b[startIndex+i] = s[i];
} }
#endif #endif
public static IOPath Concat(params IOPath[] parts) public static IOPath Concat(params IOPath[] parts)
{ {
#if USE_SPAN var needSeparator = new bool[parts.Length-1];
Span<bool> int lengthSum = 0;
#else for (int i = 0; i < parts.Length-1; i++)
unsafe
{ {
bool* lengthSum += parts[i].Length;
#endif if (!parts[i].Str.EndsWith(Sep) && !parts[i + 1].Str.StartsWith(Sep))
needSeparator = stackalloc bool[parts.Length-1];
int lengthSum = 0;
for (int i = 0; i < parts.Length-1; i++)
{ {
lengthSum += parts[i].Length; needSeparator[i] = true;
if (!parts[i].Str.EndsWith(Sep) && !parts[i + 1].Str.StartsWith(Sep)) lengthSum++;
{
needSeparator[i] = true;
lengthSum++;
}
else needSeparator[i] = false;
} }
lengthSum += parts[parts.Length-1].Length; else needSeparator[i] = false;
#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
} }
#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 /// returns just dir name or file name with extension