class extensions
This commit is contained in:
parent
4d5a69ad2f
commit
fc98e4eb65
@ -6,11 +6,8 @@ namespace EnotaryoPH.Web.Common.Extensions
|
|||||||
public static class GuidExtensions
|
public static class GuidExtensions
|
||||||
{
|
{
|
||||||
private const char Dash = '-';
|
private const char Dash = '-';
|
||||||
private const char EqualsChar = '=';
|
|
||||||
private const byte ForwardSlashByte = (byte)'/';
|
private const byte ForwardSlashByte = (byte)'/';
|
||||||
private const char Plus = '+';
|
|
||||||
private const byte PlusByte = (byte)'+';
|
private const byte PlusByte = (byte)'+';
|
||||||
private const char Slash = '/';
|
|
||||||
private const char Underscore = '_';
|
private const char Underscore = '_';
|
||||||
|
|
||||||
public static string ToBase64String(this Guid guid)
|
public static string ToBase64String(this Guid guid)
|
||||||
@ -39,25 +36,5 @@ namespace EnotaryoPH.Web.Common.Extensions
|
|||||||
|
|
||||||
return final;
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,8 +2,38 @@
|
|||||||
{
|
{
|
||||||
public static class StringExtensions
|
public static class StringExtensions
|
||||||
{
|
{
|
||||||
|
private const char EqualsChar = '=';
|
||||||
|
private const char Plus = '+';
|
||||||
|
private const char Slash = '/';
|
||||||
|
|
||||||
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;
|
public static string NullIfWhiteSpace(this string s) => string.IsNullOrWhiteSpace(s) ? null : s;
|
||||||
|
|
||||||
|
public static Guid ToGuidFromString(this string s)
|
||||||
|
{
|
||||||
|
if (s.Length != 22)
|
||||||
|
{
|
||||||
|
return Guid.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var id = s.AsSpan();
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user