@@ -179,7 +179,19 @@ public void AppendFormatted(string value, int alignment = 0, string? format = nu
179179 var buffer = rentArray . AsSpan ( ) ;
180180 var bytesWritten = Encoding . UTF8 . GetBytes ( value . AsSpan ( ) , buffer ) ;
181181
182- var space = alignment - bytesWritten ;
182+ int charCount ;
183+ #if NETSTANDARD2_0
184+ unsafe
185+ {
186+ fixed ( byte * ptr = & buffer [ 0 ] )
187+ {
188+ charCount = Encoding . UTF8 . GetCharCount ( ptr , bytesWritten ) ;
189+ }
190+ }
191+ #else
192+ charCount = Encoding . UTF8 . GetCharCount ( buffer . Slice ( 0 , bytesWritten ) ) ;
193+ #endif
194+ var space = alignment - charCount ;
183195 if ( space > 0 )
184196 {
185197 AppendWhitespace ( space ) ;
@@ -190,18 +202,30 @@ public void AppendFormatted(string value, int alignment = 0, string? format = nu
190202 destination = destination . Slice ( bytesWritten ) ;
191203 currentWritten += bytesWritten ;
192204 ArrayPool < byte > . Shared . Return ( rentArray ) ;
193- return ;
194205 }
195206 else
196207 {
197208 // add right whitespace
198209 var max = GetStringByteCount ( value . AsSpan ( ) ) ;
199210 TryGrow ( max ) ;
200211 var bytesWritten = Encoding . UTF8 . GetBytes ( value . AsSpan ( ) , destination ) ;
212+
213+ int charCount ;
214+ #if NETSTANDARD2_0
215+ unsafe
216+ {
217+ fixed ( byte * ptr = & destination [ currentWritten ] )
218+ {
219+ charCount = Encoding . UTF8 . GetCharCount ( ptr , bytesWritten ) ;
220+ }
221+ }
222+ #else
223+ charCount = Encoding . UTF8 . GetCharCount ( destination . Slice ( 0 , bytesWritten ) ) ;
224+ #endif
201225 destination = destination . Slice ( bytesWritten ) ;
202226 currentWritten += bytesWritten ;
203227
204- var space = bytesWritten + alignment ;
228+ var space = charCount + alignment ;
205229 if ( space < 0 )
206230 {
207231 AppendWhitespace ( - space ) ;
0 commit comments