[CHANGE] Refactored DotBased utilities

This commit is contained in:
max
2025-08-02 17:05:50 +02:00
parent 5dbb91de94
commit 38832fd597
2 changed files with 27 additions and 26 deletions

View File

@@ -5,9 +5,9 @@ namespace DotBased.Utilities;
public static class Culture
{
private static List<CultureInfo> _sysCultures = new List<CultureInfo>();
private static Dictionary<string, RegionInfo> _regions = new Dictionary<string, RegionInfo>();
private static readonly ILogger _logger = LogService.RegisterLogger(typeof(Culture));
private static List<CultureInfo> _sysCultures = [];
private static readonly Dictionary<string, RegionInfo> Regions = new();
private static readonly ILogger Logger = LogService.RegisterLogger(typeof(Culture));
/// <summary>
/// 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>
public static IEnumerable<CultureInfo> GetSystemCultures()
{
_logger.Debug("Getting system cultures...");
Logger.Debug("Getting system cultures...");
if (_sysCultures.Count == 0)
_sysCultures = CultureInfo.GetCultures(CultureTypes.AllCultures).ToList();
return _sysCultures;
@@ -29,16 +29,16 @@ public static class Culture
/// <returns>A list with regions from the system</returns>
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);
foreach (var culture in cultureInfos)
{
var region = new RegionInfo(culture.Name);
_regions.Add(culture.Name, region);
Regions.Add(culture.Name, region);
}
}
return _regions;
return Regions;
}
/// <summary>
@@ -52,7 +52,7 @@ public static class Culture
_sysCultures.Clear();
break;
case CacheType.Region:
_regions.Clear();
Regions.Clear();
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);

View File

@@ -12,29 +12,30 @@ public static class Suffix
/// Converts the bytes to the memory suffix.
/// </summary>
/// <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>
public static string BytesToSizeSuffix(long bytes, int decimalPlaces = 1)
{
if (decimalPlaces < 0)
decimalPlaces = 1;
switch (bytes)
if (decimalPlaces < 0) decimalPlaces = 1;
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:
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;
mag++;
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;
}
}