From 764014e49a2cc90f43219609cd0b4d569539cff1 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 5 Mar 2025 20:26:26 +0800 Subject: [PATCH] Replace all Utils.ToInt(variable) with variable.ToInt() --- v2rayN/ServiceLib/Common/StringEx.cs | 5 +++ v2rayN/ServiceLib/Common/Utils.cs | 36 +++++++------------ .../ServiceLib/Handler/Fmt/ShadowsocksFmt.cs | 4 +-- v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs | 2 +- .../CoreConfig/CoreConfigSingboxService.cs | 4 +-- .../v2rayN.Desktop/Views/MainWindow.axaml.cs | 4 +-- .../Views/ProfilesView.axaml.cs | 2 +- v2rayN/v2rayN/Views/MainWindow.xaml.cs | 4 +-- v2rayN/v2rayN/Views/ProfilesView.xaml.cs | 2 +- 9 files changed, 29 insertions(+), 34 deletions(-) diff --git a/v2rayN/ServiceLib/Common/StringEx.cs b/v2rayN/ServiceLib/Common/StringEx.cs index adeca8a2..12613a2e 100644 --- a/v2rayN/ServiceLib/Common/StringEx.cs +++ b/v2rayN/ServiceLib/Common/StringEx.cs @@ -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; + } } } diff --git a/v2rayN/ServiceLib/Common/Utils.cs b/v2rayN/ServiceLib/Common/Utils.cs index 48210b3f..4f9942f3 100644 --- a/v2rayN/ServiceLib/Common/Utils.cs +++ b/v2rayN/ServiceLib/Common/Utils.cs @@ -20,7 +20,7 @@ namespace ServiceLib.Common #region 转换函数 /// - /// 转逗号分隔的字符串 + /// Convert to comma-separated string /// /// /// @@ -51,7 +51,7 @@ namespace ServiceLib.Common } /// - /// 逗号分隔的字符串 + /// Comma-separated string /// /// /// @@ -65,7 +65,7 @@ namespace ServiceLib.Common } str = str.Replace(Environment.NewLine, ""); - return new List(str.Split(',', StringSplitOptions.RemoveEmptyEntries)); + return new(str.Split(',', StringSplitOptions.RemoveEmptyEntries)); } catch (Exception ex) { @@ -76,7 +76,7 @@ namespace ServiceLib.Common } /// - /// 逗号分隔的字符串,先排序后转List + /// Comma-separated string, sorted and then converted to List /// /// /// @@ -98,7 +98,7 @@ namespace ServiceLib.Common } /// - /// Base64编码 + /// Base64 Encode /// /// /// @@ -118,7 +118,7 @@ namespace ServiceLib.Common } /// - /// Base64解码 + /// Base64 Decode /// /// /// @@ -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 数据检查 /// - /// 判断输入的是否是数字 + /// Determine if the input is a number /// /// /// @@ -353,9 +344,8 @@ namespace ServiceLib.Common return oText.All(char.IsNumber); } - /// - /// 验证Domain地址是否合法 + /// Validate if the domain address is valid /// /// public static bool IsDomain(string? domain) @@ -477,7 +467,7 @@ namespace ServiceLib.Common } /// - /// 取得版本 + /// Get version /// /// public static string GetVersion(bool blFull = true) @@ -515,7 +505,7 @@ namespace ServiceLib.Common } /// - /// 取得GUID + /// GUID /// /// public static string GetGuid(bool full = true) diff --git a/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs index aaf7f3e0..98be504d 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/ShadowsocksFmt.cs @@ -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); } diff --git a/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs b/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs index 0d9013b4..f13cccbb 100644 --- a/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs +++ b/v2rayN/ServiceLib/Handler/Fmt/SocksFmt.cs @@ -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]; diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index 6fe4d4d5..29ff8a42 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -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 { Utils.ToInt(item.Port) }; + rule.port = new List { item.Port.ToInt() }; } } if (item.Network.IsNotEmpty()) diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs index 6e4d7c1c..ee08d281 100644 --- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs @@ -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) { diff --git a/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs b/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs index 0d6e7d26..ada0b3c3 100644 --- a/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs @@ -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 }); } diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index 4cf7233b..bf94a230 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -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) { diff --git a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs index de4220d7..351d4424 100644 --- a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs +++ b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs @@ -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 }); }