From 45742005cade9356f34a5fe7e07be7192bdfc2bd Mon Sep 17 00:00:00 2001 From: Li Xinrui Date: Mon, 30 Aug 2021 15:09:05 +0800 Subject: [PATCH] Speed up app identification greatly by using compiled regex and substring. --- src/HandyWinget/Common/Helper.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/HandyWinget/Common/Helper.cs b/src/HandyWinget/Common/Helper.cs index 3056431..334d4da 100644 --- a/src/HandyWinget/Common/Helper.cs +++ b/src/HandyWinget/Common/Helper.cs @@ -1,4 +1,6 @@ using System; +using System.Text; +using System.Globalization; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; @@ -329,10 +331,16 @@ public static IEnumerable GetInstalledAppList() /// installed app line details, extracted from winget list command /// /// + private static Regex re = new Regex("[ ]{2,}", RegexOptions.Compiled | RegexOptions.IgnoreCase); public static (string packageId, string version, string availableVersion) ParseInstalledApp(string line, string packageId) { - line = Regex.Replace(line, "[ ]{2,}", " ", RegexOptions.IgnoreCase); // remove more than 2 spaces - line = Regex.Replace(line, $@".*(?=({Regex.Escape(packageId)}))", "", RegexOptions.IgnoreCase); // remove everythings before package id + line = re.Replace(line, " "); + int startIndex = line.IndexOf(packageId); + if(startIndex < 0) + { + return (packageId: null, version: null, availableVersion: null); + } + line = line.Substring(startIndex); var lines = line.Split(" "); if (lines.Count() >= 3) // available version exist {