Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,8 @@ internal string ConvertPropertyNamesCSV(IList<string> propertyNames)
AppendStringWithEscapeAlways(_outputString, propertyName);
break;
case BaseCsvWritingCommand.QuoteKind.AsNeeded:
if (propertyName.Contains(_delimiter))

if (propertyName.AsSpan().IndexOfAny(_delimiter, '\n', '"') != -1)
{
AppendStringWithEscapeAlways(_outputString, propertyName);
}
Expand Down Expand Up @@ -1038,7 +1039,7 @@ internal string ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyN
AppendStringWithEscapeAlways(_outputString, value);
break;
case BaseCsvWritingCommand.QuoteKind.AsNeeded:
if (value != null && value.Contains(_delimiter))
if (value != null && value.AsSpan().IndexOfAny(_delimiter, '\n', '"') != -1)
{
AppendStringWithEscapeAlways(_outputString, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,44 @@ Describe "ConvertTo-Csv" -Tags "CI" {
$result[0] | Should -BeExactly "`"FirstColumn`"rSecondColumn"
$result[1] | Should -BeExactly "Hellor"
}

It "UseQuotes AsNeeded Escapes Delimiters" {
$testDelimitersObject = [pscustomobject]@{ "FirstColumn" = "Hello,"; "Second,Column" = "World" };

$result = $testDelimitersObject | ConvertTo-Csv -UseQuotes AsNeeded -Delimiter ','

$result[0] | Should -BeExactly "FirstColumn,`"Second,Column`""
$result[1] | Should -BeExactly "`"Hello,`",World"

$result = $testDelimitersObject | ConvertTo-Csv -UseQuotes AsNeeded -Delimiter "r"

$result[0] | Should -BeExactly "`"FirstColumn`"rSecond,Column"
$result[1] | Should -BeExactly "Hello,r`"World`""
}

It "UseQuotes AsNeeded Escapes Newlines" {
$testCRLFObject = [pscustomobject]@{ "First`r`nColumn" = "Hello`r`nWorld" };
$testLFObject = [pscustomobject]@{ "First`nColumn" = "Hello`nWorld" };

$result = $testCRLFObject | ConvertTo-Csv -UseQuotes AsNeeded

$result[0] | Should -BeExactly "`"First`r`nColumn`""
$result[1] | Should -BeExactly "`"Hello`r`nWorld`""

$result = $testLFObject | ConvertTo-Csv -UseQuotes AsNeeded

$result[0] | Should -BeExactly "`"First`nColumn`""
$result[1] | Should -BeExactly "`"Hello`nWorld`""
}

It "UseQuotes AsNeeded Escapes Quotes" {
$testQuotesObject = [pscustomobject]@{ "First`"Column" = "`"Hello`" World" };

$result = $testQuotesObject | ConvertTo-Csv -UseQuotes AsNeeded

$result[0] | Should -BeExactly "`"First`"`"Column`""
$result[1] | Should -BeExactly "`"`"`"Hello`"`" World`""
}

}
}