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
18 changes: 14 additions & 4 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5600,17 +5600,27 @@ $beR = Get-HarnessCapRun -Arguments '/d /c 1>&2 echo 0123456789ABCDEFGHIJ' -Cap
Test-Assert ($beR.Stderr -ceq "01234HIJ`r`n" -and $beR.StderrDroppedChars -eq 12 -and $beR.StdoutDroppedChars -eq 0) ("stderr is capped the same way, on its own ('" + ([string]$beR.Stderr -replace "`r", '\r' -replace "`n", '\n') + "', dropped " + $beR.StderrDroppedChars + ")")

# A cut never splits a surrogate pair. cmd /u writes UTF-16: four emoji are
# 8 characters, plus CRLF. A cap of 6 puts the head's end and the tail's
# start inside pairs.
# 8 characters, plus CRLF. A cap of 7 puts the head's end inside a pair (its
# first half, and the slot, go to the tail) and the tail's start inside
# another.
$beEmoji = [char]::ConvertFromUtf32(0x1F600)
$beR = Get-HarnessCapRun -Arguments ('/d /u /c echo ' + $beEmoji * 4) -Cap 6 -Encoding 'unicode'
$beR = Get-HarnessCapRun -Arguments ('/d /u /c echo ' + $beEmoji * 4) -Cap 7 -Encoding 'unicode'
$beText = [string]$beR.Stdout
$beLone = 0
for ($i = 0; $i -lt $beText.Length; $i++) {
if ([char]::IsHighSurrogate($beText[$i])) { if ($i + 1 -lt $beText.Length -and [char]::IsLowSurrogate($beText[$i + 1])) { $i++ } else { $beLone++ } }
elseif ([char]::IsLowSurrogate($beText[$i])) { $beLone++ }
}
Test-Assert ($beText -ceq ($beEmoji + "`r`n") -and $beLone -eq 0 -and $beR.StdoutDroppedChars -eq 6) ("a cut inside a surrogate pair drops the whole pair, never half (" + $beText.Length + " characters kept, " + $beLone + " half pairs, dropped " + $beR.StdoutDroppedChars + ")")
Test-Assert ($beText -ceq ($beEmoji * 2 + "`r`n") -and $beLone -eq 0 -and $beR.StdoutDroppedChars -eq 4) ("a cut inside a surrogate pair drops the whole pair, never half (" + $beText.Length + " characters kept, " + $beLone + " half pairs, dropped " + $beR.StdoutDroppedChars + ")")
# Output exactly as long as the cap is kept whole, even with a pair where the
# head would end: the tail takes the slot the head turns away.
$beR = Get-HarnessCapRun -Arguments ('/d /u /c echo ab' + $beEmoji + 'c') -Cap 7 -Encoding 'unicode'
Test-Assert ($beR.Stdout -ceq ('ab' + $beEmoji + "c`r`n") -and $beR.StdoutDroppedChars -eq 0) ("output exactly at the cap, with a surrogate pair at the head's end, comes back whole (" + ((Format-HarnessCapRun $beR) -replace [regex]::Escape($beEmoji), '<emoji>') + ")")
$beText = foreach ($beCase in @(@{ Text = $beEmoji; Cap = 2 }, @{ Text = 'a' + $beEmoji + 'b'; Cap = 4 }, @{ Text = 'ab' + $beEmoji + 'cd'; Cap = 6 })) {
$beTask = [PkStreamReadTask]::Run([System.IO.StringReader]::new($beCase.Text), $beCase.Cap)
if ($beTask.Result -cne $beCase.Text -or $beTask.DroppedChars -ne 0) { ($beCase.Text -replace [regex]::Escape($beEmoji), '<emoji>') + ' cap ' + $beCase.Cap + ": kept '" + ($beTask.Result -replace [regex]::Escape($beEmoji), '<emoji>') + "', dropped " + $beTask.DroppedChars }
}
Test-Assert ($null -eq $beText) ("the same for an emoji under a cap of 2, a<emoji>b under 4 and ab<emoji>cd under 6" + $(if ($beText) { " (" + ($beText -join '; ') + ")" }))

# Long output: what's held stays within the cap while the program writes,
# and the count adds up to the whole.
Expand Down
12 changes: 8 additions & 4 deletions ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,11 @@ public sealed class PkStreamReadTask : Task<string>
public volatile bool ReaderClosed;
// The cap's tail: the last chars read, a ring once it's full. It
// grows with the output, so a cap costs nothing until output reaches
// it. _tailCap is 0 with no cap.
private readonly int _headCap, _tailCap;
// it. _tailCap is 0 with no cap. A head that turns away a pair's
// first half gives the tail its slot, before the tail is allocated,
// so output that fits the cap is kept whole.
private readonly int _headCap;
private int _tailCap;
private bool _headFull;
private char[] _tail;
private int _tailStart, _tailCount;
Expand Down Expand Up @@ -334,8 +337,9 @@ public sealed class PkStreamReadTask : Task<string>
if (take == _headCap - _text.Length)
{
_headFull = true;
// The head never ends on the first half of a surrogate pair.
if (take > 0 && Char.IsHighSurrogate(chunk[take - 1])) { take--; }
// The head never ends on the first half of a surrogate
// pair: that half goes to the tail, and its slot too.
if (take > 0 && Char.IsHighSurrogate(chunk[take - 1])) { take--; _tailCap++; }
}
_text.Append(chunk, 0, take);
i = take;
Expand Down
12 changes: 8 additions & 4 deletions Source/PkChildProcessJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ private sealed class Buffer
public volatile bool ReaderClosed;
// The cap's tail: the last chars read, a ring once it's full. It
// grows with the output, so a cap costs nothing until output reaches
// it. _tailCap is 0 with no cap.
private readonly int _headCap, _tailCap;
// it. _tailCap is 0 with no cap. A head that turns away a pair's
// first half gives the tail its slot, before the tail is allocated,
// so output that fits the cap is kept whole.
private readonly int _headCap;
private int _tailCap;
private bool _headFull;
private char[] _tail;
private int _tailStart, _tailCount;
Expand Down Expand Up @@ -92,8 +95,9 @@ private void Append(char[] chunk, int n)
if (take == _headCap - _text.Length)
{
_headFull = true;
// The head never ends on the first half of a surrogate pair.
if (take > 0 && Char.IsHighSurrogate(chunk[take - 1])) { take--; }
// The head never ends on the first half of a surrogate
// pair: that half goes to the tail, and its slot too.
if (take > 0 && Char.IsHighSurrogate(chunk[take - 1])) { take--; _tailCap++; }
}
_text.Append(chunk, 0, take);
i = take;
Expand Down