From 9cbd2a9e0048b29b0521797edb663ec21c11574d Mon Sep 17 00:00:00 2001 From: Ali Tas Date: Wed, 27 Sep 2023 14:33:24 -0400 Subject: [PATCH 1/4] Change order of checks in default ShouldQuote implementation --- src/CsvHelper/Configuration/ConfigurationFunctions.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CsvHelper/Configuration/ConfigurationFunctions.cs b/src/CsvHelper/Configuration/ConfigurationFunctions.cs index 0cf725995..9552ec5e5 100644 --- a/src/CsvHelper/Configuration/ConfigurationFunctions.cs +++ b/src/CsvHelper/Configuration/ConfigurationFunctions.cs @@ -105,12 +105,12 @@ public static bool ShouldQuote(ShouldQuoteArgs args) var shouldQuote = !string.IsNullOrEmpty(args.Field) && ( - args.Field.Contains(config.Quote) // Contains quote - || args.Field[0] == ' ' // Starts with a space + args.Field[0] == ' ' // Starts with a space || args.Field[args.Field.Length - 1] == ' ' // Ends with a space - || (config.Delimiter.Length > 0 && args.Field.Contains(config.Delimiter)) // Contains delimiter + || args.Field.Contains(config.Quote) // Contains quote || !config.IsNewLineSet && args.Field.IndexOfAny(lineEndingChars) > -1 // Contains line ending characters || config.IsNewLineSet && args.Field.Contains(config.NewLine) // Contains newline + || (config.Delimiter.Length > 0 && args.Field.Contains(config.Delimiter)) // Contains delimiter ); return shouldQuote; From 0bf0b459adbd0c9660695692d0acb88c610e21f7 Mon Sep 17 00:00:00 2001 From: Ali Tas Date: Wed, 27 Sep 2023 14:41:35 -0400 Subject: [PATCH 2/4] Use indexer --- src/CsvHelper/Configuration/ConfigurationFunctions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CsvHelper/Configuration/ConfigurationFunctions.cs b/src/CsvHelper/Configuration/ConfigurationFunctions.cs index 9552ec5e5..ab92076f9 100644 --- a/src/CsvHelper/Configuration/ConfigurationFunctions.cs +++ b/src/CsvHelper/Configuration/ConfigurationFunctions.cs @@ -106,7 +106,7 @@ public static bool ShouldQuote(ShouldQuoteArgs args) var shouldQuote = !string.IsNullOrEmpty(args.Field) && ( args.Field[0] == ' ' // Starts with a space - || args.Field[args.Field.Length - 1] == ' ' // Ends with a space + || args.Field[^1] == ' ' // Ends with a space || args.Field.Contains(config.Quote) // Contains quote || !config.IsNewLineSet && args.Field.IndexOfAny(lineEndingChars) > -1 // Contains line ending characters || config.IsNewLineSet && args.Field.Contains(config.NewLine) // Contains newline From 31615d6b8b041ad863e824dcbd62a4f21ca4d979 Mon Sep 17 00:00:00 2001 From: Ali Tas Date: Thu, 5 Oct 2023 10:17:27 -0400 Subject: [PATCH 3/4] Use StartsWith & EndsWith instead --- src/CsvHelper/Configuration/ConfigurationFunctions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CsvHelper/Configuration/ConfigurationFunctions.cs b/src/CsvHelper/Configuration/ConfigurationFunctions.cs index ab92076f9..25575f251 100644 --- a/src/CsvHelper/Configuration/ConfigurationFunctions.cs +++ b/src/CsvHelper/Configuration/ConfigurationFunctions.cs @@ -105,8 +105,8 @@ public static bool ShouldQuote(ShouldQuoteArgs args) var shouldQuote = !string.IsNullOrEmpty(args.Field) && ( - args.Field[0] == ' ' // Starts with a space - || args.Field[^1] == ' ' // Ends with a space + args.Field.StartsWith(' ') // Starts with a space + || args.Field.EndsWith(' ') // Ends with a space || args.Field.Contains(config.Quote) // Contains quote || !config.IsNewLineSet && args.Field.IndexOfAny(lineEndingChars) > -1 // Contains line ending characters || config.IsNewLineSet && args.Field.Contains(config.NewLine) // Contains newline From 2aaad88487221cf29df71f049b1c7afb03563909 Mon Sep 17 00:00:00 2001 From: Josh Close Date: Wed, 24 Jan 2024 13:16:57 -0600 Subject: [PATCH 4/4] Cleanup. --- src/CsvHelper/Configuration/ConfigurationFunctions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CsvHelper/Configuration/ConfigurationFunctions.cs b/src/CsvHelper/Configuration/ConfigurationFunctions.cs index b1445f9bc..4d0f51a6c 100644 --- a/src/CsvHelper/Configuration/ConfigurationFunctions.cs +++ b/src/CsvHelper/Configuration/ConfigurationFunctions.cs @@ -102,8 +102,8 @@ public static bool ShouldQuote(ShouldQuoteArgs args) var shouldQuote = !string.IsNullOrEmpty(args.Field) && ( - args.Field.StartsWith(' ') // Starts with a space - || args.Field.EndsWith(' ') // Ends with a space + args.Field[0] == ' ' // Starts with a space + || args.Field[^1] == ' ' // Ends with a space || args.Field.Contains(config.Quote) // Contains quote || !config.IsNewLineSet && args.Field.IndexOfAny(lineEndingChars) > -1 // Contains line ending characters || config.IsNewLineSet && args.Field.Contains(config.NewLine) // Contains newline