Correct span and non-span versions of ToArray() and ToArrayPadded() methods (#8734)

* Correction of bug inside ToArray<T> methods

Avoid allocating too large buffers (len is expressed in bytes, not in Ts).
Added validation to ensure len is a multiple of SizeOf<T>() before converting to array.

* Update ByteBuffer.cs

* Refactor ToArray and ToArrayPadded methods

I understand from failed test that pos, len, padLeft and padRight are expressed in Ts

* Refactor ToArray and ToArrayPadded methods

* Final correction
All functions parameters expressed in bytes for homogeneity
Tests run:
  - UNSAFE_BYTEBUFFER=true/ENABLE_SPAN_T=true: passed
  - UNSAFE_BYTEBUFFER=true/ENABLE_SPAN_T=false: passed
  - UNSAFE_BYTEBUFFER=false/ENABLE_SPAN_T=false: passed
  - UNSAFE_BYTEBUFFER=false/ENABLE_SPAN_T=true: configuration forbidden by compilation
Correction of FlatBuffers.Test.csproj to allow UNSAFE_BYTEBUFFER/ENABLE_SPAN_T tests
Correction of FlatBuffersExampleTests.cs: I think the test was not run because it could not pass (to be reviewed carefully)
This commit is contained in:
vsmcea
2025-10-25 20:58:05 +02:00
committed by GitHub
parent 27325e002a
commit 95053e6a47
4 changed files with 69 additions and 46 deletions

View File

@@ -9,12 +9,12 @@
<Compile Remove="Properties\AssemblyInfo.cs" />
</ItemGroup>
<PropertyGroup Condition="'$(UnsafeByteBuffer)' == 'true'">
<PropertyGroup Condition="'$(UNSAFE_BYTEBUFFER)' == 'true'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);UNSAFE_BYTEBUFFER</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(EnableSpanT)' == 'true'">
<PropertyGroup Condition="'$(ENABLE_SPAN_T)' == 'true'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);ENABLE_SPAN_T</DefineConstants>
</PropertyGroup>