Skip to content

Commit

Permalink
Update encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Shephard committed Apr 16, 2024
1 parent 1fb8a31 commit 6338bf0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 115 deletions.
59 changes: 59 additions & 0 deletions IOUtils/Data/Encoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Numerics;
using System.Text;

namespace IOUtils.Data;

public class Encoder {
public static readonly Encoder Base2 = new("01");
public static readonly Encoder Base10 = new("0123456789");
public static readonly Encoder Base16 = new("0123456789ABCDEF");
public static readonly Encoder Base36 = new("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
public static readonly Encoder Base62 = new("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
public static readonly Encoder Base64 = new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
public static readonly Encoder Base64UriSafe = new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");

public char[] Characters { get; }

public Encoder(char[] characters) {
if (characters.Length < 2)
throw new ArgumentException("The encoder must have at least two characters", nameof(characters));

if (characters.Length != characters.Distinct().Count())
throw new ArgumentException("The encoder must not have duplicate characters", nameof(characters));

Characters = characters;
}

public Encoder(string characters) : this(characters.ToCharArray()) { }

public string Encode(byte[] bytes, bool isBigEndian = true) {
BigInteger dividend = new(bytes, true, isBigEndian);

StringBuilder encodedStringBuilder = new();

while (!dividend.IsZero) {
dividend = BigInteger.DivRem(dividend, Characters.Length, out BigInteger remainder);

char encodedChar = Characters[(int)remainder];
encodedStringBuilder.Insert(0, encodedChar);
}

return encodedStringBuilder.ToString();
}

public byte[] Decode(string encoded, bool isBigEndian = true) {
BigInteger decodedValue = BigInteger.Zero;

foreach (int charIndex in encoded.Select(encodedChar => Array.IndexOf(Characters, encodedChar))) {
if (charIndex is -1) {
throw new ArgumentException("The encoding contains unknown characters", nameof(encoded));
}

decodedValue = BigInteger.Add(BigInteger.Multiply(decodedValue, Characters.Length), charIndex);
}

return decodedValue.IsZero
? Array.Empty<byte>()
: decodedValue.ToByteArray(true, isBigEndian);
}
}
15 changes: 0 additions & 15 deletions IOUtils/Encoders/Base10Encoder.cs

This file was deleted.

15 changes: 0 additions & 15 deletions IOUtils/Encoders/Base16Encoder.cs

This file was deleted.

15 changes: 0 additions & 15 deletions IOUtils/Encoders/Base2Encoder.cs

This file was deleted.

15 changes: 0 additions & 15 deletions IOUtils/Encoders/Base36Encoder.cs

This file was deleted.

16 changes: 0 additions & 16 deletions IOUtils/Encoders/Base64Encoder.cs

This file was deleted.

39 changes: 0 additions & 39 deletions IOUtils/Utils/EncodingUtils.cs

This file was deleted.

0 comments on commit 6338bf0

Please sign in to comment.