// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Collections.Generic; using System.Linq; using Microsoft.PowerToys.Run.Plugin.Registry.Constants; namespace Microsoft.PowerToys.Run.Plugin.Registry.Helper { /// /// Helper class to easier work with queries /// internal static class QueryHelper { /// /// The character to distinguish if the search query contain multiple parts (typically "\\") /// internal const string QuerySplitCharacter = "\\\\"; /// /// A list that contain short names of all registry base keys /// private static readonly IReadOnlyDictionary _shortBaseKeys = new Dictionary(6) { { Win32.Registry.ClassesRoot.Name, KeyName.ClassRootShort }, { Win32.Registry.CurrentConfig.Name, KeyName.CurrentConfigShort }, { Win32.Registry.CurrentUser.Name, KeyName.CurrentUserShort }, { Win32.Registry.LocalMachine.Name, KeyName.LocalMachineShort }, { Win32.Registry.PerformanceData.Name, KeyName.PerformanceDataShort }, { Win32.Registry.Users.Name, KeyName.UsersShort }, }; /// /// Return the parts of a given query /// /// The query that could contain parts /// The key part of the query /// The value name part of the query /// 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)) { queryKey = query; queryValueName = string.Empty; return false; } var querySplit = query.Split(QuerySplitCharacter); queryKey = querySplit.FirstOrDefault() ?? string.Empty; queryValueName = querySplit.LastOrDefault() ?? string.Empty; return true; } /// /// Return a registry key with a long base key /// /// A registry key with a short base key /// A registry key with a long base key internal static string GetKeyWithLongBaseKey(in string registryKey) { foreach (var shortName in _shortBaseKeys) { if (!registryKey.StartsWith(shortName.Value, StringComparison.InvariantCultureIgnoreCase)) { continue; } return registryKey.Replace(shortName.Value, shortName.Key, StringComparison.InvariantCultureIgnoreCase); } return registryKey; } /// /// Return a registry key with a short base key (useful to reduce the text length of a registry key) /// /// A registry key with a full base key /// A registry key with a short base key internal static string GetKeyWithShortBaseKey(in string registryKey) { foreach (var shortName in _shortBaseKeys) { if (!registryKey.StartsWith(shortName.Key, StringComparison.InvariantCultureIgnoreCase)) { continue; } return registryKey.Replace(shortName.Key, shortName.Value, StringComparison.InvariantCultureIgnoreCase); } return registryKey; } } }