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

@@ -268,10 +268,10 @@ namespace Google.FlatBuffers.Test
}
var longArrayBytes = monster.GetVectorOfLongsBytes();
Assert.IsTrue(monster.VectorOfLongsLength * 8 == longArrayBytes.Length);
Assert.IsTrue(monster.VectorOfLongsLength == longArrayBytes.Length);
var doubleArrayBytes = monster.GetVectorOfDoublesBytes();
Assert.IsTrue(monster.VectorOfDoublesLength * 8 == doubleArrayBytes.Length);
Assert.IsTrue(monster.VectorOfDoublesLength == doubleArrayBytes.Length);
#else
var nameBytes = monster.GetNameBytes().Value;
Assert.AreEqual("MyMonster", Encoding.UTF8.GetString(nameBytes.Array, nameBytes.Offset, nameBytes.Count));