Optimize dictionary access

This commit is contained in:
Peter Butzhammer
2024-02-06 13:19:32 +01:00
parent 55050dccd6
commit 49fe1968d9

View File

@@ -12,7 +12,7 @@ namespace Sharp7.Rx
{ {
private static readonly Regex regex = new Regex(@"^(?<operand>db{1})(?<dbNr>\d{1,4})\.?(?<type>dbx|x|s|string|b|dbb|d|int|dbw|w|dint|dul|dulint|dulong|){1}(?<start>\d+)(\.(?<bitOrLength>\d+))?$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant); private static readonly Regex regex = new Regex(@"^(?<operand>db{1})(?<dbNr>\d{1,4})\.?(?<type>dbx|x|s|string|b|dbb|d|int|dbw|w|dint|dul|dulint|dulong|){1}(?<start>\d+)(\.(?<bitOrLength>\d+))?$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
private readonly Dictionary<string, DbType> types = new Dictionary<string, DbType> private static readonly IReadOnlyDictionary<string, DbType> types = new Dictionary<string, DbType>(StringComparer.InvariantCultureIgnoreCase)
{ {
{"x", DbType.Bit}, {"x", DbType.Bit},
{"dbx", DbType.Bit}, {"dbx", DbType.Bit},
@@ -30,16 +30,17 @@ namespace Sharp7.Rx
{"dulong", DbType.ULong } {"dulong", DbType.ULong }
}; };
public S7VariableAddress Parse(string input) public S7VariableAddress Parse(string input)
{ {
var match = regex.Match(input); var match = regex.Match(input);
if (match.Success) if (match.Success)
{ {
var operand = (Operand)Enum.Parse(typeof(Operand), match.Groups["operand"].Value, true); var operand = (Operand)Enum.Parse(typeof(Operand), match.Groups["operand"].Value, true);
var dbNr = ushort.Parse(match.Groups["dbNr"].Value, NumberStyles.Integer); var dbNr = ushort.Parse(match.Groups["dbNr"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture);
var start = ushort.Parse(match.Groups["start"].Value, NumberStyles.Integer); var start = ushort.Parse(match.Groups["start"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture);
var type = ParseType(match.Groups["type"].Value); if (!types.TryGetValue(match.Groups["type"].Value, out var type))
return null;
var s7VariableAddress = new S7VariableAddress var s7VariableAddress = new S7VariableAddress
{ {
@@ -49,34 +50,30 @@ namespace Sharp7.Rx
Type = type, Type = type,
}; };
if (type == DbType.Bit) switch (type)
{ {
s7VariableAddress.Length = 1; case DbType.Bit:
s7VariableAddress.Bit = byte.Parse(match.Groups["bitOrLength"].Value); s7VariableAddress.Length = 1;
} s7VariableAddress.Bit = byte.Parse(match.Groups["bitOrLength"].Value);
else if (type == DbType.Byte) break;
{ case DbType.Byte:
s7VariableAddress.Length = match.Groups["bitOrLength"].Success ? ushort.Parse(match.Groups["bitOrLength"].Value) : (ushort)1; s7VariableAddress.Length = match.Groups["bitOrLength"].Success ? ushort.Parse(match.Groups["bitOrLength"].Value) : (ushort)1;
} break;
else if (type == DbType.String) case DbType.String:
{ s7VariableAddress.Length = match.Groups["bitOrLength"].Success ? ushort.Parse(match.Groups["bitOrLength"].Value) : (ushort)0;
s7VariableAddress.Length = match.Groups["bitOrLength"].Success ? ushort.Parse(match.Groups["bitOrLength"].Value) : (ushort)0; break;
} case DbType.Integer:
else if (type == DbType.Integer) s7VariableAddress.Length = 2;
{ break;
s7VariableAddress.Length = 2; case DbType.DInteger:
} s7VariableAddress.Length = 4;
else if (type == DbType.DInteger) break;
{ case DbType.ULong:
s7VariableAddress.Length = 4; s7VariableAddress.Length = 8;
} break;
else if (type == DbType.ULong) case DbType.Double:
{ s7VariableAddress.Length = 4;
s7VariableAddress.Length = 8; break;
}
else if (type == DbType.Double)
{
s7VariableAddress.Length = 4;
} }
return s7VariableAddress; return s7VariableAddress;
@@ -84,13 +81,5 @@ namespace Sharp7.Rx
return null; return null;
} }
private DbType ParseType(string value)
{
return types
.Where(pair => pair.Key.Equals(value, StringComparison.InvariantCultureIgnoreCase))
.Select(pair => pair.Value)
.FirstOrDefault();
}
} }
} }