Skip to content

Commit 7dcd64c

Browse files
committed
Used the ToUpperInvariant/switch pattern on string matches instead of an if series.
1 parent ed43fb8 commit 7dcd64c

1 file changed

Lines changed: 33 additions & 31 deletions

File tree

SqlServerSimulator/Storage/AppLock.cs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,28 @@ public static string NormalizeResource(string resource) =>
5454
/// </summary>
5555
public static bool TryParseMode(string text, out LockMode mode)
5656
{
57-
if (text.Equals("Shared", StringComparison.OrdinalIgnoreCase))
57+
if (text.Length <= "IntentExclusive".Length)
5858
{
59-
mode = LockMode.Shared;
60-
return true;
61-
}
62-
if (text.Equals("Update", StringComparison.OrdinalIgnoreCase))
63-
{
64-
mode = LockMode.Update;
65-
return true;
66-
}
67-
if (text.Equals("IntentShared", StringComparison.OrdinalIgnoreCase))
68-
{
69-
mode = LockMode.IntentShared;
70-
return true;
71-
}
72-
if (text.Equals("IntentExclusive", StringComparison.OrdinalIgnoreCase))
73-
{
74-
mode = LockMode.IntentExclusive;
75-
return true;
76-
}
77-
if (text.Equals("Exclusive", StringComparison.OrdinalIgnoreCase))
78-
{
79-
mode = LockMode.Exclusive;
80-
return true;
59+
Span<char> upper = stackalloc char[text.Length];
60+
_ = text.ToUpperInvariant(upper);
61+
switch (upper)
62+
{
63+
case "EXCLUSIVE":
64+
mode = LockMode.Exclusive;
65+
return true;
66+
case "INTENTEXCLUSIVE":
67+
mode = LockMode.IntentExclusive;
68+
return true;
69+
case "INTENTSHARED":
70+
mode = LockMode.IntentShared;
71+
return true;
72+
case "SHARED":
73+
mode = LockMode.Shared;
74+
return true;
75+
case "UPDATE":
76+
mode = LockMode.Update;
77+
return true;
78+
}
8179
}
8280

8381
mode = default;
@@ -92,15 +90,19 @@ public static bool TryParseMode(string text, out LockMode mode)
9290
/// </summary>
9391
public static bool TryParseOwner(string text, out bool isTransaction)
9492
{
95-
if (text.Equals("Transaction", StringComparison.OrdinalIgnoreCase))
96-
{
97-
isTransaction = true;
98-
return true;
99-
}
100-
if (text.Equals("Session", StringComparison.OrdinalIgnoreCase))
93+
if (text.Length <= "Transaction".Length)
10194
{
102-
isTransaction = false;
103-
return true;
95+
Span<char> upper = stackalloc char[text.Length];
96+
_ = text.ToUpperInvariant(upper);
97+
switch (upper)
98+
{
99+
case "SESSION":
100+
isTransaction = false;
101+
return true;
102+
case "TRANSACTION":
103+
isTransaction = true;
104+
return true;
105+
}
104106
}
105107

106108
isTransaction = default;

0 commit comments

Comments
 (0)