Skip to content

Commit

Permalink
Use switch expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Mar 18, 2020
1 parent 8079861 commit ce7a762
Show file tree
Hide file tree
Showing 17 changed files with 716 additions and 931 deletions.
40 changes: 8 additions & 32 deletions src/DotNet/AssemblyHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,14 @@ namespace dnlib.DotNet {
/// <remarks>If <paramref name="hashAlgo"/> is an unsupported hash algorithm, then
/// <see cref="AssemblyHashAlgorithm.SHA1"/> will be used as the hash algorithm.</remarks>
/// <param name="hashAlgo">The algorithm to use</param>
public AssemblyHash(AssemblyHashAlgorithm hashAlgo) {
switch (hashAlgo) {
case AssemblyHashAlgorithm.MD5:
hasher = MD5.Create();
break;

case AssemblyHashAlgorithm.None:
case AssemblyHashAlgorithm.MD2:
case AssemblyHashAlgorithm.MD4:
case AssemblyHashAlgorithm.SHA1:
case AssemblyHashAlgorithm.MAC:
case AssemblyHashAlgorithm.SSL3_SHAMD5:
case AssemblyHashAlgorithm.HMAC:
case AssemblyHashAlgorithm.TLS1PRF:
case AssemblyHashAlgorithm.HASH_REPLACE_OWF:
default:
hasher = SHA1.Create();
break;

case AssemblyHashAlgorithm.SHA_256:
hasher = SHA256.Create();
break;

case AssemblyHashAlgorithm.SHA_384:
hasher = SHA384.Create();
break;

case AssemblyHashAlgorithm.SHA_512:
hasher = SHA512.Create();
break;
}
}
public AssemblyHash(AssemblyHashAlgorithm hashAlgo) =>
hasher = hashAlgo switch {
AssemblyHashAlgorithm.MD5 => MD5.Create(),
AssemblyHashAlgorithm.SHA_256 => SHA256.Create(),
AssemblyHashAlgorithm.SHA_384 => SHA384.Create(),
AssemblyHashAlgorithm.SHA_512 => SHA512.Create(),
_ => SHA1.Create(),
};

/// <inheritdoc/>
public void Dispose() {
Expand Down
33 changes: 16 additions & 17 deletions src/DotNet/AssemblyHashAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,21 @@ public enum AssemblyHashAlgorithm : uint {
}

public static partial class Extensions {
internal static string GetName(this AssemblyHashAlgorithm hashAlg) {
switch (hashAlg) {
case AssemblyHashAlgorithm.MD2: return null;
case AssemblyHashAlgorithm.MD4: return null;
case AssemblyHashAlgorithm.MD5: return "MD5";
case AssemblyHashAlgorithm.SHA1: return "SHA1";
case AssemblyHashAlgorithm.MAC: return null;
case AssemblyHashAlgorithm.SSL3_SHAMD5: return null;
case AssemblyHashAlgorithm.HMAC: return null;
case AssemblyHashAlgorithm.TLS1PRF: return null;
case AssemblyHashAlgorithm.HASH_REPLACE_OWF: return null;
case AssemblyHashAlgorithm.SHA_256: return "SHA256";
case AssemblyHashAlgorithm.SHA_384: return "SHA384";
case AssemblyHashAlgorithm.SHA_512: return "SHA512";
default: return null;
}
}
internal static string GetName(this AssemblyHashAlgorithm hashAlg) =>
hashAlg switch {
AssemblyHashAlgorithm.MD2 => null,
AssemblyHashAlgorithm.MD4 => null,
AssemblyHashAlgorithm.MD5 => "MD5",
AssemblyHashAlgorithm.SHA1 => "SHA1",
AssemblyHashAlgorithm.MAC => null,
AssemblyHashAlgorithm.SSL3_SHAMD5 => null,
AssemblyHashAlgorithm.HMAC => null,
AssemblyHashAlgorithm.TLS1PRF => null,
AssemblyHashAlgorithm.HASH_REPLACE_OWF => null,
AssemblyHashAlgorithm.SHA_256 => "SHA256",
AssemblyHashAlgorithm.SHA_384 => "SHA384",
AssemblyHashAlgorithm.SHA_512 => "SHA512",
_ => null,
};
}
}
32 changes: 16 additions & 16 deletions src/DotNet/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ public ConstantUser(object value, ElementType type) {
static ElementType GetElementType(object value) {
if (value is null)
return ElementType.Class;
switch (System.Type.GetTypeCode(value.GetType())) {
case TypeCode.Boolean: return ElementType.Boolean;
case TypeCode.Char: return ElementType.Char;
case TypeCode.SByte: return ElementType.I1;
case TypeCode.Byte: return ElementType.U1;
case TypeCode.Int16: return ElementType.I2;
case TypeCode.UInt16: return ElementType.U2;
case TypeCode.Int32: return ElementType.I4;
case TypeCode.UInt32: return ElementType.U4;
case TypeCode.Int64: return ElementType.I8;
case TypeCode.UInt64: return ElementType.U8;
case TypeCode.Single: return ElementType.R4;
case TypeCode.Double: return ElementType.R8;
case TypeCode.String: return ElementType.String;
default: return ElementType.Void;
}
return System.Type.GetTypeCode(value.GetType()) switch {
TypeCode.Boolean => ElementType.Boolean,
TypeCode.Char => ElementType.Char,
TypeCode.SByte => ElementType.I1,
TypeCode.Byte => ElementType.U1,
TypeCode.Int16 => ElementType.I2,
TypeCode.UInt16 => ElementType.U2,
TypeCode.Int32 => ElementType.I4,
TypeCode.UInt32 => ElementType.U4,
TypeCode.Int64 => ElementType.I8,
TypeCode.UInt64 => ElementType.U8,
TypeCode.Single => ElementType.R4,
TypeCode.Double => ElementType.R8,
TypeCode.String => ElementType.String,
_ => ElementType.Void,
};
}
}

Expand Down
53 changes: 25 additions & 28 deletions src/DotNet/CustomAttributeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,11 @@ CAArgument ReadArrayArgument(SZArraySig arrayType) {
}

CANamedArgument ReadNamedArgument() {
bool isField;
switch ((SerializationType)reader.ReadByte()) {
case SerializationType.Property:isField = false; break;
case SerializationType.Field: isField = true; break;
default: throw new CABlobParserException("Named argument is not a field/property");
}

var isField = (SerializationType)reader.ReadByte() switch {
SerializationType.Property => false,
SerializationType.Field => true,
_ => throw new CABlobParserException("Named argument is not a field/property"),
};
var fieldPropType = ReadFieldOrPropType();
var name = ReadUTF8String();
var argument = ReadFixedArg(fieldPropType);
Expand All @@ -559,27 +557,26 @@ CANamedArgument ReadNamedArgument() {
TypeSig ReadFieldOrPropType() {
if (!recursionCounter.Increment())
throw new CABlobParserException("Too much recursion");
TypeSig result;
switch ((SerializationType)reader.ReadByte()) {
case SerializationType.Boolean: result = module.CorLibTypes.Boolean; break;
case SerializationType.Char: result = module.CorLibTypes.Char; break;
case SerializationType.I1: result = module.CorLibTypes.SByte; break;
case SerializationType.U1: result = module.CorLibTypes.Byte; break;
case SerializationType.I2: result = module.CorLibTypes.Int16; break;
case SerializationType.U2: result = module.CorLibTypes.UInt16; break;
case SerializationType.I4: result = module.CorLibTypes.Int32; break;
case SerializationType.U4: result = module.CorLibTypes.UInt32; break;
case SerializationType.I8: result = module.CorLibTypes.Int64; break;
case SerializationType.U8: result = module.CorLibTypes.UInt64; break;
case SerializationType.R4: result = module.CorLibTypes.Single; break;
case SerializationType.R8: result = module.CorLibTypes.Double; break;
case SerializationType.String: result = module.CorLibTypes.String; break;
case SerializationType.SZArray: result = new SZArraySig(ReadFieldOrPropType()); break;
case SerializationType.Type: result = new ClassSig(module.CorLibTypes.GetTypeRef("System", "Type")); break;
case SerializationType.TaggedObject: result = module.CorLibTypes.Object; break;
case SerializationType.Enum: result = ReadType(false); break;
default: throw new CABlobParserException("Invalid type");
}
var result = (SerializationType)reader.ReadByte() switch {
SerializationType.Boolean => module.CorLibTypes.Boolean,
SerializationType.Char => module.CorLibTypes.Char,
SerializationType.I1 => module.CorLibTypes.SByte,
SerializationType.U1 => module.CorLibTypes.Byte,
SerializationType.I2 => module.CorLibTypes.Int16,
SerializationType.U2 => module.CorLibTypes.UInt16,
SerializationType.I4 => module.CorLibTypes.Int32,
SerializationType.U4 => module.CorLibTypes.UInt32,
SerializationType.I8 => module.CorLibTypes.Int64,
SerializationType.U8 => module.CorLibTypes.UInt64,
SerializationType.R4 => module.CorLibTypes.Single,
SerializationType.R8 => module.CorLibTypes.Double,
SerializationType.String => module.CorLibTypes.String,
SerializationType.SZArray => new SZArraySig(ReadFieldOrPropType()),
SerializationType.Type => new ClassSig(module.CorLibTypes.GetTypeRef("System", "Type")),
SerializationType.TaggedObject => module.CorLibTypes.Object,
SerializationType.Enum => ReadType(false),
_ => throw new CABlobParserException("Invalid type"),
};
recursionCounter.Decrement();
return result;
}
Expand Down
34 changes: 16 additions & 18 deletions src/DotNet/Emit/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,24 +572,22 @@ public bool IsLdcI4() {
/// <returns>The integer value</returns>
/// <exception cref="InvalidOperationException"><see cref="OpCode"/> isn't one of the
/// <c>ldc.i4</c> opcodes</exception>
public int GetLdcI4Value() {
switch (OpCode.Code) {
case Code.Ldc_I4_M1:return -1;
case Code.Ldc_I4_0: return 0;
case Code.Ldc_I4_1: return 1;
case Code.Ldc_I4_2: return 2;
case Code.Ldc_I4_3: return 3;
case Code.Ldc_I4_4: return 4;
case Code.Ldc_I4_5: return 5;
case Code.Ldc_I4_6: return 6;
case Code.Ldc_I4_7: return 7;
case Code.Ldc_I4_8: return 8;
case Code.Ldc_I4_S: return (sbyte)Operand;
case Code.Ldc_I4: return (int)Operand;
default:
throw new InvalidOperationException($"Not a ldc.i4 instruction: {this}");
}
}
public int GetLdcI4Value() =>
OpCode.Code switch {
Code.Ldc_I4_M1 => -1,
Code.Ldc_I4_0 => 0,
Code.Ldc_I4_1 => 1,
Code.Ldc_I4_2 => 2,
Code.Ldc_I4_3 => 3,
Code.Ldc_I4_4 => 4,
Code.Ldc_I4_5 => 5,
Code.Ldc_I4_6 => 6,
Code.Ldc_I4_7 => 7,
Code.Ldc_I4_8 => 8,
Code.Ldc_I4_S => (sbyte)Operand,
Code.Ldc_I4 => (int)Operand,
_ => throw new InvalidOperationException($"Not a ldc.i4 instruction: {this}"),
};

/// <summary>
/// Checks whether it's one of the <c>ldarg</c> instructions, but does <c>not</c> check
Expand Down
45 changes: 22 additions & 23 deletions src/DotNet/Emit/MethodBodyReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,29 +227,28 @@ OpCode ReadOpCode() {
/// Reads the instruction operand (if any)
/// </summary>
/// <param name="instr">The instruction</param>
object ReadOperand(Instruction instr) {
switch (instr.OpCode.OperandType) {
case OperandType.InlineBrTarget: return ReadInlineBrTarget(instr);
case OperandType.InlineField: return ReadInlineField(instr);
case OperandType.InlineI: return ReadInlineI(instr);
case OperandType.InlineI8: return ReadInlineI8(instr);
case OperandType.InlineMethod: return ReadInlineMethod(instr);
case OperandType.InlineNone: return ReadInlineNone(instr);
case OperandType.InlinePhi: return ReadInlinePhi(instr);
case OperandType.InlineR: return ReadInlineR(instr);
case OperandType.InlineSig: return ReadInlineSig(instr);
case OperandType.InlineString: return ReadInlineString(instr);
case OperandType.InlineSwitch: return ReadInlineSwitch(instr);
case OperandType.InlineTok: return ReadInlineTok(instr);
case OperandType.InlineType: return ReadInlineType(instr);
case OperandType.InlineVar: return ReadInlineVar(instr);
case OperandType.ShortInlineBrTarget: return ReadShortInlineBrTarget(instr);
case OperandType.ShortInlineI: return ReadShortInlineI(instr);
case OperandType.ShortInlineR: return ReadShortInlineR(instr);
case OperandType.ShortInlineVar: return ReadShortInlineVar(instr);
default: throw new InvalidOperationException("Invalid OpCode.OperandType");
}
}
object ReadOperand(Instruction instr) =>
instr.OpCode.OperandType switch {
OperandType.InlineBrTarget => ReadInlineBrTarget(instr),
OperandType.InlineField => ReadInlineField(instr),
OperandType.InlineI => ReadInlineI(instr),
OperandType.InlineI8 => ReadInlineI8(instr),
OperandType.InlineMethod => ReadInlineMethod(instr),
OperandType.InlineNone => ReadInlineNone(instr),
OperandType.InlinePhi => ReadInlinePhi(instr),
OperandType.InlineR => ReadInlineR(instr),
OperandType.InlineSig => ReadInlineSig(instr),
OperandType.InlineString => ReadInlineString(instr),
OperandType.InlineSwitch => ReadInlineSwitch(instr),
OperandType.InlineTok => ReadInlineTok(instr),
OperandType.InlineType => ReadInlineType(instr),
OperandType.InlineVar => ReadInlineVar(instr),
OperandType.ShortInlineBrTarget => ReadShortInlineBrTarget(instr),
OperandType.ShortInlineI => ReadShortInlineI(instr),
OperandType.ShortInlineR => ReadShortInlineR(instr),
OperandType.ShortInlineVar => ReadShortInlineVar(instr),
_ => throw new InvalidOperationException("Invalid OpCode.OperandType"),
};

/// <summary>
/// Reads a <see cref="OperandType.InlineBrTarget"/> operand
Expand Down
42 changes: 21 additions & 21 deletions src/DotNet/ICorLibTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,27 @@ public static CorLibTypeSig GetCorLibTypeSig(this ICorLibTypes self, string @nam
return null;
if (defAsm is null || !defAsm.IsCorLib())
return null;
switch (name) {
case "Void": return self.Void;
case "Boolean": return self.Boolean;
case "Char": return self.Char;
case "SByte": return self.SByte;
case "Byte": return self.Byte;
case "Int16": return self.Int16;
case "UInt16": return self.UInt16;
case "Int32": return self.Int32;
case "UInt32": return self.UInt32;
case "Int64": return self.Int64;
case "UInt64": return self.UInt64;
case "Single": return self.Single;
case "Double": return self.Double;
case "String": return self.String;
case "TypedReference": return self.TypedReference;
case "IntPtr": return self.IntPtr;
case "UIntPtr": return self.UIntPtr;
case "Object": return self.Object;
}
return null;
return name switch {
"Void" => self.Void,
"Boolean" => self.Boolean,
"Char" => self.Char,
"SByte" => self.SByte,
"Byte" => self.Byte,
"Int16" => self.Int16,
"UInt16" => self.UInt16,
"Int32" => self.Int32,
"UInt32" => self.UInt32,
"Int64" => self.Int64,
"UInt64" => self.UInt64,
"Single" => self.Single,
"Double" => self.Double,
"String" => self.String,
"TypedReference" => self.TypedReference,
"IntPtr" => self.IntPtr,
"UIntPtr" => self.UIntPtr,
"Object" => self.Object,
_ => null,
};
}
}
}
15 changes: 7 additions & 8 deletions src/DotNet/MD/ColumnInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,13 @@ public ColumnInfo(byte index, string name, ColumnSize columnSize, byte offset, b
/// </summary>
/// <param name="reader">A reader positioned on this column</param>
/// <returns>The column value</returns>
public uint Read(ref DataReader reader) {
switch (size) {
case 1: return reader.ReadByte();
case 2: return reader.ReadUInt16();
case 4: return reader.ReadUInt32();
default: throw new InvalidOperationException("Invalid column size");
}
}
public uint Read(ref DataReader reader) =>
size switch {
1 => reader.ReadByte(),
2 => reader.ReadUInt16(),
4 => reader.ReadUInt32(),
_ => throw new InvalidOperationException("Invalid column size"),
};

internal uint Unsafe_Read24(ref DataReader reader) {
Debug.Assert(size == 2 || size == 4);
Expand Down
35 changes: 17 additions & 18 deletions src/DotNet/MD/DotNetTableSizes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,23 @@ int GetSize(ColumnSize columnSize, IList<uint> rowCounts) {
return count > 0xFFFF ? 4 : 2;
}
else if (ColumnSize.TypeDefOrRef <= columnSize && columnSize <= ColumnSize.HasCustomDebugInformation) {
CodedToken info;
switch (columnSize) {
case ColumnSize.TypeDefOrRef: info = CodedToken.TypeDefOrRef; break;
case ColumnSize.HasConstant: info = CodedToken.HasConstant; break;
case ColumnSize.HasCustomAttribute: info = CodedToken.HasCustomAttribute; break;
case ColumnSize.HasFieldMarshal: info = CodedToken.HasFieldMarshal; break;
case ColumnSize.HasDeclSecurity: info = CodedToken.HasDeclSecurity; break;
case ColumnSize.MemberRefParent: info = CodedToken.MemberRefParent; break;
case ColumnSize.HasSemantic: info = CodedToken.HasSemantic; break;
case ColumnSize.MethodDefOrRef: info = CodedToken.MethodDefOrRef; break;
case ColumnSize.MemberForwarded: info = CodedToken.MemberForwarded; break;
case ColumnSize.Implementation: info = CodedToken.Implementation; break;
case ColumnSize.CustomAttributeType:info = CodedToken.CustomAttributeType; break;
case ColumnSize.ResolutionScope: info = CodedToken.ResolutionScope; break;
case ColumnSize.TypeOrMethodDef: info = CodedToken.TypeOrMethodDef; break;
case ColumnSize.HasCustomDebugInformation:info = CodedToken.HasCustomDebugInformation; break;
default: throw new InvalidOperationException($"Invalid ColumnSize: {columnSize}");
}
var info = columnSize switch {
ColumnSize.TypeDefOrRef => CodedToken.TypeDefOrRef,
ColumnSize.HasConstant => CodedToken.HasConstant,
ColumnSize.HasCustomAttribute => CodedToken.HasCustomAttribute,
ColumnSize.HasFieldMarshal => CodedToken.HasFieldMarshal,
ColumnSize.HasDeclSecurity => CodedToken.HasDeclSecurity,
ColumnSize.MemberRefParent => CodedToken.MemberRefParent,
ColumnSize.HasSemantic => CodedToken.HasSemantic,
ColumnSize.MethodDefOrRef => CodedToken.MethodDefOrRef,
ColumnSize.MemberForwarded => CodedToken.MemberForwarded,
ColumnSize.Implementation => CodedToken.Implementation,
ColumnSize.CustomAttributeType => CodedToken.CustomAttributeType,
ColumnSize.ResolutionScope => CodedToken.ResolutionScope,
ColumnSize.TypeOrMethodDef => CodedToken.TypeOrMethodDef,
ColumnSize.HasCustomDebugInformation => CodedToken.HasCustomDebugInformation,
_ => throw new InvalidOperationException($"Invalid ColumnSize: {columnSize}"),
};
uint maxRows = 0;
foreach (var tableType in info.TableTypes) {
int index = (int)tableType;
Expand Down
Loading

0 comments on commit ce7a762

Please sign in to comment.