...
This commit is contained in:
38
src/compiler/NubLang/HexString.cs
Normal file
38
src/compiler/NubLang/HexString.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
namespace NubLang;
|
||||
|
||||
public static class HexString
|
||||
{
|
||||
private static readonly HashSet<string> _used = [];
|
||||
|
||||
private static readonly char[] StringChars = "0123456789abcdef".ToArray();
|
||||
|
||||
public static string CreateUnique(int length)
|
||||
{
|
||||
string hex;
|
||||
|
||||
while (true)
|
||||
{
|
||||
hex = Create(length);
|
||||
if (_used.Add(hex))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return hex;
|
||||
}
|
||||
|
||||
public static string Create(int length)
|
||||
{
|
||||
var rand = new Random();
|
||||
var hexString = "";
|
||||
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
var randIndex = rand.Next(0, StringChars.Length);
|
||||
hexString += StringChars[randIndex];
|
||||
}
|
||||
|
||||
return hexString;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user