44//
55
66using System ;
7- using System . Buffers ;
87using System . Collections . Generic ;
98using System . IO ;
109using System . Text ;
@@ -30,47 +29,38 @@ public static void FoldLines(this StringBuilder result, string inputLine)
3029
3130 // Use ArrayPool<char> to rent arrays for processing
3231 // Cannot use Span<char> and stackalloc because netstandard2.0 does not support it
33- var arrayPool = ArrayPool < char > . Shared ;
34- var currentLineArray = arrayPool . Rent ( 76 ) ; // 75 characters + 1 space
32+ var currentLineArray = new char [ 76 ] ; // 75 characters + 1 space
3533
36- try
37- {
38- var currentLineIndex = 0 ;
39- var byteCount = 0 ;
40- var charCount = 0 ;
41-
42- while ( charCount < inputLine . Length )
43- {
44- var currentCharByteCount = Encoding . UTF8 . GetByteCount ( [ inputLine [ charCount ] ] ) ;
34+ var currentLineIndex = 0 ;
35+ var byteCount = 0 ;
36+ var charCount = 0 ;
4537
46- if ( byteCount + currentCharByteCount > 75 )
47- {
48- result . Append ( currentLineArray , 0 , currentLineIndex ) ;
49- result . Append ( SerializationConstants . LineBreak ) ;
50-
51- currentLineIndex = 0 ;
52- byteCount = 1 ;
53- currentLineArray [ currentLineIndex ++ ] = ' ' ;
54- }
55-
56- currentLineArray [ currentLineIndex ++ ] = inputLine [ charCount ] ;
57- byteCount += currentCharByteCount ;
58- charCount ++ ;
59- }
38+ while ( charCount < inputLine . Length )
39+ {
40+ var currentCharByteCount = Encoding . UTF8 . GetByteCount ( [ inputLine [ charCount ] ] ) ;
6041
61- // Append the remaining characters to the result
62- if ( currentLineIndex > 0 )
42+ if ( byteCount + currentCharByteCount > 75 )
6343 {
6444 result . Append ( currentLineArray , 0 , currentLineIndex ) ;
45+ result . Append ( SerializationConstants . LineBreak ) ;
46+
47+ currentLineIndex = 0 ;
48+ byteCount = 1 ;
49+ currentLineArray [ currentLineIndex ++ ] = ' ' ;
6550 }
6651
67- result . Append ( SerializationConstants . LineBreak ) ;
52+ currentLineArray [ currentLineIndex ++ ] = inputLine [ charCount ] ;
53+ byteCount += currentCharByteCount ;
54+ charCount ++ ;
6855 }
69- finally
56+
57+ // Append the remaining characters to the result
58+ if ( currentLineIndex > 0 )
7059 {
71- // Return the rented array to the pool
72- arrayPool . Return ( currentLineArray ) ;
60+ result . Append ( currentLineArray , 0 , currentLineIndex ) ;
7361 }
62+
63+ result . Append ( SerializationConstants . LineBreak ) ;
7464 }
7565
7666 public static IEnumerable < string > Chunk ( string str , int chunkSize = 73 )
0 commit comments