[CHANGE] Refactored DotBased utilities
This commit is contained in:
@@ -5,9 +5,9 @@ namespace DotBased.Utilities;
|
|||||||
|
|
||||||
public static class Culture
|
public static class Culture
|
||||||
{
|
{
|
||||||
private static List<CultureInfo> _sysCultures = new List<CultureInfo>();
|
private static List<CultureInfo> _sysCultures = [];
|
||||||
private static Dictionary<string, RegionInfo> _regions = new Dictionary<string, RegionInfo>();
|
private static readonly Dictionary<string, RegionInfo> Regions = new();
|
||||||
private static readonly ILogger _logger = LogService.RegisterLogger(typeof(Culture));
|
private static readonly ILogger Logger = LogService.RegisterLogger(typeof(Culture));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all system known cultures.
|
/// Get all system known cultures.
|
||||||
@@ -16,7 +16,7 @@ public static class Culture
|
|||||||
/// <returns>The list with <see cref="CultureInfo"/>'s the system knows</returns>
|
/// <returns>The list with <see cref="CultureInfo"/>'s the system knows</returns>
|
||||||
public static IEnumerable<CultureInfo> GetSystemCultures()
|
public static IEnumerable<CultureInfo> GetSystemCultures()
|
||||||
{
|
{
|
||||||
_logger.Debug("Getting system cultures...");
|
Logger.Debug("Getting system cultures...");
|
||||||
if (_sysCultures.Count == 0)
|
if (_sysCultures.Count == 0)
|
||||||
_sysCultures = CultureInfo.GetCultures(CultureTypes.AllCultures).ToList();
|
_sysCultures = CultureInfo.GetCultures(CultureTypes.AllCultures).ToList();
|
||||||
return _sysCultures;
|
return _sysCultures;
|
||||||
@@ -29,16 +29,16 @@ public static class Culture
|
|||||||
/// <returns>A list with regions from the system</returns>
|
/// <returns>A list with regions from the system</returns>
|
||||||
public static Dictionary<string, RegionInfo> GetRegions()
|
public static Dictionary<string, RegionInfo> GetRegions()
|
||||||
{
|
{
|
||||||
if (_regions.Count == 0)
|
if (Regions.Count == 0)
|
||||||
{
|
{
|
||||||
var cultureInfos = GetSystemCultures().Where(cul => !cul.IsNeutralCulture).Where(cul => cul.LCID != 0x7F);
|
var cultureInfos = GetSystemCultures().Where(cul => !cul.IsNeutralCulture).Where(cul => cul.LCID != 0x7F);
|
||||||
foreach (var culture in cultureInfos)
|
foreach (var culture in cultureInfos)
|
||||||
{
|
{
|
||||||
var region = new RegionInfo(culture.Name);
|
var region = new RegionInfo(culture.Name);
|
||||||
_regions.Add(culture.Name, region);
|
Regions.Add(culture.Name, region);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return _regions;
|
return Regions;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -52,7 +52,7 @@ public static class Culture
|
|||||||
_sysCultures.Clear();
|
_sysCultures.Clear();
|
||||||
break;
|
break;
|
||||||
case CacheType.Region:
|
case CacheType.Region:
|
||||||
_regions.Clear();
|
Regions.Clear();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new ArgumentOutOfRangeException(nameof(type), type, null);
|
throw new ArgumentOutOfRangeException(nameof(type), type, null);
|
||||||
|
@@ -12,29 +12,30 @@ public static class Suffix
|
|||||||
/// Converts the bytes to the memory suffix.
|
/// Converts the bytes to the memory suffix.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="bytes">The bytes to convert</param>
|
/// <param name="bytes">The bytes to convert</param>
|
||||||
/// <param name="decimalPlaces">How manay decimal places will be placed</param>
|
/// <param name="decimalPlaces">How manny decimal places will be placed</param>
|
||||||
/// <returns>The suffixed bytes in the correct format</returns>
|
/// <returns>The suffixed bytes in the correct format</returns>
|
||||||
public static string BytesToSizeSuffix(long bytes, int decimalPlaces = 1)
|
public static string BytesToSizeSuffix(long bytes, int decimalPlaces = 1)
|
||||||
{
|
{
|
||||||
if (decimalPlaces < 0)
|
if (decimalPlaces < 0) decimalPlaces = 1;
|
||||||
decimalPlaces = 1;
|
|
||||||
switch (bytes)
|
if (bytes == 0)
|
||||||
|
return $"{0.ToString($"N{decimalPlaces}")} bytes";
|
||||||
|
|
||||||
|
var negative = bytes < 0;
|
||||||
|
var absBytes = (ulong)(negative ? -bytes : bytes);
|
||||||
|
|
||||||
|
var mag = (int)Math.Log(absBytes, 1024);
|
||||||
|
var adjustedSize = absBytes / Math.Pow(1024, mag);
|
||||||
|
|
||||||
|
if (Math.Round(adjustedSize, decimalPlaces) >= 1000 && mag < SizeSuffixes.Length - 1)
|
||||||
{
|
{
|
||||||
case < 0:
|
mag++;
|
||||||
return "-" + BytesToSizeSuffix(-bytes, decimalPlaces);
|
|
||||||
case 0:
|
|
||||||
return string.Format("{0:n" + decimalPlaces + "} bytes", 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int mag = (int)Math.Log(bytes, 1024);
|
|
||||||
|
|
||||||
decimal adjustedSize = (decimal)bytes / (1L << (mag * 10));
|
|
||||||
|
|
||||||
if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
|
|
||||||
{
|
|
||||||
mag += 1;
|
|
||||||
adjustedSize /= 1024;
|
adjustedSize /= 1024;
|
||||||
}
|
}
|
||||||
return string.Format("{0:n" + decimalPlaces + "} {1}", adjustedSize, SizeSuffixes[mag]);
|
|
||||||
|
var format = $"N{decimalPlaces}";
|
||||||
|
var result = $"{adjustedSize.ToString(format)} {SizeSuffixes[mag]}";
|
||||||
|
|
||||||
|
return negative ? "-" + result : result;
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user