forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumMinimumDisambiguation.cs
More file actions
124 lines (115 loc) · 4.88 KB
/
Copy pathEnumMinimumDisambiguation.cs
File metadata and controls
124 lines (115 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System.Collections.Generic;
using System.Text;
using Dbg = System.Management.Automation;
namespace System.Management.Automation
{
/// <summary>
/// Performs enum minimum disambiguation.
/// </summary>
internal static class EnumMinimumDisambiguation
{
#region Constructors
/// <summary>
/// Initialize the dictionary for special cases of minimum disambiguation.
/// </summary>
static EnumMinimumDisambiguation()
{
// Add special minimum disambiguation cases here for certain enum types.
// The current implementation assumes that special names in each type can be
// differentiated by their first letter.
s_specialDisambiguateCases.Add(
typeof(System.IO.FileAttributes),
new string[] { "Directory", "ReadOnly", "System" });
}
#endregion
/// <summary>
/// Perform disambiguation on enum names
/// </summary>
/// <returns>complete enum name after disambiguation</returns>
internal static string EnumDisambiguate(string text, Type enumType)
{
// Get all enum names in the given enum type
string[] enumNames = Enum.GetNames(enumType);
// Get all names that matches the given prefix.
List<string> namesWithMatchingPrefix = new List<string>();
foreach (string name in enumNames)
{
if (name.StartsWith(text, StringComparison.OrdinalIgnoreCase))
{
namesWithMatchingPrefix.Add(name);
}
}
// Throw error when no match is found.
if (namesWithMatchingPrefix.Count == 0)
{
throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException),
null, "NoEnumNameMatch", EnumExpressionEvaluatorStrings.NoEnumNameMatch, text, EnumAllValues(enumType));
}
// Return the result if there is only one match.
else if (namesWithMatchingPrefix.Count == 1)
{
return namesWithMatchingPrefix[0];
}
// multiple matches situation
else
{
// test for exact match
foreach (string matchName in namesWithMatchingPrefix)
{
if (matchName.Equals(text, StringComparison.OrdinalIgnoreCase))
{
return matchName;
}
}
// test for special cases match
string[] minDisambiguateNames;
if (s_specialDisambiguateCases.TryGetValue(enumType, out minDisambiguateNames))
{
foreach (string tName in minDisambiguateNames)
{
if (tName.StartsWith(text, StringComparison.OrdinalIgnoreCase))
{
return tName;
}
}//foreach
}//if
// No special cases match, throw error for multiple matches.
StringBuilder matchListSB = new StringBuilder(namesWithMatchingPrefix[0]);
string separator = ", ";
for (int i = 1; i < namesWithMatchingPrefix.Count; i++)
{
matchListSB.Append(separator);
matchListSB.Append(namesWithMatchingPrefix[i]);
}
throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException),
null, "MultipleEnumNameMatch", EnumExpressionEvaluatorStrings.MultipleEnumNameMatch,
text, matchListSB.ToString());
}
}
/// <summary>
/// Produces a string that contains all the enumerator names in an enum type.
/// </summary>
/// <param name="enumType"></param>
/// <returns></returns>
internal static string EnumAllValues(Type enumType)
{
string[] names = Enum.GetNames(enumType);
string separator = ", ";
StringBuilder returnValue = new StringBuilder();
if (names.Length != 0)
{
for (int i = 0; i < names.Length; i++)
{
returnValue.Append(names[i]);
returnValue.Append(separator);
}
returnValue.Remove(returnValue.Length - separator.Length, separator.Length);
}
return returnValue.ToString();
}
private static Dictionary<Type, string[]> s_specialDisambiguateCases = new Dictionary<Type, string[]>();
}
}