From 3ad20fdb40bff345ba0cb328e638610c0b467e4c Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 17 Jan 2021 00:36:28 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20Fix=20for=20#20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sanitize string input, rather than T. Call ToString() on dictionary value --- X10D/src/DictionaryExtensions.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/X10D/src/DictionaryExtensions.cs b/X10D/src/DictionaryExtensions.cs index 2f7ce38..359a81b 100644 --- a/X10D/src/DictionaryExtensions.cs +++ b/X10D/src/DictionaryExtensions.cs @@ -19,15 +19,25 @@ namespace X10D /// Returns a representing the dictionary as a key=value; set. public static string ToConnectionString(this IReadOnlyDictionary dictionary) { - static string SanitizeValue(T value) + static string SanitizeValue(string? value) { - return value is string str && - Regex.IsMatch(str, "\\s") - ? $"\"{str}\"" - : value.ToString(); + if (value is null) + { + return string.Empty; + } + + for (var index = 0; index < value.Length; index++) + { + if (char.IsWhiteSpace(value[index])) + { + return $"\"{value}\""; + } + } + + return value; } - var strings = dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value)}"); + var strings = dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value?.ToString())}"); return string.Join(";", strings); }