new extension classes
This commit is contained in:
parent
ef349eceb1
commit
0e5c79c01a
@ -0,0 +1,63 @@
|
|||||||
|
using System.Buffers.Text;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace EnotaryoPH.Web.Common.Extensions
|
||||||
|
{
|
||||||
|
public static class GuidExtensions
|
||||||
|
{
|
||||||
|
private const char Dash = '-';
|
||||||
|
private const char EqualsChar = '=';
|
||||||
|
private const byte ForwardSlashByte = (byte)'/';
|
||||||
|
private const char Plus = '+';
|
||||||
|
private const byte PlusByte = (byte)'+';
|
||||||
|
private const char Slash = '/';
|
||||||
|
private const char Underscore = '_';
|
||||||
|
|
||||||
|
public static string ToBase64String(this Guid guid)
|
||||||
|
{
|
||||||
|
Span<byte> guidBytes = stackalloc byte[16];
|
||||||
|
Span<byte> encodedBytes = stackalloc byte[24];
|
||||||
|
|
||||||
|
MemoryMarshal.TryWrite(guidBytes, ref guid); // write bytes from the Guid
|
||||||
|
Base64.EncodeToUtf8(guidBytes, encodedBytes, out _, out _);
|
||||||
|
|
||||||
|
Span<char> chars = stackalloc char[22];
|
||||||
|
|
||||||
|
// replace any characters which are not URL safe
|
||||||
|
// skip the final two bytes as these will be '==' padding we don't need
|
||||||
|
for (var i = 0; i < 22; i++)
|
||||||
|
{
|
||||||
|
chars[i] = encodedBytes[i] switch
|
||||||
|
{
|
||||||
|
ForwardSlashByte => Dash,
|
||||||
|
PlusByte => Underscore,
|
||||||
|
_ => (char)encodedBytes[i],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
var final = new string(chars);
|
||||||
|
|
||||||
|
return final;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Guid ToGuidFromString(ReadOnlySpan<char> id)
|
||||||
|
{
|
||||||
|
Span<char> base64Chars = stackalloc char[24];
|
||||||
|
for (var i = 0; i < 22; i++)
|
||||||
|
{
|
||||||
|
base64Chars[i] = id[i] switch
|
||||||
|
{
|
||||||
|
'-' => Slash,
|
||||||
|
'_' => Plus,
|
||||||
|
_ => id[i]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
base64Chars[22] = EqualsChar;
|
||||||
|
base64Chars[23] = EqualsChar;
|
||||||
|
|
||||||
|
Span<byte> idBytes = stackalloc byte[16];
|
||||||
|
Convert.TryFromBase64Chars(base64Chars, idBytes, out _);
|
||||||
|
return new Guid(idBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,5 +3,7 @@
|
|||||||
public static class StringExtensions
|
public static class StringExtensions
|
||||||
{
|
{
|
||||||
public static string DefaultIfEmpty(this string s, string defaultValue) => !string.IsNullOrWhiteSpace(s) ? s : (defaultValue ?? string.Empty);
|
public static string DefaultIfEmpty(this string s, string defaultValue) => !string.IsNullOrWhiteSpace(s) ? s : (defaultValue ?? string.Empty);
|
||||||
|
|
||||||
|
public static string NullIfWhiteSpace(this string s) => string.IsNullOrWhiteSpace(s) ? null : s;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user