-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix Console.Write on iOS #56713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MaximLipnin
merged 33 commits into
dotnet:main
from
MaximLipnin:fix_console_write_on_ios
Sep 2, 2021
Merged
Fix Console.Write on iOS #56713
Changes from 15 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
71b3888
Fix Console.Write on iOS
MaximLipnin 8f6f7bc
Move the helper method to iOS-specific location
MaximLipnin a97f0d5
Add a test
MaximLipnin 90bf33c
Remove unused using
MaximLipnin e6806e4
Apply Ralf's suggestion
MaximLipnin 2bdb6e7
Move Interop.Sys.Log to a local function
MaximLipnin 612dc1c
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin 6f8542b
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin 47d8f01
Move the caching logic over to StreamWriter
MaximLipnin 4f0653a
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin 14545a8
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin 725e64c
feedback
MaximLipnin 5b8499c
Remove unused usings
MaximLipnin 61bb68b
Derive from StreamWriter
MaximLipnin 68e10e7
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin ac45868
Feedback
MaximLipnin 2e57d54
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin c8aa30f
Try a solution not changing public API surface and using NSLogStream …
MaximLipnin b76ddfe
Some feedback
MaximLipnin b999fff
Use ArrayPool
MaximLipnin 49f5c41
Not rely on the length of the rented array
MaximLipnin aaa1f35
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin 90da2a0
Address the feedback
MaximLipnin 88685c7
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin 976954b
not use _ prefix for local variable
MaximLipnin 245edb3
Address the feedback
MaximLipnin a234df5
Merge branch 'fix_console_write_on_ios' of https://github.com/maximli…
MaximLipnin 34d215f
Fix an error
MaximLipnin 1eb7528
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin a2e118e
Print a span rather than a string
MaximLipnin 732a62f
Slice the char span to the exact used length
MaximLipnin 9375848
Address Stephen's feedback
MaximLipnin 7504d30
Remove a redundant using
MaximLipnin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/libraries/System.Console/src/System/IO/StreamWriter.iOS.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,104 @@ | ||||||||||||||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||||||||||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||||||||||||||
|
|
||||||||||||||||||
| using System.Collections.Generic; | ||||||||||||||||||
| using System.Runtime.InteropServices; | ||||||||||||||||||
| using System.Text; | ||||||||||||||||||
|
|
||||||||||||||||||
| namespace System.IO | ||||||||||||||||||
| { | ||||||||||||||||||
| internal class IOSStreamWriter : StreamWriter | ||||||||||||||||||
| { | ||||||||||||||||||
| private readonly List<byte> _acc = new List<byte>(); | ||||||||||||||||||
steveisok marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||
| private int _lineStartPos; | ||||||||||||||||||
|
|
||||||||||||||||||
| public IOSStreamWriter(Stream stream, Encoding? encoding = null, int bufferSize = -1, bool leaveOpen = false) | ||||||||||||||||||
| : base(stream, encoding, bufferSize, leaveOpen) | ||||||||||||||||||
| { | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| protected override void Flush(bool flushStream, bool flushEncoder) | ||||||||||||||||||
| { | ||||||||||||||||||
| // flushEncoder should be true at the end of the file and if | ||||||||||||||||||
| // the user explicitly calls Flush (though not if AutoFlush is true). | ||||||||||||||||||
| // This is required to flush any dangling characters from our UTF-7 | ||||||||||||||||||
| // and UTF-8 encoders. | ||||||||||||||||||
| ThrowIfDisposed(); | ||||||||||||||||||
|
|
||||||||||||||||||
| // Perf boost for Flush on non-dirty writers. | ||||||||||||||||||
| if (innerCharPos == 0 && !flushStream && !flushEncoder) | ||||||||||||||||||
| { | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if (!innerHaveWrittenPreamble) | ||||||||||||||||||
| { | ||||||||||||||||||
| innerHaveWrittenPreamble = true; | ||||||||||||||||||
| ReadOnlySpan<byte> preamble = innerEncoding.Preamble; | ||||||||||||||||||
| if (preamble.Length > 0) | ||||||||||||||||||
| { | ||||||||||||||||||
| innerStream.Write(preamble); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // For sufficiently small char data being flushed, try to encode to the stack. | ||||||||||||||||||
| // For anything else, fall back to allocating the byte[] buffer. | ||||||||||||||||||
| Span<byte> byteBuffer = stackalloc byte[0]; | ||||||||||||||||||
| if (innerByteBuffer is not null) | ||||||||||||||||||
| { | ||||||||||||||||||
| byteBuffer = innerByteBuffer; | ||||||||||||||||||
| } | ||||||||||||||||||
| else | ||||||||||||||||||
| { | ||||||||||||||||||
| int maxBytesForCharPos = innerEncoding.GetMaxByteCount(innerCharPos); | ||||||||||||||||||
| byteBuffer = maxBytesForCharPos <= 1024 ? // arbitrary threshold | ||||||||||||||||||
| stackalloc byte[1024] : | ||||||||||||||||||
| (innerByteBuffer = new byte[innerEncoding.GetMaxByteCount(innerCharBuffer.Length)]); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| for (int i = _lineStartPos; i < innerCharPos; i++) | ||||||||||||||||||
| { | ||||||||||||||||||
| if (innerCharBuffer[i] == '\n') | ||||||||||||||||||
| { | ||||||||||||||||||
| int count = innerEncoder.GetBytes(new ReadOnlySpan<char>(innerCharBuffer, _lineStartPos, i - _lineStartPos), byteBuffer, flushEncoder); | ||||||||||||||||||
| if (count > 0) | ||||||||||||||||||
| { | ||||||||||||||||||
| _acc.AddRange(byteBuffer.Slice(0, count).ToArray()); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| innerStream.Write(CollectionsMarshal.AsSpan(_acc)); | ||||||||||||||||||
| _acc.Clear(); | ||||||||||||||||||
|
|
||||||||||||||||||
| _lineStartPos = i + 1; | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if (_lineStartPos < innerCharPos) | ||||||||||||||||||
| { | ||||||||||||||||||
| int count = innerEncoder.GetBytes(new ReadOnlySpan<char>(innerCharBuffer, _lineStartPos, innerCharPos - _lineStartPos), byteBuffer, flushEncoder); | ||||||||||||||||||
| if (count > 0) | ||||||||||||||||||
| { | ||||||||||||||||||
| _acc.AddRange(byteBuffer.Slice(0, count).ToArray()); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| innerCharPos = 0; | ||||||||||||||||||
| _lineStartPos = 0; | ||||||||||||||||||
|
|
||||||||||||||||||
| if (flushStream && flushEncoder) | ||||||||||||||||||
| { | ||||||||||||||||||
| var accArray = _acc.ToArray(); | ||||||||||||||||||
| if (accArray.Length > 0) | ||||||||||||||||||
| { | ||||||||||||||||||
| innerStream.Write(accArray.AsSpan<byte>().Slice(0, accArray.Length)); | ||||||||||||||||||
|
||||||||||||||||||
| var accArray = _acc.ToArray(); | |
| if (accArray.Length > 0) | |
| { | |
| innerStream.Write(accArray.AsSpan<byte>().Slice(0, accArray.Length)); | |
| var accSpan = CollectionsMarshal.AsSpan(_acc); | |
| if (accSpan.Length > 0) | |
| { | |
| innerStream.Write(accSpan.Slice(0, accSpan.Length)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.