MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.Generic;5using System.Text;6 7using Dbg = System.Management.Automation;8 9namespace System.Management.Automation10{11 /// <summary>12 /// Performs enum minimum disambiguation.13 /// </summary>14 internal static class EnumMinimumDisambiguation15 {16 #region Constructors17 18 /// <summary>19 /// Initialize the dictionary for special cases of minimum disambiguation.20 /// </summary>21 static EnumMinimumDisambiguation()22 {23 // Add special minimum disambiguation cases here for certain enum types.24 // The current implementation assumes that special names in each type can be25 // differentiated by their first letter.26 s_specialDisambiguateCases.Add(27 typeof(System.IO.FileAttributes),28 new string[] { "Directory", "ReadOnly", "System" });29 }30 31 #endregion32 33 /// <summary>34 /// Perform disambiguation on enum names.35 /// </summary>36 /// <returns>Complete enum name after disambiguation.</returns>37 internal static string EnumDisambiguate(string text, Type enumType)38 {39 // Get all enum names in the given enum type40 string[] enumNames = Enum.GetNames(enumType);41 42 // Get all names that matches the given prefix.43 List<string> namesWithMatchingPrefix = new List<string>();44 foreach (string name in enumNames)45 {46 if (name.StartsWith(text, StringComparison.OrdinalIgnoreCase))47 {48 namesWithMatchingPrefix.Add(name);49 }50 }51 52 // Throw error when no match is found.53 if (namesWithMatchingPrefix.Count == 0)54 {55 throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException),56 null, "NoEnumNameMatch", EnumExpressionEvaluatorStrings.NoEnumNameMatch, text, EnumAllValues(enumType));57 }58 // Return the result if there is only one match.59 else if (namesWithMatchingPrefix.Count == 1)60 {61 return namesWithMatchingPrefix[0];62 }63 // multiple matches situation64 else65 {66 // test for exact match67 foreach (string matchName in namesWithMatchingPrefix)68 {69 if (matchName.Equals(text, StringComparison.OrdinalIgnoreCase))70 {71 return matchName;72 }73 }74 // test for special cases match75 string[] minDisambiguateNames;76 if (s_specialDisambiguateCases.TryGetValue(enumType, out minDisambiguateNames))77 {78 foreach (string tName in minDisambiguateNames)79 {80 if (tName.StartsWith(text, StringComparison.OrdinalIgnoreCase))81 {82 return tName;83 }84 }85 }86 // No special cases match, throw error for multiple matches.87 StringBuilder matchListSB = new StringBuilder(namesWithMatchingPrefix[0]);88 const string separator = ", ";89 for (int i = 1; i < namesWithMatchingPrefix.Count; i++)90 {91 matchListSB.Append(separator);92 matchListSB.Append(namesWithMatchingPrefix[i]);93 }94 95 throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException),96 null, "MultipleEnumNameMatch", EnumExpressionEvaluatorStrings.MultipleEnumNameMatch,97 text, matchListSB.ToString());98 }99 }100 101 /// <summary>102 /// Produces a string that contains all the enumerator names in an enum type.103 /// </summary>104 /// <param name="enumType"></param>105 /// <returns></returns>106 internal static string EnumAllValues(Type enumType)107 {108 string[] names = Enum.GetNames(enumType);109 const string separator = ", ";110 StringBuilder returnValue = new StringBuilder();111 if (names.Length != 0)112 {113 for (int i = 0; i < names.Length; i++)114 {115 returnValue.Append(names[i]);116 returnValue.Append(separator);117 }118 119 returnValue.Remove(returnValue.Length - separator.Length, separator.Length);120 }121 122 return returnValue.ToString();123 }124 125 private static readonly Dictionary<Type, string[]> s_specialDisambiguateCases = new Dictionary<Type, string[]>();126 }127}128 