@@ -555,6 +555,7 @@ public static bool EndsWith(this string text, char value)
555555#endif
556556 }
557557
558+ #if ! NET
558559 /// <summary>
559560 /// Encapsulates a method that receives a span of objects of type <typeparamref name="T"/>
560561 /// and a state object of type <typeparamref name="TArg"/>.
@@ -573,49 +574,49 @@ public static bool EndsWith(this string text, char value)
573574 /// </param>
574575 public delegate void SpanAction < T , in TArg > ( Span < T > span , TArg arg ) ;
575576
576- /// <summary>
577- /// Creates a new string with a specific length and initializes it after creation by using the specified callback.
578- /// </summary>
579- /// <typeparam name="TState">
580- /// The type of the element to pass to <paramref name="action"/>.
581- /// </typeparam>
582- /// <param name="length">
583- /// The length of the string to create.
584- /// </param>
585- /// <param name="state">
586- /// The element to pass to <paramref name="action"/>.
587- /// </param>
588- /// <param name="action">
589- /// A callback to initialize the string
590- /// </param>
591- /// <returns>
592- /// The created string.
593- /// </returns>
594- /// <remarks>
595- /// The initial content of the destination span passed to <paramref name="action"/> is undefined.
596- /// Therefore, it is the delegate's responsibility to ensure that every element of the span is assigned.
597- /// Otherwise, the resulting string could contain random characters
598- /// </remarks>
599- public unsafe static string CreateString < TState > ( int length , TState state , SpanAction < char , TState > action )
577+ extension ( string )
600578 {
601- #if NET
602- return string . Create ( length , ( action , state ) , static ( span , state ) => state . action ( span , state . state ) ) ;
603- #else
604- ArgHelper . ThrowIfNegative ( length ) ;
605-
606- if ( length == 0 )
579+ /// <summary>
580+ /// Creates a new string with a specific length and initializes it after creation by using the specified callback.
581+ /// </summary>
582+ /// <typeparam name="TState">
583+ /// The type of the element to pass to <paramref name="action"/>.
584+ /// </typeparam>
585+ /// <param name="length">
586+ /// The length of the string to create.
587+ /// </param>
588+ /// <param name="state">
589+ /// The element to pass to <paramref name="action"/>.
590+ /// </param>
591+ /// <param name="action">
592+ /// A callback to initialize the string
593+ /// </param>
594+ /// <returns>
595+ /// The created string.
596+ /// </returns>
597+ /// <remarks>
598+ /// The initial content of the destination span passed to <paramref name="action"/> is undefined.
599+ /// Therefore, it is the delegate's responsibility to ensure that every element of the span is assigned.
600+ /// Otherwise, the resulting string could contain random characters.
601+ /// </remarks>
602+ public unsafe static string Create < TState > ( int length , TState state , SpanAction < char , TState > action )
607603 {
608- return string . Empty ;
609- }
604+ ArgHelper . ThrowIfNegative ( length ) ;
610605
611- var result = new string ( '\0 ' , length ) ;
606+ if ( length == 0 )
607+ {
608+ return string . Empty ;
609+ }
612610
613- fixed ( char * ptr = result )
614- {
615- action ( new Span < char > ( ptr , length ) , state ) ;
616- }
611+ var result = new string ( '\0 ' , length ) ;
617612
618- return result ;
619- #endif
613+ fixed ( char * ptr = result )
614+ {
615+ action ( new Span < char > ( ptr , length ) , state ) ;
616+ }
617+
618+ return result ;
619+ }
620620 }
621+ #endif
621622}
0 commit comments