Skip to content

Commit

Permalink
Fix SilkMarshal.PtrToStringArray ignores the encoding parameter (#2376)
Browse files Browse the repository at this point in the history
* Add Silk.NET.Core.Tests project

* Add test cases for testing string encoding

* Fix issue where PtrToStringArray was ignoring the encoding parameter
  • Loading branch information
Exanite authored Dec 6, 2024
1 parent 8da3988 commit 52f00d3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Silk.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silk.NET.OpenXR.Extensions.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silk.NET.Assimp.Tests", "src\Assimp\Silk.NET.Assimp.Tests\Silk.NET.Assimp.Tests.csproj", "{12D0A556-7DDF-4902-8911-1DA3F6331149}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silk.NET.Core.Tests", "src\Core\Silk.NET.Core.Tests\Silk.NET.Core.Tests.csproj", "{4D871493-0B88-477A-99A1-3E05561CFAD9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -3771,6 +3773,18 @@ Global
{12D0A556-7DDF-4902-8911-1DA3F6331149}.Release|x64.Build.0 = Release|Any CPU
{12D0A556-7DDF-4902-8911-1DA3F6331149}.Release|x86.ActiveCfg = Release|Any CPU
{12D0A556-7DDF-4902-8911-1DA3F6331149}.Release|x86.Build.0 = Release|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|x64.ActiveCfg = Debug|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|x64.Build.0 = Debug|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|x86.ActiveCfg = Debug|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|x86.Build.0 = Debug|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|Any CPU.Build.0 = Release|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|x64.ActiveCfg = Release|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|x64.Build.0 = Release|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|x86.ActiveCfg = Release|Any CPU
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -4072,6 +4086,7 @@ Global
{25ABCA5E-4FF6-43ED-9A5E-443E1373EC5C} = {90471225-AC23-424E-B62E-F6EC4C6ECAC0}
{01B6FFA0-5B37-44EA-ABDF-7BABD05874C5} = {90471225-AC23-424E-B62E-F6EC4C6ECAC0}
{12D0A556-7DDF-4902-8911-1DA3F6331149} = {6EADA376-E83F-40B7-9539-71DD17AEF7A4}
{4D871493-0B88-477A-99A1-3E05561CFAD9} = {0651C5EF-50AA-4598-8D9C-8F210ADD8490}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F5273D7F-3334-48DF-94E3-41AE6816CD4D}
Expand Down
30 changes: 30 additions & 0 deletions src/Core/Silk.NET.Core.Tests/Silk.NET.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Silk.NET.Core\Silk.NET.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
51 changes: 51 additions & 0 deletions src/Core/Silk.NET.Core.Tests/TestSilkMarshal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using Silk.NET.Core.Native;
using Xunit;

namespace Silk.NET.Core.Tests;

public class TestSilkMarshal
{
private readonly List<NativeStringEncoding> encodings = new()
{
NativeStringEncoding.BStr,
NativeStringEncoding.LPStr,
NativeStringEncoding.LPTStr,
NativeStringEncoding.LPUTF8Str,
NativeStringEncoding.LPWStr,
};

[Fact]
public void TestEncodingString()
{
var input = "Hello world";
foreach (var encoding in encodings)
{
var pointer = SilkMarshal.StringToPtr(input, encoding);
var roundTrip = SilkMarshal.PtrToString(pointer, encoding);
Assert.Equal(input, roundTrip);
}
}

[Fact]
public void TestEncodingStringArray()
{
var inputs = new List<string>()
{
"Hello world",
"Foo",
"Bar",
"123",
};

foreach (var encoding in encodings)
{
var pointer = SilkMarshal.StringArrayToPtr(inputs, encoding);
var roundTrip = SilkMarshal.PtrToStringArray(pointer, inputs.Count, encoding);
for (var i = 0; i < roundTrip.Length; i++)
{
Assert.Equal(inputs[i], roundTrip[i]);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Core/Silk.NET.Core/Native/SilkMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public static unsafe string[] PtrToStringArray
var ptrs = (nint*) input;
for (var i = 0; i < numStrings; i++)
{
ret[i] = PtrToString(ptrs![i]);
ret[i] = PtrToString(ptrs![i], encoding);
}

return ret;
Expand Down

0 comments on commit 52f00d3

Please sign in to comment.