Skip to content

Commit 9a8202c

Browse files
committed
Fixed bugs and added netstandard2.0 support.
1 parent 6c044b3 commit 9a8202c

File tree

13 files changed

+112
-16
lines changed

13 files changed

+112
-16
lines changed

Hexa.NET.Utilities/AllocationCallbacks.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,28 @@ public void Free(void* ptr)
128128
#endif
129129
Marshal.FreeHGlobal((nint)ptr);
130130
}
131+
132+
#if !NETSTANDARD2_1_OR_GREATER && !NET5_0_OR_GREATER
133+
134+
public unsafe void* Alloc(nint size)
135+
{
136+
return Alloc((nuint)size);
137+
}
138+
139+
public unsafe void* Alloc(int size)
140+
{
141+
return Alloc((nint)size);
142+
}
143+
144+
public unsafe void* ReAlloc(void* ptr, nint size)
145+
{
146+
return ReAlloc(ptr, (nuint)size);
147+
}
148+
149+
public unsafe void* ReAlloc(void* ptr, int size)
150+
{
151+
return ReAlloc(ptr, (nuint)size);
152+
}
153+
#endif
131154
}
132155
}

Hexa.NET.Utilities/ArrayUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static unsafe class ArrayUtils
1616
public static void Add<T>(ref T[] array, T value)
1717
{
1818
Array.Resize(ref array, array.Length + 1);
19-
array[^1] = value;
19+
array[array.Length - 1] = value;
2020
}
2121

2222
/// <summary>

Hexa.NET.Utilities/Collections/ArrayAccessor.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
namespace Hexa.NET.Utilities.Collections
1+
#if NET5_0_OR_GREATER
2+
namespace Hexa.NET.Utilities.Collections
23
{
34
using System;
45
using System.Collections.Generic;
@@ -42,4 +43,5 @@ static ArrayAccessor()
4243

4344
#nullable restore
4445
}
45-
}
46+
}
47+
#endif

Hexa.NET.Utilities/Collections/ListExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
namespace Hexa.NET.Utilities.Collections
1+
#if NET5_0_OR_GREATER
2+
namespace Hexa.NET.Utilities.Collections
23
{
34
using System.Collections.Generic;
45
using System.Runtime.CompilerServices;
@@ -34,4 +35,5 @@ public static void MutateItem<T>(this IList<T> list, int index, Func<T, T> func)
3435
list[index] = item;
3536
}
3637
}
37-
}
38+
}
39+
#endif

Hexa.NET.Utilities/Hexa.NET.Utilities.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net9.0;net8.0;net6.0;netstandard2.1</TargetFrameworks>
4+
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@@ -19,7 +19,7 @@
1919
<PropertyGroup>
2020
<PackageId>Hexa.NET.Utilities</PackageId>
2121
<AssemblyVersion>1.0.0</AssemblyVersion>
22-
<PackageVersion>2.1.11</PackageVersion>
22+
<PackageVersion>2.1.12</PackageVersion>
2323
<Authors>Juna</Authors>
2424
<AssemblyName>Hexa.NET.Utilities</AssemblyName>
2525
<PackageProjectUrl>https://github.com/HexaEngine/Hexa.NET.Utilities</PackageProjectUrl>
@@ -41,7 +41,7 @@
4141
<Content Include="../README.md" Pack="true" PackagePath="\" />
4242
</ItemGroup>
4343

44-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
44+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'netstandard2.0'">
4545
<PackageReference Include="System.Memory" Version="4.6.0" />
4646
</ItemGroup>
4747

Hexa.NET.Utilities/IAllocationCallbacks.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface IAllocationCallbacks
88
#endif
99
);
1010

11+
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
1112
unsafe void* Alloc(nint size
1213
#if TRACELEAK
1314
, string name
@@ -33,13 +34,29 @@ public interface IAllocationCallbacks
3334
#endif
3435
);
3536
}
37+
#else
38+
39+
unsafe void* Alloc(nint size
40+
#if TRACELEAK
41+
, string name
42+
#endif
43+
);
44+
45+
unsafe void* Alloc(int size
46+
#if TRACELEAK
47+
, string name
48+
#endif
49+
);
50+
51+
#endif
3652

3753
unsafe void* ReAlloc(void* ptr, nuint size
3854
#if TRACELEAK
3955
, string name
4056
#endif
4157
);
4258

59+
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
4360
unsafe void* ReAlloc(void* ptr, nint size
4461
#if TRACELEAK
4562
, string name
@@ -65,6 +82,21 @@ public interface IAllocationCallbacks
6582
#endif
6683
);
6784
}
85+
#else
86+
87+
unsafe void* ReAlloc(void* ptr, nint size
88+
#if TRACELEAK
89+
, string name
90+
#endif
91+
);
92+
93+
unsafe void* ReAlloc(void* ptr, int size
94+
#if TRACELEAK
95+
, string name
96+
#endif
97+
);
98+
99+
#endif
68100

