Skip to content

Commit 6a9bf10

Browse files
committed
Fix GenerateLinesWithoutWordWrap()
We split a line to fit in a windows width and if on last iteration we did get zero length tail we did next iteration but we should stop.
1 parent 9dafad3 commit 6a9bf10

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

‎src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,12 @@ private static StringCollection GenerateLinesWithoutWordWrap(DisplayCells displa
416416

417417
for (int k = 0; k < lines.Length; k++)
418418
{
419-
if (lines[k] == null || displayCells.Length(lines[k]) <= firstLineLen)
419+
var currentLine = lines[k];
420+
421+
if (currentLine == null || displayCells.Length(currentLine) <= firstLineLen)
420422
{
421423
// we do not need to split further, just add
422-
retVal.Add(lines[k]);
424+
retVal.Add(currentLine);
423425
continue;
424426
}
425427

@@ -432,15 +434,15 @@ private static StringCollection GenerateLinesWithoutWordWrap(DisplayCells displa
432434

433435
int offset = 0; // offset into the line we are splitting
434436

435-
while (true)
437+
while (currentLine.Length > offset)
436438
{
437439
// acquire the current active display line length (it can very from call to call)
438440
int currentDisplayLen = accumulator.ActiveLen;
439441

440442
// determine if the current tail would fit or not
441443

442444
// for the remaining part of the string, determine its display cell count
443-
int currentCellsToFit = displayCells.Length(lines[k], offset);
445+
int currentCellsToFit = displayCells.Length(currentLine, offset);
444446

445447
// determine if we fit into the line
446448
int excessCells = currentCellsToFit - currentDisplayLen;
@@ -449,7 +451,7 @@ private static StringCollection GenerateLinesWithoutWordWrap(DisplayCells displa
449451
{
450452
// we are not at the end of the string, select a sub string
451453
// that would fit in the remaining display length
452-
int charactersToAdd = displayCells.GetHeadSplitLength(lines[k], offset, currentDisplayLen);
454+
int charactersToAdd = displayCells.GetHeadSplitLength(currentLine, offset, currentDisplayLen);
453455

454456
if (charactersToAdd <= 0)
455457
{
@@ -463,7 +465,7 @@ private static StringCollection GenerateLinesWithoutWordWrap(DisplayCells displa
463465
else
464466
{
465467
// of the given length, add it to the accumulator
466-
accumulator.AddLine(lines[k].Substring(offset, charactersToAdd));
468+
accumulator.AddLine(currentLine.Substring(offset, charactersToAdd));
467469
}
468470

469471
// increase the offset by the # of characters added
@@ -472,7 +474,7 @@ private static StringCollection GenerateLinesWithoutWordWrap(DisplayCells displa
472474
else
473475
{
474476
// we reached the last (partial) line, we add it all
475-
accumulator.AddLine(lines[k].Substring(offset));
477+
accumulator.AddLine(currentLine.Substring(offset));
476478
break;
477479
}
478480
}

‎src/System.Management.Automation/FormatAndOutput/out-console/ConsoleLineOutput.cs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,15 @@ internal DisplayCellsPSHost(PSHostRawUserInterface rawUserInterface)
8787

8888
internal override int Length(string str, int offset)
8989
{
90-
Dbg.Assert(offset >= 0, "offset >= 0");
91-
Dbg.Assert(string.IsNullOrEmpty(str) || (offset < str.Length), "offset < str.Length");
90+
if (string.IsNullOrEmpty(str))
91+
{
92+
return 0;
93+
}
94+
95+
if (offset < 0 || offset >= str.Length)
96+
{
97+
throw PSTraceSource.NewArgumentException(nameof(offset));
98+
}
9299

93100
try
94101
{
@@ -100,7 +107,7 @@ internal override int Length(string str, int offset)
100107
// we will fallback to the default value.
101108
}
102109

103-
return string.IsNullOrEmpty(str) ? 0 : str.Length - offset;
110+
return str.Length - offset;
104111
}
105112

106113
internal override int Length(string str)

0 commit comments

Comments
 (0)