@@ -205,7 +205,7 @@ public static ReadOnlySequence<T> Concat<T>(this ReadOnlyMemory<T> first, ReadOn
205205 /// <param name="destination">Destination memory.</param>
206206 /// <param name="writtenCount">The number of copied elements.</param>
207207 /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
208- public static void CopyTo < T > ( this in ReadOnlySequence < T > source , scoped Span < T > destination , out int writtenCount )
208+ public static void CopyTo < T > ( this in ReadOnlySequence < T > source , Span < T > destination , out int writtenCount )
209209 {
210210 if ( source . IsSingleSegment )
211211 {
@@ -237,61 +237,48 @@ static int CopyToSlow(in ReadOnlySequence<T> source, Span<T> destination)
237237 /// </summary>
238238 /// <param name="source">Source sequence.</param>
239239 /// <param name="destination">Destination memory.</param>
240- /// <param name="position ">The position within <paramref name="source"/> that represents the end of <paramref name="destination"/>.</param>
240+ /// <param name="consumed ">The position within <paramref name="source"/> that represents the end of <paramref name="destination"/>.</param>
241241 /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
242242 /// <returns>The number of copied elements.</returns>
243- public static int CopyTo < T > ( this in ReadOnlySequence < T > source , scoped Span < T > destination , out SequencePosition position )
243+ public static int CopyTo < T > ( this in ReadOnlySequence < T > source , Span < T > destination , out SequencePosition consumed )
244244 {
245- int writtenCount ;
246- ReadOnlySpan < T > segment ;
247- if ( ! source . IsSingleSegment )
248- {
249- // slow path - multisegment sequence
250- writtenCount = CopyToSlow ( in source , destination , out position ) ;
251- }
252- else if ( ( segment = source . FirstSpan ) . Length <= destination . Length )
245+ var writtenCount = 0 ;
246+ ReadOnlyMemory < T > block ;
247+
248+ consumed = source . Start ;
249+ if ( source . IsSingleSegment )
253250 {
254- writtenCount = segment . Length ;
255- position = source . End ;
251+ block = source . First ;
256252 }
257253 else
258254 {
259- writtenCount = destination . Length ;
260- segment = segment . Slice ( 0 , writtenCount ) ;
261- segment . CopyTo ( destination ) ;
262- position = source . GetPosition ( writtenCount , source . Start ) ;
263- }
264-
265- return writtenCount ;
266-
267- static int CopyToSlow ( in ReadOnlySequence < T > source , Span < T > destination , out SequencePosition consumed )
268- {
269- var result = 0 ;
270-
271- ReadOnlyMemory < T > block ;
272- for ( var position = consumed = source . Start ;
255+ for ( var position = consumed ;
273256 source . TryGet ( ref position , out block ) && block . Length <= destination . Length ;
274257 consumed = position ,
275- result += block . Length )
258+ writtenCount += block . Length )
276259 {
277260 block . Span . CopyTo ( destination ) ;
278261 destination = destination . Slice ( block . Length ) ;
279262 }
263+ }
280264
281- if ( block . Length > destination . Length )
265+ writtenCount += CopyLastSegment ( source , block . Span , destination , ref consumed ) ;
266+ return writtenCount ;
267+
268+ static int CopyLastSegment ( in ReadOnlySequence < T > source , ReadOnlySpan < T > segment , Span < T > destination , ref SequencePosition consumed )
269+ {
270+ if ( segment . Length > destination . Length )
282271 {
283- block = block . Slice ( 0 , destination . Length ) ;
272+ segment = segment . Slice ( 0 , destination . Length ) ;
284273 consumed = source . GetPosition ( destination . Length , consumed ) ;
285274 }
286275 else
287276 {
288277 consumed = source . End ;
289278 }
290-
291- block . Span . CopyTo ( destination ) ;
292- result += block . Length ;
293279
294- return result ;
280+ segment . CopyTo ( destination ) ;
281+ return segment . Length ;
295282 }
296283 }
297284
0 commit comments