/
EnumUtils.cs
399 lines (341 loc) · 15.2 KB
/
EnumUtils.cs
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Serialization;
#if !HAVE_LINQ
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using System.Reflection;
using System.Text;
namespace Newtonsoft.Json.Utilities
{
internal static class EnumUtils
{
private const char EnumSeparatorChar = ',';
private const string EnumSeparatorString = ", ";
private static readonly ThreadSafeStore<Type, EnumInfo> ValuesAndNamesPerEnum = new ThreadSafeStore<Type, EnumInfo>(InitializeValuesAndNames);
private static EnumInfo InitializeValuesAndNames(Type enumType)
{
string[] names = Enum.GetNames(enumType);
string[] resolvedNames = new string[names.Length];
ulong[] values = new ulong[names.Length];
for (int i = 0; i < names.Length; i++)
{
string name = names[i];
FieldInfo f = enumType.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
values[i] = ToUInt64(f.GetValue(null));
string resolvedName;
#if HAVE_DATA_CONTRACTS
resolvedName = f.GetCustomAttributes(typeof(EnumMemberAttribute), true)
.Cast<EnumMemberAttribute>()
.Select(a => a.Value)
.SingleOrDefault() ?? f.Name;
if (Array.IndexOf(resolvedNames, resolvedName, 0, i) != -1)
{
throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, resolvedName, enumType.Name));
}
#else
resolvedName = name;
#endif
resolvedNames[i] = resolvedName;
}
bool isFlags = enumType.IsDefined(typeof(FlagsAttribute), false);
return new EnumInfo(isFlags, values, names, resolvedNames);
}
public static IList<T> GetFlagsValues<T>(T value) where T : struct
{
Type enumType = typeof(T);
if (!enumType.IsDefined(typeof(FlagsAttribute), false))
{
throw new ArgumentException("Enum type {0} is not a set of flags.".FormatWith(CultureInfo.InvariantCulture, enumType));
}
Type underlyingType = Enum.GetUnderlyingType(value.GetType());
ulong num = ToUInt64(value);
EnumInfo enumNameValues = GetEnumValuesAndNames(enumType);
IList<T> selectedFlagsValues = new List<T>();
for (int i = 0; i < enumNameValues.Values.Length; i++)
{
ulong v = enumNameValues.Values[i];
if ((num & v) == v && v != 0)
{
selectedFlagsValues.Add((T)Convert.ChangeType(v, underlyingType, CultureInfo.CurrentCulture));
}
}
if (selectedFlagsValues.Count == 0 && enumNameValues.Values.Any(v => v == 0))
{
selectedFlagsValues.Add(default(T));
}
return selectedFlagsValues;
}
public static bool TryToString(Type enumType, object value, bool camelCaseText, out string name)
{
EnumInfo enumInfo = ValuesAndNamesPerEnum.Get(enumType);
ulong v = ToUInt64(value);
if (!enumInfo.IsFlags)
{
int index = Array.BinarySearch(enumInfo.Values, v);
if (index >= 0)
{
name = enumInfo.ResolvedNames[index];
if (camelCaseText)
{
name = StringUtils.ToCamelCase(name);
}
return true;
}
// is number value
name = null;
return false;
}
else // These are flags OR'ed together (We treat everything as unsigned types)
{
name = InternalFlagsFormat(enumInfo, v, camelCaseText);
return name != null;
}
}
private static String InternalFlagsFormat(EnumInfo entry, ulong result, bool camelCaseText)
{
string[] resolvedNames = entry.ResolvedNames;
ulong[] values = entry.Values;
int index = values.Length - 1;
StringBuilder sb = new StringBuilder();
bool firstTime = true;
ulong saveResult = result;
// We will not optimize this code further to keep it maintainable. There are some boundary checks that can be applied
// to minimize the comparsions required. This code works the same for the best/worst case. In general the number of
// items in an enum are sufficiently small and not worth the optimization.
while (index >= 0)
{
if (index == 0 && values[index] == 0)
{
break;
}
if ((result & values[index]) == values[index])
{
result -= values[index];
if (!firstTime)
{
sb.Insert(0, EnumSeparatorString);
}
string resolvedName = resolvedNames[index];
sb.Insert(0, camelCaseText ? StringUtils.ToCamelCase(resolvedName) : resolvedName);
firstTime = false;
}
index--;
}
string returnString;
if (result != 0)
{
// We were unable to represent this number as a bitwise or of valid flags
returnString = null; // return null so the caller knows to .ToString() the input
}
else if (saveResult == 0)
{
// For the cases when we have zero
if (values.Length > 0 && values[0] == 0)
{
returnString = resolvedNames[0]; // Zero was one of the enum values.
if (camelCaseText)
{
returnString = StringUtils.ToCamelCase(returnString);
}
}
else
{
returnString = null;
}
}
else
{
returnString = sb.ToString(); // Return the string representation
}
return returnString;
}
public static EnumInfo GetEnumValuesAndNames(Type enumType)
{
return ValuesAndNamesPerEnum.Get(enumType);
}
private static ulong ToUInt64(object value)
{
PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(value.GetType(), out bool _);
switch (typeCode)
{
case PrimitiveTypeCode.SByte:
return (ulong)(sbyte)value;
case PrimitiveTypeCode.Byte:
return (byte)value;
case PrimitiveTypeCode.Boolean:
// direct cast from bool to byte is not allowed
return Convert.ToByte((bool)value);
case PrimitiveTypeCode.Int16:
return (ulong)(short)value;
case PrimitiveTypeCode.UInt16:
return (ushort)value;
case PrimitiveTypeCode.Char:
return (char)value;
case PrimitiveTypeCode.UInt32:
return (uint)value;
case PrimitiveTypeCode.Int32:
return (ulong)(int)value;
case PrimitiveTypeCode.UInt64:
return (ulong)value;
case PrimitiveTypeCode.Int64:
return (ulong)(long)value;
// All unsigned types will be directly cast
default:
throw new InvalidOperationException("Unknown enum type.");
}
}
public static object ParseEnum(Type enumType, string value, bool disallowNumber)
{
ValidationUtils.ArgumentNotNull(enumType, nameof(enumType));
ValidationUtils.ArgumentNotNull(value, nameof(value));
if (!enumType.IsEnum())
{
throw new ArgumentException("Type provided must be an Enum.", nameof(enumType));
}
EnumInfo entry = ValuesAndNamesPerEnum.Get(enumType);
string[] enumNames = entry.Names;
string[] resolvedNames = entry.ResolvedNames;
ulong[] enumValues = entry.Values;
// first check if the entire text (including commas) matches a resolved name
int? matchingIndex = FindIndexByName(resolvedNames, value, 0, value.Length, StringComparison.Ordinal);
if (matchingIndex != null)
{
return Enum.ToObject(enumType, enumValues[matchingIndex.Value]);
}
int firstNonWhitespaceIndex = -1;
for (int i = 0; i < value.Length; i++)
{
if (!char.IsWhiteSpace(value[i]))
{
firstNonWhitespaceIndex = i;
break;
}
}
if (firstNonWhitespaceIndex == -1)
{
throw new ArgumentException("Must specify valid information for parsing in the string.");
}
// check whether string is a number and parse as a number value
char firstNonWhitespaceChar = value[firstNonWhitespaceIndex];
if (char.IsDigit(firstNonWhitespaceChar) || firstNonWhitespaceChar == '-' || firstNonWhitespaceChar == '+')
{
Type underlyingType = Enum.GetUnderlyingType(enumType);
value = value.Trim();
object temp = null;
try
{
temp = Convert.ChangeType(value, underlyingType, CultureInfo.InvariantCulture);
}
catch (FormatException)
{
// We need to Parse this as a String instead. There are cases
// when you tlbimp enums that can have values of the form "3D".
// Don't fix this code.
}
if (temp != null)
{
if (disallowNumber)
{
throw new FormatException("Integer string '{0}' is not allowed.".FormatWith(CultureInfo.InvariantCulture, value));
}
return Enum.ToObject(enumType, temp);
}
}
ulong result = 0;
int valueIndex = firstNonWhitespaceIndex;
while (valueIndex <= value.Length) // '=' is to handle invalid case of an ending comma
{
// Find the next separator, if there is one, otherwise the end of the string.
int endIndex = value.IndexOf(EnumSeparatorChar, valueIndex);
if (endIndex == -1)
{
endIndex = value.Length;
}
// Shift the starting and ending indices to eliminate whitespace
int endIndexNoWhitespace = endIndex;
while (valueIndex < endIndex && char.IsWhiteSpace(value[valueIndex]))
{
valueIndex++;
}
while (endIndexNoWhitespace > valueIndex && char.IsWhiteSpace(value[endIndexNoWhitespace - 1]))
{
endIndexNoWhitespace--;
}
int valueSubstringLength = endIndexNoWhitespace - valueIndex;
// match with case sensitivity
matchingIndex = MatchName(value, enumNames, resolvedNames, valueIndex, valueSubstringLength, StringComparison.Ordinal);
// if no match found, attempt case insensitive search
if (matchingIndex == null)
{
matchingIndex = MatchName(value, enumNames, resolvedNames, valueIndex, valueSubstringLength, StringComparison.OrdinalIgnoreCase);
}
if (matchingIndex == null)
{
// still can't find a match
// before we throw an error, check whether the entire string has a case insensitive match against resolve names
matchingIndex = FindIndexByName(resolvedNames, value, 0, value.Length, StringComparison.OrdinalIgnoreCase);
if (matchingIndex != null)
{
return Enum.ToObject(enumType, enumValues[matchingIndex.Value]);
}
// no match so error
throw new ArgumentException("Requested value '{0}' was not found.".FormatWith(CultureInfo.InvariantCulture, value));
}
result |= enumValues[matchingIndex.Value];
// Move our pointer to the ending index to go again.
valueIndex = endIndex + 1;
}
return Enum.ToObject(enumType, result);
}
private static int? MatchName(string value, string[] enumNames, string[] resolvedNames, int valueIndex, int valueSubstringLength, StringComparison comparison)
{
int? matchingIndex = FindIndexByName(resolvedNames, value, valueIndex, valueSubstringLength, comparison);
if (matchingIndex == null)
{
matchingIndex = FindIndexByName(enumNames, value, valueIndex, valueSubstringLength, comparison);
}
return matchingIndex;
}
private static int? FindIndexByName(string[] enumNames, string value, int valueIndex, int valueSubstringLength, StringComparison comparison)
{
for (int i = 0; i < enumNames.Length; i++)
{
if (enumNames[i].Length == valueSubstringLength &&
string.Compare(enumNames[i], 0, value, valueIndex, valueSubstringLength, comparison) == 0)
{
return i;
}
}
return null;
}
}
}