-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Improve Console for iOS and Android #37078
Changes from all commits
87622d0
ad41075
eb867ae
f4f6613
835cdc0
8eb4d08
bd8b9f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,5 +93,37 @@ protected void ValidateWrite(byte[] buffer, int offset, int count) | |
|
||
if (!_canWrite) throw Error.GetWriteNotSupported(); | ||
} | ||
|
||
protected static void AccumulateNewLines(StringBuilder accumulator, ReadOnlySpan<char> buffer, Action<string> printer) | ||
{ | ||
int lineStartIndex = 0; | ||
for (int i = 0; i < buffer.Length; i++) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will always walk the entire buffer. I think a more performant algorithm would be to walk the buffer backwards, and if you find a newline character, you can print everything up to that character (prepending anything in the buffer), and then put the rest in the buffer. This would be much faster for Console.WriteLine statements, since it would only check the last character in the buffer. for (int i = buffer.Length - 1; i >= 0; i--) {
if (buffer[i] == '\n') {
if (accumulator.Length > 0) {
printer (accumulator + buffer.Slice(0, i + 1));
accumulator.Clear();
} else {
printer (buffer.Slice(0, i + 1));
}
if (i < buffer.Length - 1) {
// there's text after the last newline, add it to the accumulator
accumulator.Append(buffer.Slice(i + 1));
}
return;
}
}
// no newlines found, add the entire buffer to the accumulator
accumulator.Append(buffer); |
||
{ | ||
if (buffer[i] == '\n') | ||
{ | ||
ReadOnlySpan<char> sliceWithNl = buffer.Slice(lineStartIndex, i - lineStartIndex); | ||
if (accumulator.Length > 0) | ||
{ | ||
// we found a new line, append content from accumulator if it's not empty | ||
accumulator.Append(sliceWithNl); | ||
printer(accumulator.ToString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are lots of strings created. Would it be possible to pass ReadOnlySpans around instead? |
||
accumulator.Clear(); | ||
} | ||
else | ||
{ | ||
// accumulator is empty - print the line as it is | ||
printer(sliceWithNl.ToString()); | ||
} | ||
lineStartIndex = i + 1; | ||
} | ||
} | ||
|
||
if (buffer.Length > 0 && buffer[^1] != '\n') | ||
{ | ||
// add a line without '\n' to accumulator | ||
ReadOnlySpan<char> appendix = buffer.Slice(lineStartIndex, buffer.Length - lineStartIndex); | ||
accumulator.Append(appendix); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't necessarily be two-byte aligned. Is that ok?