Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PT-Run] Allow interchangeable use of / instead of \ #33309

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public sealed class QueryHelperTest
[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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -32,6 +33,18 @@ internal static class QueryHelper
{ 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>
Expand All @@ -41,14 +54,16 @@ internal static class QueryHelper
/// <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();
Expand Down
Loading