[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.
This commit is contained in:
Vaibhav Sharma 2024-07-22 19:44:10 +05:30 committed by GitHub
parent af6916a538
commit 70d3d5f16e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 3 deletions

View File

@ -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);

View File

@ -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 },
};
/// <summary>
/// Sanitize the query to avoid issues with the regex
/// </summary>
/// <param name="query">Query containing front-slash</param>
/// <returns>A string replacing all the front-slashes with back-slashes</returns>
private static string SanitizeQuery(in string query)
{
var sanitizedQuery = Regex.Replace(query, @"/(?<=^(?:[^""]*""[^""]*"")*[^""]*)(?<!//.+)", "\\");
return sanitizedQuery.Replace("\"", string.Empty);
}
/// <summary>
/// Return the parts of a given query
/// </summary>
@ -41,14 +54,16 @@ namespace Microsoft.PowerToys.Run.Plugin.Registry.Helper
/// <returns><see langword="true"/> when the query search for a key and a value name, otherwise <see langword="false"/></returns>
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();