44// </copyright>
55
66using System ;
7+ using System . Diagnostics ;
78using System . IO ;
89using System . Runtime . CompilerServices ;
910using System . Threading . Tasks ;
@@ -25,6 +26,7 @@ public class Base85(Base85Alphabet alphabet) : IBaseCoder, IBaseStreamCoder, INo
2526 const int stringBlockSize = 5 ;
2627 const long fourSpaceChars = 0x20202020 ;
2728 const int decodeBufferSize = 5120 ; // don't remember what was special with this number
29+
2830 static readonly Lazy < Base85 > z85 = new ( ( ) => new Base85 ( Base85Alphabet . Z85 ) ) ;
2931 static readonly Lazy < Base85 > ascii85 = new ( ( ) => new Base85 ( Base85Alphabet . Ascii85 ) ) ;
3032 static readonly Lazy < Base85IPv6 > rfc1924 = new ( ( ) => new Base85IPv6 ( Base85Alphabet . Rfc1924 ) ) ;
@@ -155,8 +157,11 @@ public byte[] Decode(ReadOnlySpan<char> text)
155157
156158 // allocate a larger buffer if we're using shortcuts
157159 int decodeBufferLen = getSafeByteCountForDecoding ( text . Length , Alphabet . HasShortcut ) ;
160+ Debug . WriteLine ( $ "Decoding buffer length: { decodeBufferLen } ") ;
158161 Span < byte > decodeBuffer = decodeBufferLen < Bits . SafeStackMaxAllocSize ? stackalloc byte [ decodeBufferLen ] : new byte [ decodeBufferLen ] ;
159- return internalDecode ( text , decodeBuffer , out int bytesWritten ) switch
162+ var decodeResult = internalDecode ( text , decodeBuffer , out int bytesWritten ) ;
163+ Debug . WriteLine ( $ "Actual bytes written: { bytesWritten } ") ;
164+ return decodeResult switch
160165 {
161166 ( DecodeResult . Success , _ ) => decodeBuffer [ ..bytesWritten ] . ToArray ( ) ,
162167 ( DecodeResult . InvalidCharacter , char c ) => throw CodingAlphabet . InvalidCharacter ( c ) ,
@@ -190,19 +195,19 @@ static bool writeEncodedValue(
190195
191196 if ( blockLength == stringBlockSize )
192197 {
193- if ( block == 0 && zeroShortcutChar is not null )
194- {
195- output [ 0 ] = zeroShortcutChar . Value ; // guaranteed to be non-null
196- bytesWritten = 1 ;
197- return true ;
198- }
198+ if ( block == 0 && zeroShortcutChar is not null )
199+ {
200+ output [ 0 ] = zeroShortcutChar . Value ; // guaranteed to be non-null
201+ bytesWritten = 1 ;
202+ return true ;
203+ }
199204
200- if ( block == fourSpaceChars && spaceShortcutChar is not null )
201- {
202- output [ 0 ] = spaceShortcutChar . Value ; // guaranteed to be non-null
203- bytesWritten = 1 ;
204- return true ;
205- }
205+ if ( block == fourSpaceChars && spaceShortcutChar is not null )
206+ {
207+ output [ 0 ] = spaceShortcutChar . Value ; // guaranteed to be non-null
208+ bytesWritten = 1 ;
209+ return true ;
210+ }
206211 }
207212
208213 if ( blockLength > output . Length )
@@ -432,6 +437,7 @@ enum DecodeResult
432437 {
433438 return ( result , null ) ;
434439 }
440+ Debug . WriteLine ( $ "Decoded whole block of { bytesWrittenNow } bytes") ;
435441
436442 bytesWritten += bytesWrittenNow ;
437443 blockIndex = 0 ;
@@ -453,10 +459,13 @@ enum DecodeResult
453459 {
454460 return ( result , null ) ;
455461 }
462+ Debug . WriteLine ( $ "Wrote last remaining block of { bytesWrittenNow } bytes") ;
456463
457464 bytesWritten += bytesWrittenNow ;
458465 }
459466
467+ Debug . WriteLine ( $ "Final bytesWritten is { bytesWritten } ") ;
468+
460469 return ( DecodeResult . Success , null ) ;
461470 }
462471}
0 commit comments