use a comma as the dynamic enum delimiter to be in line with regular enums

This commit is contained in:
Ell 2024-08-12 22:53:25 +02:00
parent 1fe7725ca9
commit 23e94fa6f5
4 changed files with 9 additions and 8 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ obj
packages packages
*.nupkg *.nupkg
TestResults TestResults
*.DotSettings.user

View file

@ -110,7 +110,7 @@ namespace DynamicEnums {
var otherValue = DynamicEnum.GetValue(other); var otherValue = DynamicEnum.GetValue(other);
if (otherValue != 0) { if (otherValue != 0) {
if (included.Length > 0) if (included.Length > 0)
included.Append(" | "); included.Append(", ");
included.Append(other); included.Append(other);
remain &= ~otherValue; remain &= ~otherValue;
} }
@ -426,7 +426,7 @@ namespace DynamicEnums {
var cache = DynamicEnum.GetStorage(type).ParseCache; var cache = DynamicEnum.GetStorage(type).ParseCache;
if (!cache.TryGetValue(strg, out var cached)) { if (!cache.TryGetValue(strg, out var cached)) {
BigInteger? accum = null; BigInteger? accum = null;
foreach (var val in strg.Split('|')) { foreach (var val in strg.Split(',')) {
foreach (var defined in DynamicEnum.GetValues(type)) { foreach (var defined in DynamicEnum.GetValues(type)) {
if (defined.name == val.Trim()) { if (defined.name == val.Trim()) {
accum = (accum ?? 0) | DynamicEnum.GetValue(defined); accum = (accum ?? 0) | DynamicEnum.GetValue(defined);

View file

@ -48,11 +48,11 @@ bool hasAll = allFlags1.HasAllFlags(MyEnum.FlagOne | MyEnum.ValueOne); // false
// displaying a dynamic enum value or flag // displaying a dynamic enum value or flag
Console.WriteLine(MyEnum.FlagOne); // "FlagOne" Console.WriteLine(MyEnum.FlagOne); // "FlagOne"
Console.WriteLine(allFlags1); // "FlagOne | FlagTwo | FlagThree" Console.WriteLine(allFlags1); // "FlagOne, FlagTwo, FlagThree"
// parsing a dynamic enum value // parsing a dynamic enum value
MyEnum parsed1 = DynamicEnum.Parse<MyEnum>("FlagOne"); MyEnum parsed1 = DynamicEnum.Parse<MyEnum>("FlagOne");
MyEnum parsed2 = DynamicEnum.Parse<MyEnum>("FlagOne | FlagThree"); MyEnum parsed2 = DynamicEnum.Parse<MyEnum>("FlagOne, FlagThree");
``` ```
You can also check out [the tests](https://github.com/Ellpeck/DynamicEnums/tree/main/Tests) for some more complex examples. You can also check out [the tests](https://github.com/Ellpeck/DynamicEnums/tree/main/Tests) for some more complex examples.

View file

@ -74,13 +74,13 @@ public class EnumTests {
Assert.AreEqual(flags[24].HasAnyFlags(DynamicEnum.GetEnumValue<TestDynamicEnum>(0)), false); Assert.AreEqual(flags[24].HasAnyFlags(DynamicEnum.GetEnumValue<TestDynamicEnum>(0)), false);
Assert.AreEqual(DynamicEnum.Parse<TestDynamicEnum>("Flag24"), flags[24]); Assert.AreEqual(DynamicEnum.Parse<TestDynamicEnum>("Flag24"), flags[24]);
Assert.AreEqual(DynamicEnum.Parse<TestDynamicEnum>("Flag24 | Flag43"), DynamicEnum.Or(flags[24], flags[43])); Assert.AreEqual(DynamicEnum.Parse<TestDynamicEnum>("Flag24, Flag43"), DynamicEnum.Or(flags[24], flags[43]));
Assert.AreEqual(flags[24].ToString(), "Flag24"); Assert.AreEqual(flags[24].ToString(), "Flag24");
Assert.AreEqual(zero.ToString(), "Zero"); Assert.AreEqual(zero.ToString(), "Zero");
Assert.AreEqual(DynamicEnum.GetEnumValue<TestDynamicEnum>(0).ToString(), "Zero"); Assert.AreEqual(DynamicEnum.GetEnumValue<TestDynamicEnum>(0).ToString(), "Zero");
Assert.AreEqual(DynamicEnum.Or(flags[24], flags[43]).ToString(), "Flag24 | Flag43"); Assert.AreEqual(DynamicEnum.Or(flags[24], flags[43]).ToString(), "Flag24, Flag43");
Assert.AreEqual(DynamicEnum.Or(flags[24], DynamicEnum.GetEnumValue<TestDynamicEnum>(new BigInteger(1) << 99)).ToString(), "Flag24 | Flag99"); Assert.AreEqual(DynamicEnum.Or(flags[24], DynamicEnum.GetEnumValue<TestDynamicEnum>(new BigInteger(1) << 99)).ToString(), "Flag24, Flag99");
Assert.AreEqual(DynamicEnum.Or(flags[24], DynamicEnum.GetEnumValue<TestDynamicEnum>(new BigInteger(1) << 101)).ToString(), (DynamicEnum.GetValue(flags[24]) | new BigInteger(1) << 101).ToString()); Assert.AreEqual(DynamicEnum.Or(flags[24], DynamicEnum.GetEnumValue<TestDynamicEnum>(new BigInteger(1) << 101)).ToString(), (DynamicEnum.GetValue(flags[24]) | new BigInteger(1) << 101).ToString());
Assert.True(DynamicEnum.IsDefined(flags[27])); Assert.True(DynamicEnum.IsDefined(flags[27]));