|
| 1 | +// <auto-generated /> |
| 2 | +#region License |
| 3 | +// MIT License |
| 4 | +// |
| 5 | +// Copyright (c) Daniel Cazzulino |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | +#endregion |
| 25 | + |
| 26 | +#nullable enable |
| 27 | +using System.Linq; |
| 28 | +using System.Numerics; |
| 29 | +using System.Text; |
| 30 | + |
| 31 | +namespace System |
| 32 | +{ |
| 33 | + /// <summary> |
| 34 | + /// Inspired in a bunch of searches, samples and snippets on various languages |
| 35 | + /// and blogs and what-not on doing URL shortering :), heavily tweaked to make |
| 36 | + /// it fully idiomatic in C#. |
| 37 | + /// </summary> |
| 38 | + static partial class Base62 |
| 39 | + { |
| 40 | + /// <summary> |
| 41 | + /// Encodes a Guid into a base62 string. |
| 42 | + /// </summary> |
| 43 | + public static string ToBase62(this Guid guid) |
| 44 | + => Encode(new BigInteger(guid.ToByteArray())); |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Encodes the absolute numeric value into a base62 string. |
| 48 | + /// </summary> |
| 49 | + public static string Encode(BigInteger value) |
| 50 | + { |
| 51 | + if (value == 0) |
| 52 | + return "0"; |
| 53 | + |
| 54 | + // normalize sign in case we got negative. |
| 55 | + value = BigInteger.Abs(value); |
| 56 | + |
| 57 | + // TODO: I'm almost sure there's a more succint way |
| 58 | + // of doing this with LINQ and Aggregate, but just |
| 59 | + // can't figure it out... |
| 60 | + var sb = new StringBuilder(); |
| 61 | + |
| 62 | + while (value != 0) |
| 63 | + { |
| 64 | + sb = sb.Append(ToBase62(value % 62)); |
| 65 | + value /= 62; |
| 66 | + } |
| 67 | + |
| 68 | + // Reverse the string, since we're building it backwards, |
| 69 | + var chars = sb.ToString().ToCharArray(); |
| 70 | + Array.Reverse(chars); |
| 71 | + |
| 72 | + return new string(chars); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Decodes a base62 string into its original numeric value. |
| 77 | + /// </summary> |
| 78 | + public static BigInteger Decode(string value) |
| 79 | + => value.Aggregate(new BigInteger(0), (current, c) => current * 62 + FromBase62(c)); |
| 80 | + |
| 81 | + static char ToBase62(BigInteger d) => d switch |
| 82 | + { |
| 83 | + BigInteger v when v < 10 => (char)('0' + d), |
| 84 | + BigInteger v when v < 36 => (char)('A' + d - 10), |
| 85 | + BigInteger v when v < 62 => (char)('a' + d - 36), |
| 86 | + _ => throw new ArgumentException($"Cannot encode digit {d} to base 62.", nameof(d)), |
| 87 | + }; |
| 88 | + |
| 89 | + static BigInteger FromBase62(char c) => c switch |
| 90 | + { |
| 91 | + char v when c >= 'a' && v <= 'z' => 36 + c - 'a', |
| 92 | + char v when c >= 'A' && v <= 'Z' => 10 + c - 'A', |
| 93 | + char v when c >= '0' && v <= '9' => c - '0', |
| 94 | + _ => throw new ArgumentException($"Cannot decode char '{c}' from base 62.", nameof(c)), |
| 95 | + }; |
| 96 | + } |
| 97 | +} |
0 commit comments