From a40089a579733c086e5c962d1f0146fe20bedc80 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 4 Dec 2020 21:11:25 +0500 Subject: [PATCH 1/3] Exclude string.ToLowerInvariant() in GetEnvironmentVariableAsBool() --- .../utils/Telemetry.cs | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/System.Management.Automation/utils/Telemetry.cs b/src/System.Management.Automation/utils/Telemetry.cs index 46026bb931d..31f201902e7 100644 --- a/src/System.Management.Automation/utils/Telemetry.cs +++ b/src/System.Management.Automation/utils/Telemetry.cs @@ -563,19 +563,41 @@ private static bool GetEnvironmentVariableAsBool(string name, bool defaultValue) return defaultValue; } - switch (str.ToLowerInvariant()) + var boolStr = str.AsSpan().Trim(); + + if (boolStr.Length == 1) { - case "true": - case "1": - case "yes": + if (boolStr[0] == '1') + { return true; - case "false": - case "0": - case "no": + } + + if (boolStr[0] == '0') + { return false; - default: - return defaultValue; + } } + + if (boolStr.Length == 4 && + (boolStr[0] == 't' || boolStr[0] == 'T') && + (boolStr[1] == 'r' || boolStr[1] == 'R') && + (boolStr[2] == 'u' || boolStr[2] == 'U') && + (boolStr[3] == 'e' || boolStr[3] == 'E')) + { + return true; + } + + if (boolStr.Length == 5 && + (boolStr[0] == 'f' || boolStr[0] == 'F') && + (boolStr[1] == 'a' || boolStr[1] == 'A') && + (boolStr[2] == 'l' || boolStr[2] == 'L') && + (boolStr[3] == 's' || boolStr[3] == 'S') && + (boolStr[4] == 'e' || boolStr[4] == 'E')) + { + return false; + } + + return defaultValue; } /// From dae3653abef7456809ec0f06ac9d9c72584f8dea Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 4 Dec 2020 22:19:40 +0500 Subject: [PATCH 2/3] process yes and no values --- .../utils/Telemetry.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/System.Management.Automation/utils/Telemetry.cs b/src/System.Management.Automation/utils/Telemetry.cs index 31f201902e7..1ba69932830 100644 --- a/src/System.Management.Automation/utils/Telemetry.cs +++ b/src/System.Management.Automation/utils/Telemetry.cs @@ -578,6 +578,21 @@ private static bool GetEnvironmentVariableAsBool(string name, bool defaultValue) } } + if (boolStr.Length == 3 && + (boolStr[0] == 'y' || boolStr[0] == 'Y') && + (boolStr[1] == 'e' || boolStr[1] == 'E') && + (boolStr[2] == 's' || boolStr[2] == 'S')) + { + return true; + } + + if (boolStr.Length == 2 && + (boolStr[0] == 'n' || boolStr[0] == 'N') && + (boolStr[1] == 'o' || boolStr[1] == 'O')) + { + return false; + } + if (boolStr.Length == 4 && (boolStr[0] == 't' || boolStr[0] == 'T') && (boolStr[1] == 'r' || boolStr[1] == 'R') && From a4f2968973d3fa55674ecd2a45d827f01a33a021 Mon Sep 17 00:00:00 2001 From: Ilya Date: Sat, 5 Dec 2020 11:34:42 +0500 Subject: [PATCH 3/3] Address feedback --- src/System.Management.Automation/utils/Telemetry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/utils/Telemetry.cs b/src/System.Management.Automation/utils/Telemetry.cs index 1ba69932830..3ff3c908867 100644 --- a/src/System.Management.Automation/utils/Telemetry.cs +++ b/src/System.Management.Automation/utils/Telemetry.cs @@ -563,7 +563,7 @@ private static bool GetEnvironmentVariableAsBool(string name, bool defaultValue) return defaultValue; } - var boolStr = str.AsSpan().Trim(); + var boolStr = str.AsSpan(); if (boolStr.Length == 1) {