@@ -293,11 +293,11 @@ public static int WriteFieldValue(Memory<byte> memory, object value)
293
293
{
294
294
case string val :
295
295
memory . Span [ 0 ] = ( byte ) 'S' ;
296
- if ( MemoryMarshal . TryGetArray ( memory . Slice ( 5 , Encoding . UTF8 . GetByteCount ( val ) ) , out ArraySegment < byte > segment ) )
296
+ if ( MemoryMarshal . TryGetArray ( memory , out ArraySegment < byte > segment ) )
297
297
{
298
- NetworkOrderSerializer . WriteUInt32 ( slice , ( uint ) segment . Count ) ;
299
- Encoding . UTF8 . GetBytes ( val , 0 , val . Length , segment . Array , segment . Offset ) ;
300
- return segment . Count + 5 ;
298
+ int bytesWritten = Encoding . UTF8 . GetBytes ( val , 0 , val . Length , segment . Array , segment . Offset + 5 ) ;
299
+ NetworkOrderSerializer . WriteUInt32 ( slice , ( uint ) bytesWritten ) ;
300
+ return 5 + bytesWritten ;
301
301
}
302
302
303
303
throw new WireFormattingException ( "Unable to get array segment from memory." ) ;
@@ -429,12 +429,11 @@ public static int WriteShort(Memory<byte> memory, ushort val)
429
429
430
430
public static int WriteShortstr ( Memory < byte > memory , string val )
431
431
{
432
- int stringBytesNeeded = Encoding . UTF8 . GetByteCount ( val ) ;
433
- if ( MemoryMarshal . TryGetArray ( memory . Slice ( 1 , stringBytesNeeded ) , out ArraySegment < byte > segment ) )
432
+ if ( MemoryMarshal . TryGetArray ( memory , out ArraySegment < byte > segment ) )
434
433
{
435
- memory . Span [ 0 ] = ( byte ) stringBytesNeeded ;
436
- Encoding . UTF8 . GetBytes ( val , 0 , val . Length , segment . Array , segment . Offset ) ;
437
- return stringBytesNeeded + 1 ;
434
+ int bytesWritten = Encoding . UTF8 . GetBytes ( val , 0 , val . Length , segment . Array , segment . Offset + 1 ) ;
435
+ memory . Span [ 0 ] = ( byte ) bytesWritten ;
436
+ return bytesWritten + 1 ;
438
437
}
439
438
440
439
throw new WireFormattingException ( "Unable to get array segment from memory." ) ;
0 commit comments