Skip to content

Commit

Permalink
convert to .NET 8 / C# 12
Browse files Browse the repository at this point in the history
  • Loading branch information
ssg committed Nov 14, 2023
1 parent 0242c25 commit d7b5ab4
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 210 deletions.
1 change: 1 addition & 0 deletions SimpleBase.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitignore = .gitignore
global.json = global.json
LICENSE.txt = LICENSE.txt
pack.cmd = pack.cmd
pack.sh = pack.sh
Expand Down
6 changes: 6 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"version": "8.0.100",
"rollForward": "latestMinor"
}
}
19 changes: 7 additions & 12 deletions src/Base16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,16 @@ namespace SimpleBase;
/// <summary>
/// Base16 encoding/decoding.
/// </summary>
public sealed class Base16 : IBaseCoder, IBaseStreamCoder, INonAllocatingBaseCoder
/// <remarks>
/// Initializes a new instance of the <see cref="Base16"/> class.
/// </remarks>
/// <param name="alphabet">Alphabet to use.</param>
public sealed class Base16(Base16Alphabet alphabet) : IBaseCoder, IBaseStreamCoder, INonAllocatingBaseCoder
{
private static readonly Lazy<Base16> upperCase = new(() => new Base16(Base16Alphabet.UpperCase));
private static readonly Lazy<Base16> lowerCase = new(() => new Base16(Base16Alphabet.LowerCase));
private static readonly Lazy<Base16> modHex = new(() => new Base16(Base16Alphabet.ModHex));

/// <summary>
/// Initializes a new instance of the <see cref="Base16"/> class.
/// </summary>
/// <param name="alphabet">Alphabet to use.</param>
public Base16(Base16Alphabet alphabet)
{
Alphabet = alphabet;
}

/// <summary>
/// Gets upper case Base16 encoder. Decoding is case-insensitive.
/// </summary>
Expand All @@ -46,7 +41,7 @@ public Base16(Base16Alphabet alphabet)
/// <summary>
/// Gets the alphabet used by the encoder.
/// </summary>
public Base16Alphabet Alphabet { get; }
public Base16Alphabet Alphabet { get; } = alphabet;

/// <summary>
/// Decode Upper/Lowercase Base16 text into bytes.
Expand Down Expand Up @@ -146,7 +141,7 @@ public byte[] Decode(ReadOnlySpan<char> text)
int textLen = text.Length;
if (textLen == 0)
{
return Array.Empty<byte>();
return [];
}

byte[] output = new byte[GetSafeByteCountForDecoding(text)];
Expand Down
2 changes: 1 addition & 1 deletion src/Base32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public byte[] Decode(ReadOnlySpan<char> text)
int outputLen = getAllocationByteCountForDecoding(textLen);
if (outputLen == 0)
{
return Array.Empty<byte>();
return [];
}

var outputBuffer = new byte[outputLen];
Expand Down
24 changes: 9 additions & 15 deletions src/Base58.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ namespace SimpleBase;
/// Base58 doesn't implement a Stream-based interface because it's not feasible to use
/// on large buffers.
/// </remarks>
public sealed class Base58 : IBaseCoder, INonAllocatingBaseCoder
/// <remarks>
/// Initializes a new instance of the <see cref="Base58"/> class
/// using a custom alphabet.
/// </remarks>
/// <param name="alphabet">Alphabet to use.</param>
public sealed class Base58(Base58Alphabet alphabet) : IBaseCoder, INonAllocatingBaseCoder
{
private const int reductionFactor = 733; // https://github.com/bitcoin/bitcoin/blob/master/src/base58.cpp#L48
private const int divisor = 58;
Expand All @@ -27,17 +32,6 @@ public sealed class Base58 : IBaseCoder, INonAllocatingBaseCoder
private static readonly Lazy<Base58> ripple = new(() => new Base58(Base58Alphabet.Ripple));
private static readonly Lazy<Base58> flickr = new(() => new Base58(Base58Alphabet.Flickr));

/// <summary>
/// Initializes a new instance of the <see cref="Base58"/> class
/// using a custom alphabet.
/// </summary>
/// <param name="alphabet">Alphabet to use.</param>
public Base58(Base58Alphabet alphabet)
{
Alphabet = alphabet;
ZeroChar = alphabet.Value[0];
}

/// <summary>
/// Gets Bitcoin flavor.
/// </summary>
Expand All @@ -56,12 +50,12 @@ public Base58(Base58Alphabet alphabet)
/// <summary>
/// Gets the encoding alphabet.
/// </summary>
public Base58Alphabet Alphabet { get; }
public Base58Alphabet Alphabet { get; } = alphabet;

/// <summary>
/// Gets the character for zero.
/// </summary>
public char ZeroChar { get; }
public char ZeroChar { get; } = alphabet.Value[0];

/// <summary>
/// Retrieve safe byte count while avoiding multiple counting operations.
Expand Down Expand Up @@ -246,7 +240,7 @@ public byte[] Decode(ReadOnlySpan<char> text)
{
if (text.Length == 0)
{
return Array.Empty<byte>();
return [];
}

char zeroChar = ZeroChar;
Expand Down
17 changes: 6 additions & 11 deletions src/Base58Alphabet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ namespace SimpleBase;
/// <summary>
/// Base58 alphabet.
/// </summary>
public sealed class Base58Alphabet : CodingAlphabet
/// <remarks>
/// Initializes a new instance of the <see cref="Base58Alphabet"/> class
/// using a custom alphabet.
/// </remarks>
/// <param name="alphabet">Alphabet to use.</param>
public sealed class Base58Alphabet(string alphabet) : CodingAlphabet(58, alphabet)
{
private static readonly Lazy<Base58Alphabet> bitcoinAlphabet = new(()
=> new Base58Alphabet("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"));
Expand All @@ -21,16 +26,6 @@ public sealed class Base58Alphabet : CodingAlphabet
private static readonly Lazy<Base58Alphabet> flickrAlphabet = new(()
=> new Base58Alphabet("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"));

/// <summary>
/// Initializes a new instance of the <see cref="Base58Alphabet"/> class
/// using a custom alphabet.
/// </summary>
/// <param name="alphabet">Alphabet to use.</param>
public Base58Alphabet(string alphabet)
: base(58, alphabet)
{
}

/// <summary>
/// Gets Bitcoin alphabet.
/// </summary>
Expand Down
21 changes: 8 additions & 13 deletions src/Base85.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ namespace SimpleBase;
/// <summary>
/// Base58 encoding/decoding class.
/// </summary>
public class Base85 : IBaseCoder, IBaseStreamCoder, INonAllocatingBaseCoder
/// <remarks>
/// Initializes a new instance of the <see cref="Base85"/> class
/// using a custom alphabet.
/// </remarks>
/// <param name="alphabet">Alphabet to use.</param>
public class Base85(Base85Alphabet alphabet) : IBaseCoder, IBaseStreamCoder, INonAllocatingBaseCoder
{
private const int baseLength = 85;
private const int byteBlockSize = 4;
Expand All @@ -25,16 +30,6 @@ public class Base85 : IBaseCoder, IBaseStreamCoder, INonAllocatingBaseCoder
private static readonly Lazy<Base85> ascii85 = new(() => new Base85(Base85Alphabet.Ascii85));
private static readonly Lazy<Base85Ipv6> rfc1924 = new(() => new Base85Ipv6(Base85Alphabet.Rfc1924));

/// <summary>
/// Initializes a new instance of the <see cref="Base85"/> class
/// using a custom alphabet.
/// </summary>
/// <param name="alphabet">Alphabet to use.</param>
public Base85(Base85Alphabet alphabet)
{
Alphabet = alphabet;
}

/// <summary>
/// Gets Z85 flavor of Base85.
/// </summary>
Expand All @@ -53,7 +48,7 @@ public Base85(Base85Alphabet alphabet)
/// <summary>
/// Gets the encoding alphabet.
/// </summary>
public Base85Alphabet Alphabet { get; }
public Base85Alphabet Alphabet { get; } = alphabet;

/// <inheritdoc/>
public int GetSafeByteCountForDecoding(ReadOnlySpan<char> text)
Expand Down Expand Up @@ -154,7 +149,7 @@ public byte[] Decode(ReadOnlySpan<char> text)
{
if (text.Length == 0)
{
return Array.Empty<byte>();
return [];
}

// allocate a larger buffer if we're using shortcuts
Expand Down
33 changes: 13 additions & 20 deletions src/Base85Alphabet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ namespace SimpleBase;
/// <summary>
/// Base85 Alphabet.
/// </summary>
public sealed class Base85Alphabet : CodingAlphabet
/// <remarks>
/// Initializes a new instance of the <see cref="Base85Alphabet"/> class
/// using custom settings.
/// </remarks>
/// <param name="alphabet">Alphabet to use.</param>
/// <param name="allZeroShortcut">Character to substitute for all zero.</param>
/// <param name="allSpaceShortcut">Character to substitute for all space.</param>
public sealed class Base85Alphabet(
string alphabet,
char? allZeroShortcut = null,
char? allSpaceShortcut = null) : CodingAlphabet(85, alphabet)
{
private static readonly Lazy<Base85Alphabet> z85 = new(() => new Base85Alphabet(
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"));
Expand All @@ -23,23 +33,6 @@ public sealed class Base85Alphabet : CodingAlphabet
private static readonly Lazy<Base85Alphabet> rfc1924 = new(() => new Base85Alphabet(
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"));

/// <summary>
/// Initializes a new instance of the <see cref="Base85Alphabet"/> class
/// using custom settings.
/// </summary>
/// <param name="alphabet">Alphabet to use.</param>
/// <param name="allZeroShortcut">Character to substitute for all zero.</param>
/// <param name="allSpaceShortcut">Character to substitute for all space.</param>
public Base85Alphabet(
string alphabet,
char? allZeroShortcut = null,
char? allSpaceShortcut = null)
: base(85, alphabet)
{
AllZeroShortcut = allZeroShortcut;
AllSpaceShortcut = allSpaceShortcut;
}

/// <summary>
/// Gets ZeroMQ Z85 Alphabet.
/// </summary>
Expand All @@ -59,12 +52,12 @@ public Base85Alphabet(
/// <summary>
/// Gets the character to be used for "all zeros".
/// </summary>
public char? AllZeroShortcut { get; }
public char? AllZeroShortcut { get; } = allZeroShortcut;

/// <summary>
/// Gets the character to be used for "all spaces".
/// </summary>
public char? AllSpaceShortcut { get; }
public char? AllSpaceShortcut { get; } = allSpaceShortcut;

/// <summary>
/// Gets a value indicating whether the alphabet uses one of shortcut characters for all spaces
Expand Down
15 changes: 5 additions & 10 deletions src/Base85Ipv6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,16 @@ namespace SimpleBase;
/// So, that's why I only included a proof of concept implementation instead of working on optimizing it.
/// RFC 1924 should die, and this code should only be used to support some obscure standard or code somewhere.
/// </remarks>
public class Base85Ipv6 : Base85
/// <remarks>
/// Initializes a new instance of the <see cref="Base85Ipv6"/> class.
/// </remarks>
/// <param name="alphabet">Coding alphabet.</param>
public class Base85Ipv6(Base85Alphabet alphabet) : Base85(alphabet)
{
private const int ipv6bytes = 16;
private const int ipv6chars = 20;
private static readonly BigInteger divisor = new(85);

/// <summary>
/// Initializes a new instance of the <see cref="Base85Ipv6"/> class.
/// </summary>
/// <param name="alphabet">Coding alphabet.</param>
public Base85Ipv6(Base85Alphabet alphabet)
: base(alphabet)
{
}

/// <summary>
/// Encode IPv6 address into RFC 1924 Base85 text.
/// </summary>
Expand Down
4 changes: 0 additions & 4 deletions src/SimpleBase.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="IDisposableAnalyzers" Version="4.0.7">
<PrivateAssets>all</PrivateAssets>
Expand Down
26 changes: 13 additions & 13 deletions test/Base16/Base16Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ namespace SimpleBaseTest.Base16Test;
[Parallelizable]
internal class Base16Test
{
private static readonly Base16[] encoders = new[]
{
private static readonly Base16[] encoders =
[
Base16.LowerCase,
Base16.UpperCase,
Base16.ModHex
};

private static readonly object[][] testCases = new[]
{ // LowerCase // UpperCase // ModHex
new object[] { Array.Empty<byte>(), "", "", "" },
new object[] { new byte[] { 0xAB }, "ab", "AB", "ln" },
new object[] { new byte[] { 0x00, 0x01, 0x02, 0x03 }, "00010203", "00010203", "cccbcdce" },
new object[] { new byte[] { 0x10, 0x11, 0x12, 0x13 }, "10111213", "10111213", "bcbbbdbe" },
new object[] { new byte[] { 0xAB, 0xCD, 0xEF, 0xBA }, "abcdefba", "ABCDEFBA", "lnrtuvnl" },
new object[] { new byte[] { 0xAB, 0xCD, 0xEF, 0xBA, 0xAB, 0xCD, 0xEF, 0xBA }, "abcdefbaabcdefba", "ABCDEFBAABCDEFBA", "lnrtuvnllnrtuvnl" },
};
];

private static readonly object[][] testCases =
[ // LowerCase // UpperCase // ModHex
[Array.Empty<byte>(), "", "", ""],
[new byte[] { 0xAB }, "ab", "AB", "ln"],
[new byte[] { 0x00, 0x01, 0x02, 0x03 }, "00010203", "00010203", "cccbcdce"],
[new byte[] { 0x10, 0x11, 0x12, 0x13 }, "10111213", "10111213", "bcbbbdbe"],
[new byte[] { 0xAB, 0xCD, 0xEF, 0xBA }, "abcdefba", "ABCDEFBA", "lnrtuvnl"],
[new byte[] { 0xAB, 0xCD, 0xEF, 0xBA, 0xAB, 0xCD, 0xEF, 0xBA }, "abcdefbaabcdefba", "ABCDEFBAABCDEFBA", "lnrtuvnllnrtuvnl"],
];

private static IEnumerable<TestCaseData> testData
{
Expand Down
30 changes: 15 additions & 15 deletions test/Base32/Bech32Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ namespace SimpleBaseTest.Base32Test;
class Bech32Test
{
// test data was genereated with cryptii.com with a custom alphabet
private static readonly string[][] testData = new[]
{
new[] { "", "" },
new[] {"f", "vc======" },
new[] {"fo", "vehs====" },
new[] {"foo", "vehk7===" },
new[] {"foob", "vehk7cs=" },
new[] {"fooba", "vehk7cnp" },
new[] {"foobar", "vehk7cnpwg======" },
new[] {"foobar1", "vehk7cnpwgcs====" },
new[] {"foobar12", "vehk7cnpwgcny===" },
new[] {"foobar123", "vehk7cnpwgcnyvc=" },
new[] {"foobar1234", "vehk7cnpwgcnyve5" },
new[] {"1234567890123456789012345678901234567890", "xyerxdp4xcmnswfsxyerxdp4xcmnswfsxyerxdp4xcmnswfsxyerxdp4xcmnswfs" },
};
private static readonly string[][] testData =
[
["", ""],
["f", "vc======"],
["fo", "vehs===="],
["foo", "vehk7==="],
["foob", "vehk7cs="],
["fooba", "vehk7cnp"],
["foobar", "vehk7cnpwg======"],
["foobar1", "vehk7cnpwgcs===="],
["foobar12", "vehk7cnpwgcny==="],
["foobar123", "vehk7cnpwgcnyvc="],
["foobar1234", "vehk7cnpwgcnyve5"],
["1234567890123456789012345678901234567890", "xyerxdp4xcmnswfsxyerxdp4xcmnswfsxyerxdp4xcmnswfsxyerxdp4xcmnswfs"],
];

[Test]
[TestCaseSource(nameof(testData))]
Expand Down
Loading

0 comments on commit d7b5ab4

Please sign in to comment.