Make Import-Csv support CR, LF and CRLF as line delimiters (#5363)
With the fix, `Import-Csv` support CR (\r), LF (\n), CRLF (\r\n) as line delimiters.
This commit is contained in:
parent
2be13a623e
commit
c86f243ca0
|
@ -1483,21 +1483,12 @@ namespace Microsoft.PowerShell.Commands
|
|||
break;
|
||||
}
|
||||
}
|
||||
else if (IsNewLine(ch))
|
||||
else if (IsNewLine(ch, out string newLine))
|
||||
{
|
||||
if (ch == '\r')
|
||||
{
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
if (seenBeginQuote)
|
||||
{
|
||||
//newline inside quote are valid
|
||||
current.Append(ch);
|
||||
if (ch == '\r')
|
||||
{
|
||||
current.Append('\n');
|
||||
}
|
||||
current.Append(newLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1520,23 +1511,30 @@ namespace Microsoft.PowerShell.Commands
|
|||
return result;
|
||||
}
|
||||
|
||||
// If we detect a newline we return it as a string "\r", "\n" or "\r\n"
|
||||
private
|
||||
bool
|
||||
IsNewLine(char ch)
|
||||
IsNewLine(char ch, out string newLine)
|
||||
{
|
||||
bool newLine = false;
|
||||
if (ch == '\n')
|
||||
{
|
||||
newLine = true;
|
||||
}
|
||||
else if (ch == '\r')
|
||||
newLine = "";
|
||||
if (ch == '\r')
|
||||
{
|
||||
if (PeekNextChar('\n'))
|
||||
{
|
||||
newLine = true;
|
||||
ReadChar();
|
||||
newLine = "\r\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
newLine = "\r";
|
||||
}
|
||||
}
|
||||
return newLine;
|
||||
else if (ch == '\n')
|
||||
{
|
||||
newLine = "\n";
|
||||
}
|
||||
|
||||
return newLine != "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -1574,13 +1572,9 @@ namespace Microsoft.PowerShell.Commands
|
|||
{
|
||||
break;
|
||||
}
|
||||
else if (IsNewLine(ch))
|
||||
else if (IsNewLine(ch, out string newLine))
|
||||
{
|
||||
endOfRecord = true;
|
||||
if (ch == '\r')
|
||||
{
|
||||
ReadChar();
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
|
|
|
@ -92,3 +92,24 @@ Describe "Import-Csv #Type Tests" -Tags "CI" {
|
|||
$importType | Should Be $expectedProcessType
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Import-Csv with different newlines" -Tags "CI" {
|
||||
It "Test import-csv with '<name>' newline" -TestCases @(
|
||||
@{ name = "CR"; newline = "`r" }
|
||||
@{ name = "LF"; newline = "`n" }
|
||||
@{ name = "CRLF"; newline = "`r`n" }
|
||||
) {
|
||||
param($newline)
|
||||
$csvFile = Join-Path $TestDrive -ChildPath $((New-Guid).Guid)
|
||||
$delimiter = ','
|
||||
"h1,h2,h3$($newline)11,12,13$($newline)21,22,23$($newline)" | Out-File -FilePath $csvFile
|
||||
$returnObject = Import-Csv -Path $csvFile -Delimiter $delimiter
|
||||
$returnObject.Count | Should Be 2
|
||||
$returnObject[0].h1 | Should Be 11
|
||||
$returnObject[0].h2 | Should Be 12
|
||||
$returnObject[0].h3 | Should Be 13
|
||||
$returnObject[1].h1 | Should Be 21
|
||||
$returnObject[1].h2 | Should Be 22
|
||||
$returnObject[1].h3 | Should Be 23
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue