2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System ;
5
+ using System . Buffers . Binary ;
5
6
using System . Text ;
6
7
7
8
#nullable enable
@@ -29,7 +30,7 @@ private static string[] ReadStringTable(ReadOnlySpan<byte> data, ReadOnlySpan<by
29
30
30
31
for ( var i = 0 ; i < indexes . Length ; i += 4 )
31
32
{
32
- var index = BitConverter . ToInt32 ( indexes . Slice ( i , 4 ) ) ;
33
+ var index = BinaryPrimitives . ReadInt32LittleEndian ( indexes . Slice ( i , 4 ) ) ;
33
34
34
35
// The string table entries are all length-prefixed UTF8 blobs
35
36
var length = ( int ) ReadUnsignedLEB128 ( data , index , out var numLEB128Bytes ) ;
@@ -46,21 +47,21 @@ private static ArrayRange<RenderTreeDiff> ReadUpdatedComponents(ReadOnlySpan<byt
46
47
47
48
for ( var i = 0 ; i < indexes . Length ; i += 4 )
48
49
{
49
- var index = BitConverter . ToInt32 ( indexes . Slice ( i , 4 ) ) ;
50
+ var index = BinaryPrimitives . ReadInt32LittleEndian ( indexes . Slice ( i , 4 ) ) ;
50
51
51
- var componentId = BitConverter . ToInt32 ( data . Slice ( index , 4 ) ) ;
52
- var editCount = BitConverter . ToInt32 ( data . Slice ( index + 4 , 4 ) ) ;
52
+ var componentId = BinaryPrimitives . ReadInt32LittleEndian ( data . Slice ( index , 4 ) ) ;
53
+ var editCount = BinaryPrimitives . ReadInt32LittleEndian ( data . Slice ( index + 4 , 4 ) ) ;
53
54
54
55
var editData = data . Slice ( index + 8 ) ;
55
56
var edits = new RenderTreeEdit [ editCount ] ;
56
57
for ( var j = 0 ; j < editCount ; j ++ )
57
58
{
58
- var type = ( RenderTreeEditType ) BitConverter . ToInt32 ( editData . Slice ( 0 , 4 ) ) ;
59
- var siblingIndex = BitConverter . ToInt32 ( editData . Slice ( 4 , 4 ) ) ;
59
+ var type = ( RenderTreeEditType ) BinaryPrimitives . ReadInt32LittleEndian ( editData . Slice ( 0 , 4 ) ) ;
60
+ var siblingIndex = BinaryPrimitives . ReadInt32LittleEndian ( editData . Slice ( 4 , 4 ) ) ;
60
61
61
62
// ReferenceFrameIndex and MoveToSiblingIndex share a slot, so this reads
62
63
// whichever one applies to the edit type
63
- var referenceFrameIndex = BitConverter . ToInt32 ( editData . Slice ( 8 , 4 ) ) ;
64
+ var referenceFrameIndex = BinaryPrimitives . ReadInt32LittleEndian ( editData . Slice ( 8 , 4 ) ) ;
64
65
var removedAttributeName = ReadString ( editData . Slice ( 12 , 4 ) , strings ) ;
65
66
66
67
editData = editData . Slice ( 16 ) ;
@@ -133,7 +134,7 @@ private static ArrayRange<RenderTreeFrame> ReadReferenceFrames(ReadOnlySpan<byte
133
134
{
134
135
var frameData = data . Slice ( i , ReferenceFrameSize ) ;
135
136
136
- var type = ( RenderTreeFrameType ) BitConverter . ToInt32 ( frameData . Slice ( 0 , 4 ) ) ;
137
+ var type = ( RenderTreeFrameType ) BinaryPrimitives . ReadInt32LittleEndian ( frameData . Slice ( 0 , 4 ) ) ;
137
138
138
139
// We want each frame to take up the same number of bytes, so that the
139
140
// recipient can index into the array directly instead of having to
@@ -146,13 +147,13 @@ private static ArrayRange<RenderTreeFrame> ReadReferenceFrames(ReadOnlySpan<byte
146
147
case RenderTreeFrameType . Attribute :
147
148
var attributeName = ReadString ( frameData . Slice ( 4 , 4 ) , strings ) ;
148
149
var attributeValue = ReadString ( frameData . Slice ( 8 , 4 ) , strings ) ;
149
- var attributeEventHandlerId = BitConverter . ToUInt64 ( frameData . Slice ( 12 , 8 ) ) ;
150
+ var attributeEventHandlerId = BinaryPrimitives . ReadUInt64LittleEndian ( frameData . Slice ( 12 , 8 ) ) ;
150
151
result [ i / ReferenceFrameSize ] = RenderTreeFrame . Attribute ( 0 , attributeName , attributeValue ) . WithAttributeEventHandlerId ( attributeEventHandlerId ) ;
151
152
break ;
152
153
153
154
case RenderTreeFrameType . Component :
154
- var componentSubtreeLength = BitConverter . ToInt32 ( frameData . Slice ( 4 , 4 ) ) ;
155
- var componentId = BitConverter . ToInt32 ( frameData . Slice ( 8 , 4 ) ) ; // Nowhere to put this without creating a ComponentState
155
+ var componentSubtreeLength = BinaryPrimitives . ReadInt32LittleEndian ( frameData . Slice ( 4 , 4 ) ) ;
156
+ var componentId = BinaryPrimitives . ReadInt32LittleEndian ( frameData . Slice ( 8 , 4 ) ) ; // Nowhere to put this without creating a ComponentState
156
157
result [ i / ReferenceFrameSize ] = RenderTreeFrame . ChildComponent ( 0 , componentType : null )
157
158
. WithComponentSubtreeLength ( componentSubtreeLength )
158
159
. WithComponent ( new ComponentState ( componentId ) ) ;
@@ -164,7 +165,7 @@ private static ArrayRange<RenderTreeFrame> ReadReferenceFrames(ReadOnlySpan<byte
164
165
break ;
165
166
166
167
case RenderTreeFrameType . Element :
167
- var elementSubtreeLength = BitConverter . ToInt32 ( frameData . Slice ( 4 , 4 ) ) ;
168
+ var elementSubtreeLength = BinaryPrimitives . ReadInt32LittleEndian ( frameData . Slice ( 4 , 4 ) ) ;
168
169
var elementName = ReadString ( frameData . Slice ( 8 , 4 ) , strings ) ;
169
170
result [ i / ReferenceFrameSize ] = RenderTreeFrame . Element ( 0 , elementName ) . WithElementSubtreeLength ( elementSubtreeLength ) ;
170
171
break ;
@@ -176,7 +177,7 @@ private static ArrayRange<RenderTreeFrame> ReadReferenceFrames(ReadOnlySpan<byte
176
177
break ;
177
178
178
179
case RenderTreeFrameType . Region :
179
- var regionSubtreeLength = BitConverter . ToInt32 ( frameData . Slice ( 4 , 4 ) ) ;
180
+ var regionSubtreeLength = BinaryPrimitives . ReadInt32LittleEndian ( frameData . Slice ( 4 , 4 ) ) ;
180
181
result [ i / ReferenceFrameSize ] = RenderTreeFrame . Region ( 0 ) . WithRegionSubtreeLength ( regionSubtreeLength ) ;
181
182
break ;
182
183
@@ -210,7 +211,7 @@ private static ArrayRange<ulong> ReadDisposedEventHandlerIds(ReadOnlySpan<byte>
210
211
211
212
private static string ? ReadString ( ReadOnlySpan < byte > data , string [ ] strings )
212
213
{
213
- var index = BitConverter . ToInt32 ( data . Slice ( 0 , 4 ) ) ;
214
+ var index = BinaryPrimitives . ReadInt32LittleEndian ( data . Slice ( 0 , 4 ) ) ;
214
215
return index >= 0 ? strings [ index ] : null ;
215
216
}
216
217
@@ -237,11 +238,11 @@ private readonly struct Sections
237
238
public static Sections Parse ( ReadOnlySpan < byte > data )
238
239
{
239
240
return new Sections (
240
- BitConverter . ToInt32 ( data . Slice ( data . Length - 20 , 4 ) ) ,
241
- BitConverter . ToInt32 ( data . Slice ( data . Length - 16 , 4 ) ) ,
242
- BitConverter . ToInt32 ( data . Slice ( data . Length - 12 , 4 ) ) ,
243
- BitConverter . ToInt32 ( data . Slice ( data . Length - 8 , 4 ) ) ,
244
- BitConverter . ToInt32 ( data . Slice ( data . Length - 4 , 4 ) ) ) ;
241
+ BinaryPrimitives . ReadInt32LittleEndian ( data . Slice ( data . Length - 20 , 4 ) ) ,
242
+ BinaryPrimitives . ReadInt32LittleEndian ( data . Slice ( data . Length - 16 , 4 ) ) ,
243
+ BinaryPrimitives . ReadInt32LittleEndian ( data . Slice ( data . Length - 12 , 4 ) ) ,
244
+ BinaryPrimitives . ReadInt32LittleEndian ( data . Slice ( data . Length - 8 , 4 ) ) ,
245
+ BinaryPrimitives . ReadInt32LittleEndian ( data . Slice ( data . Length - 4 , 4 ) ) ) ;
245
246
}
246
247
247
248
private readonly int _updatedComponents ;
@@ -262,14 +263,14 @@ public Sections(int updatedComponents, int referenceFrames, int disposedComponen
262
263
public ReadOnlySpan < byte > GetUpdatedComponentIndexes ( ReadOnlySpan < byte > data )
263
264
{
264
265
// This is count-prefixed contiguous array of of integers.
265
- var count = BitConverter . ToInt32 ( data . Slice ( _updatedComponents , 4 ) ) ;
266
+ var count = BinaryPrimitives . ReadInt32LittleEndian ( data . Slice ( _updatedComponents , 4 ) ) ;
266
267
return data . Slice ( _updatedComponents + 4 , count * 4 ) ;
267
268
}
268
269
269
270
public ReadOnlySpan < byte > GetReferenceFrameData ( ReadOnlySpan < byte > data )
270
271
{
271
272
// This is a count-prefixed contiguous array of RenderTreeFrame.
272
- var count = BitConverter . ToInt32 ( data . Slice ( _referenceFrames , 4 ) ) ;
273
+ var count = BinaryPrimitives . ReadInt32LittleEndian ( data . Slice ( _referenceFrames , 4 ) ) ;
273
274
return data . Slice ( _referenceFrames + 4 , count * ReferenceFrameSize ) ;
274
275
}
275
276
0 commit comments