1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 01:18:46 +00:00

🐞 Fix for #20

Sanitize string input, rather than T. Call ToString() on dictionary value
This commit is contained in:
Oliver Booth 2021-01-17 00:36:28 +00:00
parent 6f26c04f6c
commit 3ad20fdb40

View File

@ -19,15 +19,25 @@ namespace X10D
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value; set.</returns>
public static string ToConnectionString<T1, T2>(this IReadOnlyDictionary<T1, T2> dictionary)
{
static string SanitizeValue<T>(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;
}
var strings = dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value)}");
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?.ToString())}");
return string.Join(";", strings);
}