From 3f4aa7ed3d733724b4d703e00e2b3cc518322e4f Mon Sep 17 00:00:00 2001 From: Tho Ho Date: Wed, 15 May 2024 01:27:59 +0800 Subject: [PATCH] Improve split key=value --- .../MapConfigurationProvider.cs | 11 +++++++---- .../TransformConfigurationProvider.cs | 9 ++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/NetLah.Extensions.Configuration/MapConfigurationProvider.cs b/src/NetLah.Extensions.Configuration/MapConfigurationProvider.cs index 42626a3..c4f4da1 100644 --- a/src/NetLah.Extensions.Configuration/MapConfigurationProvider.cs +++ b/src/NetLah.Extensions.Configuration/MapConfigurationProvider.cs @@ -53,11 +53,14 @@ private void InternalLoad() void TryParse(string keyValue) { var pos = keyValue.IndexOf('='); - var key1 = keyValue[..pos]; - var key2 = keyValue[(pos + 1)..]; - if (configuration[key1] is { } value) + if (pos > 0 && pos < keyValue.Length - 1) { - Data[key2] = value; + var key1 = keyValue[..pos]; + var key2 = keyValue[(pos + 1)..]; + if (configuration[key1] is { } value) + { + Data[key2] = value; + } } } } diff --git a/src/NetLah.Extensions.Configuration/TransformConfigurationProvider.cs b/src/NetLah.Extensions.Configuration/TransformConfigurationProvider.cs index feba957..b85e4ae 100644 --- a/src/NetLah.Extensions.Configuration/TransformConfigurationProvider.cs +++ b/src/NetLah.Extensions.Configuration/TransformConfigurationProvider.cs @@ -62,9 +62,12 @@ private void InternalLoad() void TryParse(string prefix, string keyValue) { var pos = keyValue.IndexOf('='); - var key = keyValue[..pos]; - var value = keyValue[(pos + 1)..]; - Data[prefix + key] = value; + if (pos > 0 && pos < keyValue.Length - 1) + { + var key = keyValue[..pos]; + var value = keyValue[(pos + 1)..]; + Data[prefix + key] = value; + } } } }