Update Utils.cs

AI-optimized code
This commit is contained in:
2dust 2025-03-06 10:47:06 +08:00
parent 0a8ce0f961
commit d35f65f86d

View file

@ -27,28 +27,23 @@ namespace ServiceLib.Common
/// <returns></returns>
public static string List2String(List<string>? lst, bool wrap = false)
{
try
{
if (lst == null)
if (lst == null || lst.Count == 0)
{
return string.Empty;
}
if (wrap)
var separator = wrap ? "," + Environment.NewLine : ",";
try
{
return string.Join("," + Environment.NewLine, lst);
}
else
{
return string.Join(",", lst);
}
return string.Join(separator, lst);
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
}
return string.Empty;
}
}
/// <summary>
/// Comma-separated string
@ -57,23 +52,22 @@ namespace ServiceLib.Common
/// <returns></returns>
public static List<string>? String2List(string? str)
{
try
{
if (str == null)
if (string.IsNullOrWhiteSpace(str))
{
return null;
}
str = str.Replace(Environment.NewLine, "");
return new(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
try
{
str = str.Replace(Environment.NewLine, string.Empty);
return new List<string>(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
}
return null;
}
}
/// <summary>
/// Comma-separated string, sorted and then converted to List
@ -82,19 +76,9 @@ namespace ServiceLib.Common
/// <returns></returns>
public static List<string>? String2ListSorted(string str)
{
try
{
str = str.Replace(Environment.NewLine, "");
List<string> list = new(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
list.Sort();
return list;
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
}
return null;
var lst = String2List(str);
lst?.Sort();
return lst;
}
/// <summary>
@ -179,55 +163,25 @@ namespace ServiceLib.Common
}
}
private static void ToHumanReadable(long amount, out double result, out string unit)
{
var factor = 1024u;
//long KBs = amount / factor;
var KBs = amount;
if (KBs > 0)
{
// multi KB
var MBs = KBs / factor;
if (MBs > 0)
{
// multi MB
var GBs = MBs / factor;
if (GBs > 0)
{
// multi GB
var TBs = GBs / factor;
if (TBs > 0)
{
result = TBs + ((GBs % factor) / (factor + 0.0));
unit = "TB";
return;
}
result = GBs + ((MBs % factor) / (factor + 0.0));
unit = "GB";
return;
}
result = MBs + ((KBs % factor) / (factor + 0.0));
unit = "MB";
return;
}
result = KBs + ((amount % factor) / (factor + 0.0));
unit = "KB";
return;
}
else
{
result = amount;
unit = "B";
}
}
public static string HumanFy(long amount)
{
ToHumanReadable(amount, out var result, out var unit);
return $"{result:f1} {unit}";
if (amount <= 0)
{
return $"{amount:f1} B";
}
string[] units = ["KB", "MB", "GB", "TB", "PB"];
var unitIndex = 0;
double size = amount;
// Loop and divide by 1024 until a suitable unit is found
while (size >= 1024 && unitIndex < units.Length - 1)
{
size /= 1024;
unitIndex++;
}
return $"{size:f1} {units[unitIndex]}";
}
public static string UrlEncode(string url)