Skip to content

Commit 52f00d3

Browse files
authored
Fix SilkMarshal.PtrToStringArray ignores the encoding parameter (#2376)
* Add Silk.NET.Core.Tests project * Add test cases for testing string encoding * Fix issue where PtrToStringArray was ignoring the encoding parameter
1 parent 8da3988 commit 52f00d3

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

Silk.NET.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silk.NET.OpenXR.Extensions.
618618
EndProject
619619
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}"
620620
EndProject
621+
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}"
622+
EndProject
621623
Global
622624
GlobalSection(SolutionConfigurationPlatforms) = preSolution
623625
Debug|Any CPU = Debug|Any CPU
@@ -3771,6 +3773,18 @@ Global
37713773
{12D0A556-7DDF-4902-8911-1DA3F6331149}.Release|x64.Build.0 = Release|Any CPU
37723774
{12D0A556-7DDF-4902-8911-1DA3F6331149}.Release|x86.ActiveCfg = Release|Any CPU
37733775
{12D0A556-7DDF-4902-8911-1DA3F6331149}.Release|x86.Build.0 = Release|Any CPU
3776+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3777+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|Any CPU.Build.0 = Debug|Any CPU
3778+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|x64.ActiveCfg = Debug|Any CPU
3779+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|x64.Build.0 = Debug|Any CPU
3780+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|x86.ActiveCfg = Debug|Any CPU
3781+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Debug|x86.Build.0 = Debug|Any CPU
3782+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|Any CPU.ActiveCfg = Release|Any CPU
3783+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|Any CPU.Build.0 = Release|Any CPU
3784+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|x64.ActiveCfg = Release|Any CPU
3785+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|x64.Build.0 = Release|Any CPU
3786+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|x86.ActiveCfg = Release|Any CPU
3787+
{4D871493-0B88-477A-99A1-3E05561CFAD9}.Release|x86.Build.0 = Release|Any CPU
37743788
EndGlobalSection
37753789
GlobalSection(SolutionProperties) = preSolution
37763790
HideSolutionNode = FALSE
@@ -4072,6 +4086,7 @@ Global
40724086
{25ABCA5E-4FF6-43ED-9A5E-443E1373EC5C} = {90471225-AC23-424E-B62E-F6EC4C6ECAC0}
40734087
{01B6FFA0-5B37-44EA-ABDF-7BABD05874C5} = {90471225-AC23-424E-B62E-F6EC4C6ECAC0}
40744088
{12D0A556-7DDF-4902-8911-1DA3F6331149} = {6EADA376-E83F-40B7-9539-71DD17AEF7A4}
4089+
{4D871493-0B88-477A-99A1-3E05561CFAD9} = {0651C5EF-50AA-4598-8D9C-8F210ADD8490}
40754090
EndGlobalSection
40764091
GlobalSection(ExtensibilityGlobals) = postSolution
40774092
SolutionGuid = {F5273D7F-3334-48DF-94E3-41AE6816CD4D}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net6.0</TargetFrameworks>
5+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
6+
<LangVersion>preview</LangVersion>
7+
<Nullable>enable</Nullable>
8+
9+
<IsPackable>false</IsPackable>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\Silk.NET.Core\Silk.NET.Core.csproj" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
18+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
19+
<PackageReference Include="xunit" Version="2.6.6" />
20+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22+
<PrivateAssets>all</PrivateAssets>
23+
</PackageReference>
24+
<PackageReference Include="coverlet.collector" Version="6.0.0">
25+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
26+
<PrivateAssets>all</PrivateAssets>
27+
</PackageReference>
28+
</ItemGroup>
29+
30+
</Project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Collections.Generic;
2+
using Silk.NET.Core.Native;
3+
using Xunit;
4+
5+
namespace Silk.NET.Core.Tests;
6+
7+
public class TestSilkMarshal
8+
{
9+
private readonly List<NativeStringEncoding> encodings = new()
10+
{
11+
NativeStringEncoding.BStr,
12+
NativeStringEncoding.LPStr,
13+
NativeStringEncoding.LPTStr,
14+
NativeStringEncoding.LPUTF8Str,
15+
NativeStringEncoding.LPWStr,
16+
};
17+
18+
[Fact]
19+
public void TestEncodingString()
20+
{
21+
var input = "Hello world";
22+
foreach (var encoding in encodings)
23+
{
24+
var pointer = SilkMarshal.StringToPtr(input, encoding);
25+
var roundTrip = SilkMarshal.PtrToString(pointer, encoding);
26+
Assert.Equal(input, roundTrip);
27+
}
28+
}
29+
30+
[Fact]
31+
public void TestEncodingStringArray()
32+
{
33+
var inputs = new List<string>()
34+
{
35+
"Hello world",
36+
"Foo",
37+
"Bar",
38+
"123",
39+
};
40+
41+
foreach (var encoding in encodings)
42+
{
43+
var pointer = SilkMarshal.StringArrayToPtr(inputs, encoding);
44+
var roundTrip = SilkMarshal.PtrToStringArray(pointer, inputs.Count, encoding);
45+
for (var i = 0; i < roundTrip.Length; i++)
46+
{
47+
Assert.Equal(inputs[i], roundTrip[i]);
48+
}
49+
}
50+
}
51+
}

src/Core/Silk.NET.Core/Native/SilkMarshal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ public static unsafe string[] PtrToStringArray
456456
var ptrs = (nint*) input;
457457
for (var i = 0; i < numStrings; i++)
458458
{
459-
ret[i] = PtrToString(ptrs![i]);
459+
ret[i] = PtrToString(ptrs![i], encoding);
460460
}
461461

462462
return ret;

0 commit comments

Comments
 (0)