@@ -168,6 +168,10 @@ public static int ReadShortstr(ReadOnlySpan<byte> span, out string value)
168
168
// equals span.Length >= byteCount + 1
169
169
if ( span . Length > byteCount )
170
170
{
171
+ #if NETCOREAPP
172
+ value = Encoding . UTF8 . GetString ( span . Slice ( 1 , byteCount ) ) ;
173
+ return 1 + byteCount ;
174
+ #else
171
175
unsafe
172
176
{
173
177
fixed ( byte * bytes = & MemoryMarshal . GetReference ( span . Slice ( 1 ) ) )
@@ -176,6 +180,7 @@ public static int ReadShortstr(ReadOnlySpan<byte> span, out string value)
176
180
return 1 + byteCount ;
177
181
}
178
182
}
183
+ #endif
179
184
}
180
185
181
186
throw new ArgumentOutOfRangeException ( "span" , $ "Span has not enough space ({ span . Length } instead of { byteCount + 1 } )") ;
@@ -725,6 +730,18 @@ public static int WriteShortstr(Span<byte> span, string val)
725
730
maxLength = byte . MaxValue ;
726
731
}
727
732
733
+ #if NETCOREAPP
734
+ try
735
+ {
736
+ int bytesWritten = Encoding . UTF8 . GetBytes ( val , span . Slice ( 1 ) ) ;
737
+ span [ 0 ] = ( byte ) bytesWritten ;
738
+ return bytesWritten + 1 ;
739
+ }
740
+ catch ( ArgumentException e )
741
+ {
742
+ throw new ArgumentOutOfRangeException ( $ "Value exceeds the maximum allowed length of { maxLength } bytes.", e ) ;
743
+ }
744
+ #else
728
745
unsafe
729
746
{
730
747
fixed ( char * chars = val )
@@ -742,6 +759,7 @@ public static int WriteShortstr(Span<byte> span, string val)
742
759
}
743
760
}
744
761
}
762
+ #endif
745
763
}
746
764
747
765
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
@@ -753,6 +771,11 @@ public static int WriteLongstr(Span<byte> span, string val)
753
771
return 4 ;
754
772
}
755
773
774
+ #if NETCOREAPP
775
+ int bytesWritten = Encoding . UTF8 . GetBytes ( val , span . Slice ( 4 ) ) ;
776
+ NetworkOrderSerializer . WriteUInt32 ( span , ( uint ) bytesWritten ) ;
777
+ return bytesWritten + 4 ;
778
+ #else
756
779
unsafe
757
780
{
758
781
fixed ( char * chars = val )
@@ -763,6 +786,7 @@ public static int WriteLongstr(Span<byte> span, string val)
763
786
return bytesWritten + 4 ;
764
787
}
765
788
}
789
+ #endif
766
790
}
767
791
768
792
public static int WriteTable ( Span < byte > span , IDictionary val )
0 commit comments