Replace all Utils.ToInt(variable) with variable.ToInt()

This commit is contained in:
2dust 2025-03-05 20:26:26 +08:00
parent 71cc6d7a88
commit 764014e49a
9 changed files with 29 additions and 34 deletions

View file

@ -70,5 +70,10 @@ namespace ServiceLib.Common
{
return string.IsNullOrEmpty(value) ? string.Empty : $"\"{value}\"";
}
public static int ToInt(this string? value, int defaultValue = 0)
{
return int.TryParse(value, out var result) ? result : defaultValue;
}
}
}

View file

@ -20,7 +20,7 @@ namespace ServiceLib.Common
#region
/// <summary>
/// 转逗号分隔的字符串
/// Convert to comma-separated string
/// </summary>
/// <param name="lst"></param>
/// <param name="wrap"></param>
@ -51,7 +51,7 @@ namespace ServiceLib.Common
}
/// <summary>
/// 逗号分隔的字符串
/// Comma-separated string
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
@ -65,7 +65,7 @@ namespace ServiceLib.Common
}
str = str.Replace(Environment.NewLine, "");
return new List<string>(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
return new(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
}
catch (Exception ex)
{
@ -76,7 +76,7 @@ namespace ServiceLib.Common
}
/// <summary>
/// 逗号分隔的字符串,先排序后转List
/// Comma-separated string, sorted and then converted to List
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
@ -98,7 +98,7 @@ namespace ServiceLib.Common
}
/// <summary>
/// Base64编码
/// Base64 Encode
/// </summary>
/// <param name="plainText"></param>
/// <returns></returns>
@ -118,7 +118,7 @@ namespace ServiceLib.Common
}
/// <summary>
/// Base64解码
/// Base64 Decode
/// </summary>
/// <param name="plainText"></param>
/// <returns></returns>
@ -127,7 +127,10 @@ namespace ServiceLib.Common
try
{
if (plainText.IsNullOrEmpty())
{
return "";
}
plainText = plainText.Trim()
.Replace(Environment.NewLine, "")
.Replace("\n", "")
@ -152,18 +155,6 @@ namespace ServiceLib.Common
return string.Empty;
}
public static int ToInt(object? obj)
{
try
{
return Convert.ToInt32(obj ?? string.Empty);
}
catch
{
return 0;
}
}
public static bool ToBool(object obj)
{
try
@ -344,7 +335,7 @@ namespace ServiceLib.Common
#region
/// <summary>
/// 判断输入的是否是数字
/// Determine if the input is a number
/// </summary>
/// <param name="oText"></param>
/// <returns></returns>
@ -353,9 +344,8 @@ namespace ServiceLib.Common
return oText.All(char.IsNumber);
}
/// <summary>
/// 验证Domain地址是否合法
/// Validate if the domain address is valid
/// </summary>
/// <param name="domain"></param>
public static bool IsDomain(string? domain)
@ -477,7 +467,7 @@ namespace ServiceLib.Common
}
/// <summary>
/// 取得版本
/// Get version
/// </summary>
/// <returns></returns>
public static string GetVersion(bool blFull = true)
@ -515,7 +505,7 @@ namespace ServiceLib.Common
}
/// <summary>
/// 取得GUID
/// GUID
/// </summary>
/// <returns></returns>
public static string GetGuid(bool full = true)

View file

@ -76,7 +76,7 @@ namespace ServiceLib.Handler.Fmt
item.Security = details.Groups["method"].Value;
item.Id = details.Groups["password"].Value;
item.Address = details.Groups["hostname"].Value;
item.Port = Utils.ToInt(details.Groups["port"].Value);
item.Port = details.Groups["port"].Value.ToInt();
return item;
}
@ -162,7 +162,7 @@ namespace ServiceLib.Handler.Fmt
Security = it.method,
Id = it.password,
Address = it.server,
Port = Utils.ToInt(it.server_port)
Port = it.server_port.ToInt()
};
lst.Add(ssItem);
}

View file

@ -77,7 +77,7 @@ namespace ServiceLib.Handler.Fmt
return null;
}
item.Address = arr1[1][..indexPort];
item.Port = Utils.ToInt(arr1[1][(indexPort + 1)..]);
item.Port = (arr1[1][(indexPort + 1)..]).ToInt();
item.Security = arr21.First();
item.Id = arr21[1];

View file

@ -755,7 +755,7 @@ namespace ServiceLib.Services.CoreConfig
outbound.peer_public_key = node.PublicKey;
outbound.reserved = Utils.String2List(node.Path)?.Select(int.Parse).ToList();
outbound.local_address = Utils.String2List(node.RequestHost);
outbound.mtu = Utils.ToInt(node.ShortId.IsNullOrEmpty() ? Global.TunMtus.First() : node.ShortId);
outbound.mtu = node.ShortId.IsNullOrEmpty() ? Global.TunMtus.First() : node.ShortId.ToInt();
break;
}
}
@ -1093,7 +1093,7 @@ namespace ServiceLib.Services.CoreConfig
}
else
{
rule.port = new List<int> { Utils.ToInt(item.Port) };
rule.port = new List<int> { item.Port.ToInt() };
}
}
if (item.Network.IsNotEmpty())

View file

@ -461,8 +461,8 @@ namespace v2rayN.Desktop.Views
private void StorageUI(string? n = null)
{
_config.UiItem.MainWidth = Utils.ToInt(this.Width);
_config.UiItem.MainHeight = Utils.ToInt(this.Height);
_config.UiItem.MainWidth = this.Width;
_config.UiItem.MainHeight = this.Height;
if (_config.UiItem.MainGirdOrientation == EGirdOrientation.Horizontal)
{

View file

@ -404,7 +404,7 @@ namespace v2rayN.Desktop.Views
lvColumnItem.Add(new()
{
Name = (string)item2.Tag,
Width = item2.IsVisible == true ? Utils.ToInt(item2.ActualWidth) : -1,
Width = (int)(item2.IsVisible == true ? item2.ActualWidth : -1),
Index = item2.DisplayIndex
});
}

View file

@ -414,8 +414,8 @@ namespace v2rayN.Views
private void StorageUI(string? n = null)
{
_config.UiItem.MainWidth = Utils.ToInt(this.Width);
_config.UiItem.MainHeight = Utils.ToInt(this.Height);
_config.UiItem.MainWidth = this.Width;
_config.UiItem.MainHeight = this.Height;
if (_config.UiItem.MainGirdOrientation == EGirdOrientation.Horizontal)
{

View file

@ -375,7 +375,7 @@ namespace v2rayN.Views
lvColumnItem.Add(new()
{
Name = item2.ExName,
Width = item2.Visibility == Visibility.Visible ? Utils.ToInt(item2.ActualWidth) : -1,
Width = (int)(item2.Visibility == Visibility.Visible ? item2.ActualWidth : -1),
Index = item2.DisplayIndex
});
}