[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
2 changed files with 30 additions and 3 deletions

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