using System;
using System.Buffers.Text;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace SmallInjectorDemo
{
///
/// Static helper class for generating random numbers.
///
public static class Helper
{
///
/// Encode Base64 string
///
public static string EncodeBase64String(this Guid guid)
{
Span guidBytes = stackalloc byte[16];
Span encodedBytes = stackalloc byte[24];
MemoryMarshal.TryWrite(guidBytes, ref guid); // write bytes from the Guid
Base64.EncodeToUtf8(guidBytes, encodedBytes, out _, out _);
// skip the last two bytes as these will be '==' padding
var final = Encoding.UTF8.GetString(encodedBytes.Slice(0, 22));
return final;
}
///
/// Write method string.
///
/// The current clock
/// id of the object
/// member name
/// file path
///
public static string WriteMethodString(IClock clock, string id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null)
{
var classname = Path.GetFileNameWithoutExtension(filepath);
return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
}
}
}