NameNormalization.h (3371B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <AppInstallerArchitecture.h> 5 6 #include <string> 7 #include <string_view> 8 9 10 namespace AppInstaller::Utility 11 { 12 // The specific version of normalization being used. 13 enum class NormalizationVersion 14 { 15 Initial, 16 InitialPreserveWhiteSpace, 17 }; 18 19 // List of name normalization fields. Architecture, locale, etc. 20 // Currently only architecture is used. 21 enum class NormalizationField : uint32_t 22 { 23 None = 0x0, 24 Architecture = 0x1, 25 }; 26 27 DEFINE_ENUM_FLAG_OPERATORS(NormalizationField); 28 29 struct NameNormalizer; 30 31 // A package publisher and name that has been normalized, allowing direct 32 // comparison across versions and many other facet. Also allows use in 33 // generating and Id for local packages. 34 struct NormalizedName 35 { 36 NormalizedName() = default; 37 38 const std::string& Name() const { return m_name; } 39 void Name(std::string&& name) { m_name = std::move(name); } 40 void Name(std::string_view name) { m_name = name; } 41 42 Utility::Architecture Architecture() const { return m_arch; } 43 void Architecture(Utility::Architecture arch) { m_arch = arch; } 44 45 const std::string& Locale() const { return m_locale; } 46 void Locale(std::string&& locale) { m_locale = std::move(locale); } 47 48 const std::string& Publisher() const { return m_publisher; } 49 void Publisher(std::string&& publisher) { m_publisher = std::move(publisher); } 50 void Publisher(std::string_view publisher) { m_publisher = publisher; } 51 52 // Gets normalized name with additional normalization fields included. 53 std::string GetNormalizedName(NormalizationField fieldsToInclude) const; 54 // Gets a flag indicating the list of fields detected in normalization. 55 NormalizationField GetNormalizedFields() const; 56 57 private: 58 std::string m_name; 59 Utility::Architecture m_arch = Utility::Architecture::Unknown; 60 std::string m_locale; 61 std::string m_publisher; 62 }; 63 64 namespace details 65 { 66 // NameNormalizer interface to allow different versions. 67 struct INameNormalizer 68 { 69 virtual ~INameNormalizer() = default; 70 71 // Normalize both the name and publisher at the same time. 72 virtual NormalizedName Normalize(std::string_view name, std::string_view publisher) const = 0; 73 74 // Normalize only the name. 75 virtual NormalizedName NormalizeName(std::string_view name) const = 0; 76 77 // Normalize only the publisher. 78 virtual std::string NormalizePublisher(std::string_view publisher) const = 0; 79 }; 80 } 81 82 // Helper that manages the lifetime of the internals required to 83 // execute the name normalization. 84 struct NameNormalizer 85 { 86 NameNormalizer(NormalizationVersion version); 87 88 NormalizedName Normalize(std::string_view name, std::string_view publisher) const; 89 NormalizedName NormalizeName(std::string_view name) const; 90 std::string NormalizePublisher(std::string_view publisher) const; 91 92 private: 93 std::unique_ptr<details::INameNormalizer> m_normalizer; 94 }; 95 }