diff --git a/src/Base58Encoding/Base58.Encode.cs b/src/Base58Encoding/Base58.Encode.cs index 150cd95..032dae8 100644 --- a/src/Base58Encoding/Base58.Encode.cs +++ b/src/Base58Encoding/Base58.Encode.cs @@ -78,7 +78,7 @@ private string EncodeGenericToString(ReadOnlySpan data) { Span digits = stackalloc byte[size]; int digitCount = ComputeGenericDigits(inputSpan, digits); - var state = new EncodeState(digits, 0, digitCount, TAlphabet.Characters, TAlphabet.FirstCharacter, leadingZeros); + var state = new EncodeState(digits, 0, digitCount, leadingZeros); return string.Create(state.OutputLength, state, static (span, s) => s.EmitReverse(span)); } @@ -91,7 +91,7 @@ private string EncodeGenericToStringLarge(ReadOnlySpan inputSpan, int lead try { int digitCount = ComputeGenericDigits(inputSpan, rented); - var state = new EncodeState(rented, 0, digitCount, TAlphabet.Characters, TAlphabet.FirstCharacter, leadingZeros); + var state = new EncodeState(rented, 0, digitCount, leadingZeros); return string.Create(state.OutputLength, state, static (span, s) => s.EmitReverse(span)); } finally @@ -129,7 +129,7 @@ private int EncodeGenericToBytes(ReadOnlySpan data, Span destination ThrowHelper.ThrowDestinationTooSmall(nameof(destination)); } - var state = new EncodeState(digits, 0, digitCount, TAlphabet.Characters, TAlphabet.FirstCharacter, leadingZeros); + var state = new EncodeState(digits, 0, digitCount, leadingZeros); state.EmitReverse(destination); return outputLength; } @@ -149,7 +149,7 @@ private int EncodeGenericToBytesLarge(ReadOnlySpan inputSpan, int leadingZ ThrowHelper.ThrowDestinationTooSmall(nameof(destination)); } - var state = new EncodeState(rented, 0, digitCount, TAlphabet.Characters, TAlphabet.FirstCharacter, leadingZeros); + var state = new EncodeState(rented, 0, digitCount, leadingZeros); state.EmitReverse(destination); return outputLength; } @@ -202,7 +202,7 @@ internal static string EncodeBitcoin32FastToString(ReadOnlySpan data) Debug.Assert(skip >= 0, "rawLeadingZeros should always be >= inLeadingZeros by Base58 math"); int digitCount = Base58BitcoinTables.Raw58Sz32 - rawLeadingZeros; - var state = new EncodeState(rawBase58, rawLeadingZeros, digitCount, Base58BitcoinTables.BitcoinChars, (byte)'1', inLeadingZeros); + var state = new EncodeState(rawBase58, rawLeadingZeros, digitCount, inLeadingZeros); return string.Create(state.OutputLength, state, static (span, s) => s.EmitForward(span)); } @@ -235,7 +235,7 @@ private static int EncodeBitcoin32FastToBytes(ReadOnlySpan data, Span(rawBase58, rawLeadingZeros, digitCount, inLeadingZeros); state.EmitForward(destination); return outputLength; } @@ -311,7 +311,7 @@ internal static string EncodeBitcoin64FastToString(ReadOnlySpan data) Debug.Assert(skip >= 0, "rawLeadingZeros should always be >= inLeadingZeros by Base58 math"); int digitCount = Base58BitcoinTables.Raw58Sz64 - rawLeadingZeros; - var state = new EncodeState(rawBase58, rawLeadingZeros, digitCount, Base58BitcoinTables.BitcoinChars, (byte)'1', inLeadingZeros); + var state = new EncodeState(rawBase58, rawLeadingZeros, digitCount, inLeadingZeros); return string.Create(state.OutputLength, state, static (span, s) => s.EmitForward(span)); } @@ -344,7 +344,7 @@ private static int EncodeBitcoin64FastToBytes(ReadOnlySpan data, Span(rawBase58, rawLeadingZeros, digitCount, inLeadingZeros); state.EmitForward(destination); return outputLength; } @@ -419,28 +419,23 @@ private static int ComputeBitcoin64FastRaw(ReadOnlySpan data, Span r return rawLeadingZeros; } - private readonly ref struct EncodeState + private readonly ref struct EncodeState + where T : struct, IBase58Alphabet { public readonly ReadOnlySpan Digits; - public readonly ReadOnlySpan Alphabet; public readonly int DigitStart; public readonly int DigitCount; - public readonly byte LeadingFill; public readonly int LeadingCount; public EncodeState( ReadOnlySpan digits, int digitStart, int digitCount, - ReadOnlySpan alphabet, - byte leadingFill, int leadingCount) { Digits = digits; DigitStart = digitStart; DigitCount = digitCount; - Alphabet = alphabet; - LeadingFill = leadingFill; LeadingCount = leadingCount; } @@ -451,14 +446,15 @@ public void EmitForward(Span destination) { if (LeadingCount > 0) { - destination[..LeadingCount].Fill(TChar.CreateTruncating(LeadingFill)); + destination[..LeadingCount].Fill(TChar.CreateTruncating(T.FirstCharacter)); } int index = LeadingCount; int end = DigitStart + DigitCount; + ReadOnlySpan alphabet = T.Characters; for (int i = DigitStart; i < end; i++) { - destination[index++] = TChar.CreateTruncating((ushort)Alphabet[Digits[i]]); + destination[index++] = TChar.CreateTruncating((ushort)alphabet[Digits[i]]); } } @@ -467,13 +463,14 @@ public void EmitReverse(Span destination) { if (LeadingCount > 0) { - destination[..LeadingCount].Fill(TChar.CreateTruncating(LeadingFill)); + destination[..LeadingCount].Fill(TChar.CreateTruncating(T.FirstCharacter)); } int index = LeadingCount; + ReadOnlySpan alphabet = T.Characters; for (int i = DigitStart + DigitCount - 1; i >= DigitStart; i--) { - destination[index++] = TChar.CreateTruncating((ushort)Alphabet[Digits[i]]); + destination[index++] = TChar.CreateTruncating((ushort)alphabet[Digits[i]]); } } }