Files
PowerToys/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Registry/Helper/QueryHelper.cs
Mykhailo Pylyp c651a4b36e [PowerToys Run] Update to net5 (#12434)
* [Setup] Add support for installing both dotnet 3 and 5 (#12306)

* [PowerToys Run] Update to net5 (#12286)

* Change targets of projects

* Update Microsoft.Toolkit.Uwp.Notifications,

changed TargetFramework for PowerLauncher project in order to resolve an issue with ModernWpf

* Specify windows version in order to fix build errors

* Fixed suppressed warnings

* Updated sdk

* Removed usage of obsolete GlobalAssemblyCache

* Removed obsolete DesktopNotificationManagerCompat

* Update nuget versions

* Update installer

* [PowerToys Run] Obsolete APIs and warnings introduced in .net5 (#12423)

* Change targets of projects

* Update Microsoft.Toolkit.Uwp.Notifications,

changed TargetFramework for PowerLauncher project in order to resolve an issue with ModernWpf

* Fixed suppressed warnings

* Removed obsolete DesktopNotificationManagerCompat

* Get rid of binary formatter

* Update tests

* Don't include new image cache file to the report

* There's no need to call IsOwner as it doesn't make sense

* Fix different nullability exception

* Exclude extra dlls from tests

Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
2021-07-21 19:02:45 +03:00

99 lines
3.9 KiB
C#

// 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
{
/// <summary>
/// Helper class to easier work with queries
/// </summary>
internal static class QueryHelper
{
/// <summary>
/// The character to distinguish if the search query contain multiple parts (typically "\\")
/// </summary>
internal const string QuerySplitCharacter = "\\\\";
/// <summary>
/// A list that contain short names of all registry base keys
/// </summary>
private static readonly IReadOnlyDictionary<string, string> _shortBaseKeys = new Dictionary<string, string>(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 },
};
/// <summary>
/// Return the parts of a given query
/// </summary>
/// <param name="query">The query that could contain parts</param>
/// <param name="queryKey">The key part of the query</param>
/// <param name="queryValueName">The value name part of the query</param>
/// <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))
{
queryKey = query;
queryValueName = string.Empty;
return false;
}
var querySplit = query.Split(QuerySplitCharacter);
queryKey = querySplit.FirstOrDefault() ?? string.Empty;
queryValueName = querySplit.LastOrDefault() ?? string.Empty;
return true;
}
/// <summary>
/// Return a registry key with a long base key
/// </summary>
/// <param name="registryKey">A registry key with a short base key</param>
/// <returns>A registry key with a long base key</returns>
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;
}
/// <summary>
/// Return a registry key with a short base key (useful to reduce the text length of a registry key)
/// </summary>
/// <param name="registryKey">A registry key with a full base key</param>
/// <returns>A registry key with a short base key</returns>
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;
}
}
}