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

View file

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

View file

@ -77,7 +77,7 @@ namespace ServiceLib.Handler.Fmt
return null; return null;
} }
item.Address = arr1[1][..indexPort]; 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.Security = arr21.First();
item.Id = arr21[1]; item.Id = arr21[1];

View file

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

View file

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

View file

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

View file

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

View file

@ -375,7 +375,7 @@ namespace v2rayN.Views
lvColumnItem.Add(new() lvColumnItem.Add(new()
{ {
Name = item2.ExName, 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 Index = item2.DisplayIndex
}); });
} }