-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fb8a31
commit 6338bf0
Showing
7 changed files
with
59 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.