69101
unsafe void Free(void* ptr);
70102
}

Hexa.NET.Utilities/ListPool.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public void Return(List<T> list)
5050
/// </summary>
5151
public void Clear()
5252
{
53+
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
5354
pool.Clear();
55+
#endif
5456
}
5557
}
5658
}

Hexa.NET.Utilities/StdString.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ public StdString(string s)
188188
data = AllocT<byte>(byteCount + 1);
189189
ZeroMemoryT(data, byteCount + 1);
190190
capacity = size = byteCount;
191-
Encoding.UTF8.GetBytes(s, new Span<byte>(data, byteCount));
191+
fixed (char* chars = s)
192+
{
193+
Encoding.UTF8.GetBytes(chars, s.Length, data, byteCount);
194+
}
192195
}
193196

194197
/// <summary>
@@ -1187,12 +1190,21 @@ public override bool Equals(object? obj)
11871190
/// <returns>The hash code for the string.</returns>
11881191
public override int GetHashCode()
11891192
{
1193+
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
11901194
HashCode hashCode = new();
11911195
for (int i = 0; i < size; i++)
11921196
{
11931197
hashCode.Add(data[i]);
11941198
}
11951199
return hashCode.ToHashCode();
1200+
#else
1201+
int hash = 17;
1202+
for (int i = 0; i < size; i++)
1203+
{
1204+
hash = hash * 31 + data[i].GetHashCode();
1205+
}
1206+
return hash;
1207+
#endif
11961208
}
11971209

11981210
/// <summary>

Hexa.NET.Utilities/StdWString.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public StdWString(string s)
4343
var byteCount = Encoding.Unicode.GetByteCount(s) / sizeof(char);
4444
data = AllocT<char>(byteCount + 1);
4545
capacity = size = s.Length;
46-
Encoding.Unicode.GetBytes(s, new Span<byte>(data, byteCount * sizeof(char)));
46+
fixed (char* chars = s)
47+
{
48+
Encoding.Unicode.GetBytes(chars, s.Length, (byte*)data, byteCount);
49+
}
4750
data[size] = '\0';
4851
}
4952

@@ -863,7 +866,10 @@ public readonly ReadOnlySpan<char> AsReadOnlySpan()
863866
/// <returns><c>true</c> if the two strings are equal; otherwise, <c>false</c>.</returns>
864867
public static bool operator ==(StdWString str1, string str2)
865868
{
866-
return str1.Compare(str2);
869+
fixed (char* pStr = str2)
870+
{
871+
return str1.Compare(new ReadOnlySpan<char>(pStr, str2.Length));
872+
}
867873
}
868874

869875
/// <summary>
@@ -874,7 +880,10 @@ public readonly ReadOnlySpan<char> AsReadOnlySpan()
874880
/// <returns><c>true</c> if the two strings are not equal; otherwise, <c>false</c>.</returns>
875881
public static bool operator !=(StdWString str1, string str2)
876882
{
877-
return !str1.Compare(str2);
883+
fixed (char* pStr = str2)
884+
{
885+
return !str1.Compare(new ReadOnlySpan<char>(pStr, str2.Length));
886+
}
878887
}
879888

880889
/// <summary>
@@ -932,7 +941,10 @@ public override bool Equals(object? obj)
932941
}
933942
if (obj is string str)
934943
{
935-
return Compare(str);
944+
fixed (char* pStr = str)
945+
{
946+
return Compare(new ReadOnlySpan<char>(pStr, str.Length));
947+
}
936948
}
937949
return false;
938950
}
@@ -943,12 +955,21 @@ public override bool Equals(object? obj)
943955
/// <returns>The hash code for the string.</returns>
944956
public override int GetHashCode()
945957
{
958+
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
946959
HashCode hashCode = new();
947960
for (int i = 0; i < size; i++)
948961
{
949962
hashCode.Add(data[i]);
950963
}
951964
return hashCode.ToHashCode();
965+
#else
966+
int hash = 17;
967+
for (int i = 0; i < size; i++)
968+
{
969+
hash = hash * 31 + data[i].GetHashCode();
970+
}
971+
return hash;
972+
#endif
952973
}
953974

954975
/// <summary>

Hexa.NET.Utilities/Threading/ThreadDispatcher.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ protected virtual void Dispose(bool disposing)
356356
{
357357
if (!disposedValue)
358358
{
359+
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
359360
waitingQueue.Clear();
361+
#endif
360362
disposedValue = true;
361363
}
362364
}

0 commit comments

Comments
 (0)