From 492ef81f7c5e6dce05bdc29d0483901af5092438 Mon Sep 17 00:00:00 2001 From: "SONGE9B3\\song_pd_win11" Date: Mon, 2 Jan 2023 17:25:35 +0800 Subject: [PATCH] perf: extraction of public methods --- STranslate/Util/Util.cs | 61 +++++++++++++++++++++++++++++++ STranslate/ViewModel/MainVM.cs | 67 +++------------------------------- 2 files changed, 66 insertions(+), 62 deletions(-) diff --git a/STranslate/Util/Util.cs b/STranslate/Util/Util.cs index a3aa3d5..0b8b8d6 100644 --- a/STranslate/Util/Util.cs +++ b/STranslate/Util/Util.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Net.Http; using System.Security.Cryptography; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; @@ -175,5 +176,65 @@ namespace STranslate.Util } } #endregion + + #region GenString + /// + /// 构造蛇形结果 + /// + /// + /// + public static string GenSnakeString(List req) + { + var ret = string.Empty; + + req.ForEach(x => + { + ret += "_" + x.ToLower(); + }); + return ret.Substring(1); + } + + /// + /// 构造驼峰结果 + /// + /// + /// 是否为小驼峰 + /// + public static string GenHumpString(List req, bool isSmallHump) + { + string ret = string.Empty; + var array = req.ToArray(); + for (var j = 0; j < array.Length; j++) + { + char[] chars = array[j].ToCharArray(); + if (j == 0 && isSmallHump) chars[0] = char.ToLower(chars[0]); + else chars[0] = char.ToUpper(chars[0]); + for (int i = 1; i < chars.Length; i++) + { + chars[i] = char.ToLower(chars[i]); + } + ret += new string(chars); + } + return ret; + + } + /// + /// 提取英文 + /// + /// + /// + public static string ExtractEngString(string str) + { + Regex regex = new Regex("[a-zA-Z]+"); + + MatchCollection mMactchCol = regex.Matches(str); + string strA_Z = string.Empty; + foreach (Match mMatch in mMactchCol) + { + strA_Z += mMatch.Value; + } + return strA_Z; + } + #endregion } } diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 1cc1733..4adde03 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -10,6 +10,7 @@ using System.Windows.Media.Imaging; using System.Windows.Media; using System.Text.RegularExpressions; using System.Security.Cryptography; +using STranslate.Util; namespace STranslate.ViewModel { @@ -88,7 +89,7 @@ namespace STranslate.ViewModel string.Empty); //2. 取出上一步中所有英文字符 - var engStr = GetSubString(text); + var engStr = Util.Util.ExtractEngString(text); var ratio = (double)engStr.Length / text.Length; @@ -167,9 +168,9 @@ namespace STranslate.ViewModel var splitList = OutputTxt.Split(' ').ToList(); if (splitList.Count > 1) { - SnakeRet = GenSnakeString(splitList); - SmallHumpRet = GenHumpString(splitList, true); //小驼峰 - LargeHumpRet = GenHumpString(splitList, false); //大驼峰 + SnakeRet = Util.Util.GenSnakeString(splitList); + SmallHumpRet = Util.Util.GenHumpString(splitList, true); //小驼峰 + LargeHumpRet = Util.Util.GenHumpString(splitList, false); //大驼峰 } //System.Diagnostics.Debug.Print(SnakeRet + "\n" + SmallHumpRet + "\n" + LargeHumpRet); } @@ -178,64 +179,6 @@ namespace STranslate.ViewModel OutputTxt = ex.Message; } } - /// - /// 构造蛇形结果 - /// - /// - /// - private string GenSnakeString(List req) - { - //TODO: 构造时间过长 - var ret = string.Empty; - - req.ForEach(x => - { - ret += "_" + x.ToLower(); - }); - return ret.Substring(1); - } - - /// - /// 构造驼峰结果 - /// - /// - /// 是否为小驼峰 - /// - private string GenHumpString(List req, bool isSmallHump) - { - string ret = string.Empty; - var array = req.ToArray(); - for (var j = 0; j < array.Length; j++) - { - char[] chars = array[j].ToCharArray(); - if (j == 0 && isSmallHump) chars[0] = char.ToLower(chars[0]); - else chars[0] = char.ToUpper(chars[0]); - for (int i = 1; i < chars.Length; i++) - { - chars[i] = char.ToLower(chars[i]); - } - ret += new string(chars); - } - return ret; - - } - /// - /// 提取英文 - /// - /// - /// - public string GetSubString(string str) - { - Regex regex = new Regex("[a-zA-Z]+"); - - MatchCollection mMactchCol = regex.Matches(str); - string strA_Z = string.Empty; - foreach (Match mMatch in mMactchCol) - { - strA_Z += mMatch.Value; - } - return strA_Z; - } #endregion handle #region Params