Browse Source

Fix #1745: Empty string is missing from switch result

pull/1754/head
Siegfried Pammer 6 years ago
parent
commit
cc3a0bef7e
  1. 20
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs
  2. 5
      ICSharpCode.Decompiler/IL/Instructions/StringToInt.cs
  3. 8
      ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

20
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs

@ -1193,6 +1193,26 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -1193,6 +1193,26 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
public static void Issue1745(string aaa)
{
switch (aaa) {
case "a":
case "b":
case "c":
case "d":
case "e":
case "f":
Console.WriteLine(aaa);
break;
case null:
Console.WriteLine("<null>");
break;
case "":
Console.WriteLine("<empty>");
break;
}
}
public static bool DoNotRemoveAssignmentBeforeSwitch(string x, out ConsoleKey key)
{
key = (ConsoleKey)0;

5
ICSharpCode.Decompiler/IL/Instructions/StringToInt.cs

@ -55,7 +55,10 @@ namespace ICSharpCode.Decompiler.IL @@ -55,7 +55,10 @@ namespace ICSharpCode.Decompiler.IL
int i = 0;
foreach (var entry in Map) {
if (i > 0) output.Write(", ");
output.Write($"[\"{entry.Key}\"] = {entry.Value}");
if (entry.Key is null)
output.Write($"[null] = {entry.Value}");
else
output.Write($"[\"{entry.Key}\"] = {entry.Value}");
i++;
}
output.Write(" })");

8
ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

@ -449,7 +449,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -449,7 +449,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return false;
if (!tryGetValueBlock.Instructions[0].MatchIfInstruction(out condition, out var defaultBlockJump))
return false;
if (!defaultBlockJump.MatchBranch(out var defaultBlock) && !defaultBlockJump.MatchLeave(leaveContainer))
if (!defaultBlockJump.MatchBranch(out var defaultBlock) && !((leaveContainer != null && defaultBlockJump.MatchLeave(leaveContainer)) || defaultBlockJump.MatchLeave(out _)))
return false;
if (!(condition.MatchLogicNot(out var arg) && arg is CallInstruction c && c.Method.Name == "TryGetValue" &&
MatchDictionaryFieldLoad(c.Arguments[0], IsStringToIntDictionary, out var dictField2, out _) && dictField2.Equals(dictField)))
@ -557,9 +557,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -557,9 +557,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return true;
}
bool AddNullSection(List<SwitchSection> sections, List<(string, int)> stringValues, Block nullValueCaseBlock)
bool AddNullSection(List<SwitchSection> sections, List<(string Value, int Index)> stringValues, Block nullValueCaseBlock)
{
var label = new LongSet(sections.Count);
var label = new LongSet(stringValues.Max(item => item.Index) + 1);
var possibleConflicts = sections.Where(sec => sec.Labels.Overlaps(label)).ToArray();
if (possibleConflicts.Length > 1)
return false;
@ -984,7 +984,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -984,7 +984,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return false;
if (!MatchStringLengthCall(lengthCheckCondition, switchValueVar))
return false;
if (!(exitBranch.MatchBranch(out defaultOrExitBlock) && exitBranch2.MatchBranch(defaultOrExitBlock)))
if (!exitBranch.Match(exitBranch2).Success)
return false;
if (bodyBranch.MatchLeave(out _)) {
bodyOrLeave = bodyBranch;

Loading…
Cancel
Save