mirror of
https://github.com/2dust/v2rayN.git
synced 2025-04-19 21:52:25 +00:00
Update Utils.cs
AI-optimized code
This commit is contained in:
parent
0a8ce0f961
commit
d35f65f86d
1 changed files with 37 additions and 83 deletions
|
@ -27,27 +27,22 @@ namespace ServiceLib.Common
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string List2String(List<string>? lst, bool wrap = false)
|
public static string List2String(List<string>? lst, bool wrap = false)
|
||||||
{
|
{
|
||||||
|
if (lst == null || lst.Count == 0)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var separator = wrap ? "," + Environment.NewLine : ",";
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (lst == null)
|
return string.Join(separator, lst);
|
||||||
{
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
if (wrap)
|
|
||||||
{
|
|
||||||
return string.Join("," + Environment.NewLine, lst);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return string.Join(",", lst);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logging.SaveLog(_tag, ex);
|
Logging.SaveLog(_tag, ex);
|
||||||
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
return string.Empty;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -57,22 +52,21 @@ namespace ServiceLib.Common
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static List<string>? String2List(string? str)
|
public static List<string>? String2List(string? str)
|
||||||
{
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(str))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (str == null)
|
str = str.Replace(Environment.NewLine, string.Empty);
|
||||||
{
|
return new List<string>(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
str = str.Replace(Environment.NewLine, "");
|
|
||||||
return new(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logging.SaveLog(_tag, ex);
|
Logging.SaveLog(_tag, ex);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -82,19 +76,9 @@ namespace ServiceLib.Common
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static List<string>? String2ListSorted(string str)
|
public static List<string>? String2ListSorted(string str)
|
||||||
{
|
{
|
||||||
try
|
var lst = String2List(str);
|
||||||
{
|
lst?.Sort();
|
||||||
str = str.Replace(Environment.NewLine, "");
|
return lst;
|
||||||
List<string> list = new(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
|
|
||||||
list.Sort();
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logging.SaveLog(_tag, ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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)
|
public static string HumanFy(long amount)
|
||||||
{
|
{
|
||||||
ToHumanReadable(amount, out var result, out var unit);
|
if (amount <= 0)
|
||||||
return $"{result:f1} {unit}";
|
{
|
||||||
|
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)
|
public static string UrlEncode(string url)
|
||||||
|
|
Loading…
Reference in a new issue