From 70d3d5f16ea0501fb7b7b4893a63855d595d7ea7 Mon Sep 17 00:00:00 2001 From: Vaibhav Sharma <48472541+GhostVaibhav@users.noreply.github.com> Date: Mon, 22 Jul 2024 19:44:10 +0530 Subject: [PATCH] [PTRun][Registry]Allow interchangeable use of / instead of \ (#33309) ## Summary of the Pull Request As the title suggests, the PR adds the feature of using / instead of \ in the Registry plugin of PT Run. --- .../Helper/QueryHelperTest.cs | 12 +++++++++++ .../Helper/QueryHelper.cs | 21 ++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry.UnitTest/Helper/QueryHelperTest.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry.UnitTest/Helper/QueryHelperTest.cs index d6b31f6829..585cb3a5bd 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry.UnitTest/Helper/QueryHelperTest.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry.UnitTest/Helper/QueryHelperTest.cs @@ -17,6 +17,18 @@ namespace Microsoft.PowerToys.Run.Plugin.Registry.UnitTest.Helper [DataRow(@"HKLM\\Test", true, @"HKLM", "Test")] [DataRow(@"HKLM\Test\\TestTest", true, @"HKLM\Test", "TestTest")] [DataRow(@"HKLM\Test\\\TestTest", true, @"HKLM\Test", @"\TestTest")] + [DataRow("HKLM/\"Software\"/", false, @"HKLM\Software\", "")] + [DataRow("HKLM/\"Software\"//test", true, @"HKLM\Software", "test")] + [DataRow("HKLM/\"Software\"//test/123", true, @"HKLM\Software", "test/123")] + [DataRow("HKLM/\"Software\"//test\\123", true, @"HKLM\Software", @"test\123")] + [DataRow("HKLM/\"Software\"/test", false, @"HKLM\Software\test", "")] + [DataRow("HKLM\\Software\\\"test\"", false, @"HKLM\Software\test", "")] + [DataRow("HKLM\\\"Software\"\\\"test\"", false, @"HKLM\Software\test", "")] + [DataRow("HKLM\\\"Software\"\\\"test/software\"", false, @"HKLM\Software\test/software", "")] + [DataRow("HKLM\\\"Software\"/\"test\"\\hello", false, @"HKLM\Software\test\hello", "")] + [DataRow("HKLM\\\"Software\"\\\"test\"\\hello\\\\\"some/value\"", true, @"HKLM\Software\test\hello", "some/value")] + [DataRow("HKLM\\\"Software\"\\\"test\"/hello\\\\\"some/value\"", true, @"HKLM\Software\test\hello", "some/value")] + [DataRow("HKLM\\\"Software\"\\\"test\"\\hello\\\\some\\value", true, @"HKLM\Software\test\hello", @"some\value")] public void GetQueryPartsTest(string query, bool expectedHasValueName, string expectedQueryKey, string expectedQueryValueName) { var hasValueName = QueryHelper.GetQueryParts(query, out var queryKey, out var queryValueName); diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry/Helper/QueryHelper.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry/Helper/QueryHelper.cs index 8b8371ff11..ed8403a3f4 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry/Helper/QueryHelper.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry/Helper/QueryHelper.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; using Microsoft.PowerToys.Run.Plugin.Registry.Constants; namespace Microsoft.PowerToys.Run.Plugin.Registry.Helper @@ -32,6 +33,18 @@ namespace Microsoft.PowerToys.Run.Plugin.Registry.Helper { Win32.Registry.Users.Name, KeyName.UsersShort }, }; + /// + /// Sanitize the query to avoid issues with the regex + /// + /// Query containing front-slash + /// A string replacing all the front-slashes with back-slashes + private static string SanitizeQuery(in string query) + { + var sanitizedQuery = Regex.Replace(query, @"/(?<=^(?:[^""]*""[^""]*"")*[^""]*)(? /// Return the parts of a given query /// @@ -41,14 +54,16 @@ namespace Microsoft.PowerToys.Run.Plugin.Registry.Helper /// when the query search for a key and a value name, otherwise internal static bool GetQueryParts(in string query, out string queryKey, out string queryValueName) { - if (!query.Contains(QuerySplitCharacter, StringComparison.InvariantCultureIgnoreCase)) + var sanitizedQuery = SanitizeQuery(query); + + if (!sanitizedQuery.Contains(QuerySplitCharacter, StringComparison.InvariantCultureIgnoreCase)) { - queryKey = query; + queryKey = sanitizedQuery; queryValueName = string.Empty; return false; } - var querySplit = query.Split(QuerySplitCharacter); + var querySplit = sanitizedQuery.Split(QuerySplitCharacter); queryKey = querySplit.First(); queryValueName = querySplit.Last();