@@ -293,11 +293,11 @@ public static int WriteFieldValue(Memory<byte> memory, object value)
293293 {
294294 case string val :
295295 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 ) )
297297 {
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 ;
301301 }
302302
303303 throw new WireFormattingException ( "Unable to get array segment from memory." ) ;
@@ -429,12 +429,11 @@ public static int WriteShort(Memory<byte> memory, ushort val)
429429
430430 public static int WriteShortstr ( Memory < byte > memory , string val )
431431 {
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 ) )
434433 {
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 ;
438437 }
439438
440439 throw new WireFormattingException ( "Unable to get array segment from memory." ) ;
0 commit comments