Port to .NET 5

This commit is contained in:
Dankrushen
2021-01-26 02:37:16 -05:00
parent 0a236361e0
commit 72dcf90b68
95 changed files with 24769 additions and 1 deletions

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Flowframes.MiscUtils
{
class Benchmarker
{
static Stopwatch sw = new Stopwatch();
public static void Start ()
{
sw.Restart();
}
public static string GetTimeStr (bool stop)
{
if (stop)
sw.Stop();
return FormatUtils.TimeSw(sw);
}
public static TimeSpan GetTime(bool stop)
{
if (stop)
sw.Stop();
return sw.Elapsed;
}
public static long GetTimeMs(bool stop)
{
if (stop)
sw.Stop();
return sw.ElapsedMilliseconds;
}
}
}

View File

@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Flowframes.MiscUtils
{
class FormatUtils
{
public static string Bytes(long sizeBytes)
{
try
{
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
if (sizeBytes == 0)
return "0" + suf[0];
long bytes = Math.Abs(sizeBytes);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
return ($"{Math.Sign(sizeBytes) * num} {suf[place]}");
}
catch
{
return "N/A B";
}
}
public static string Time(long milliseconds)
{
double secs = (milliseconds / 1000f);
if (milliseconds <= 1000)
{
return milliseconds + "ms";
}
return secs.ToString("0.00") + "s";
}
public static string Time (TimeSpan span, bool allowMs = true)
{
if(span.TotalHours >= 1f)
return span.ToString(@"hh\:mm\:ss");
if (span.TotalMinutes >= 1f)
return span.ToString(@"mm\:ss");
if (span.TotalSeconds >= 1f || !allowMs)
return span.ToString(@"ss".TrimStart('0').PadLeft(1, '0')) + "s";
return span.ToString(@"fff").TrimStart('0').PadLeft(1, '0') + "ms";
}
public static string TimeSw(Stopwatch sw)
{
long elapsedMs = sw.ElapsedMilliseconds;
return Time(elapsedMs);
}
public static string Ratio(long numFrom, long numTo)
{
float ratio = ((float)numTo / (float)numFrom) * 100f;
return ratio.ToString("0.00") + "%";
}
public static string RatioInt(long numFrom, long numTo)
{
double ratio = Math.Round(((float)numTo / (float)numFrom) * 100f);
return ratio + "%";
}
public static string ConcatStrings(string[] strings, char delimiter = ',', bool distinct = false)
{
string outStr = "";
strings = strings.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
if(distinct)
strings = strings.Distinct().ToArray();
for (int i = 0; i < strings.Length; i++)
{
outStr += strings[i];
if (i + 1 != strings.Length)
outStr += delimiter;
}
return outStr;
}
}
}