mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-01-04 20:38:24 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace Sandbox;
|
|
|
|
public static partial class SandboxSystemExtensions
|
|
{
|
|
private const string CharList = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
private static char[] CharArray = CharList.ToCharArray();
|
|
|
|
/// <summary>
|
|
/// Encode the given number into a Base36 string
|
|
/// </summary>
|
|
public static string ToBase36<T>( this T i ) where T : struct, IComparable, IComparable<T>, IConvertible, IEquatable<T>, IFormattable
|
|
{
|
|
var input = (long)Convert.ToDecimal( i );
|
|
|
|
if ( input < 0 ) throw new ArgumentOutOfRangeException( "input", input, "input cannot be negative" );
|
|
|
|
char[] clistarr = CharList.ToCharArray();
|
|
var result = new Stack<char>();
|
|
while ( input != 0 )
|
|
{
|
|
result.Push( clistarr[input % 36] );
|
|
input /= 36;
|
|
}
|
|
|
|
return new string( result.ToArray() );
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decode the Base36 Encoded string into a number
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public static long FromBase36( this string input )
|
|
{
|
|
var reversed = input.ToLower().Reverse();
|
|
long result = 0;
|
|
int pos = 0;
|
|
foreach ( char c in reversed )
|
|
{
|
|
result += CharList.IndexOf( c ) * (long)System.Math.Pow( 36, pos );
|
|
pos++;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|