1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 19:42:55 +08:00

Merge pull request #19014 from peppy/line-buffered-reader-simplification

Enable NRT and simplify `LineBufferedReader`
This commit is contained in:
Dan Balasescu 2022-07-06 20:27:04 +09:00 committed by GitHub
commit b9acdcdbe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 22 deletions

View File

@ -29,7 +29,7 @@ namespace osu.Game.Beatmaps.Formats
{ {
Section section = Section.General; Section section = Section.General;
string line; string? line;
while ((line = stream.ReadLine()) != null) while ((line = stream.ReadLine()) != null)
{ {

View File

@ -1,10 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
@ -17,34 +14,31 @@ namespace osu.Game.IO
public class LineBufferedReader : IDisposable public class LineBufferedReader : IDisposable
{ {
private readonly StreamReader streamReader; private readonly StreamReader streamReader;
private readonly Queue<string> lineBuffer;
private string? peekedLine;
public LineBufferedReader(Stream stream, bool leaveOpen = false) public LineBufferedReader(Stream stream, bool leaveOpen = false)
{ {
streamReader = new StreamReader(stream, Encoding.UTF8, true, 1024, leaveOpen); streamReader = new StreamReader(stream, Encoding.UTF8, true, -1, leaveOpen);
lineBuffer = new Queue<string>();
} }
/// <summary> /// <summary>
/// Reads the next line from the stream without consuming it. /// Reads the next line from the stream without consuming it.
/// Subsequent calls to <see cref="PeekLine"/> without a <see cref="ReadLine"/> will return the same string. /// Subsequent calls to <see cref="PeekLine"/> without a <see cref="ReadLine"/> will return the same string.
/// </summary> /// </summary>
public string PeekLine() public string? PeekLine() => peekedLine ??= streamReader.ReadLine();
{
if (lineBuffer.Count > 0)
return lineBuffer.Peek();
string line = streamReader.ReadLine();
if (line != null)
lineBuffer.Enqueue(line);
return line;
}
/// <summary> /// <summary>
/// Reads the next line from the stream and consumes it. /// Reads the next line from the stream and consumes it.
/// If a line was peeked, that same line will then be consumed and returned. /// If a line was peeked, that same line will then be consumed and returned.
/// </summary> /// </summary>
public string ReadLine() => lineBuffer.Count > 0 ? lineBuffer.Dequeue() : streamReader.ReadLine(); public string? ReadLine()
{
string? line = peekedLine ?? streamReader.ReadLine();
peekedLine = null;
return line;
}
/// <summary> /// <summary>
/// Reads the stream to its end and returns the text read. /// Reads the stream to its end and returns the text read.
@ -53,14 +47,13 @@ namespace osu.Game.IO
public string ReadToEnd() public string ReadToEnd()
{ {
string remainingText = streamReader.ReadToEnd(); string remainingText = streamReader.ReadToEnd();
if (lineBuffer.Count == 0) if (peekedLine == null)
return remainingText; return remainingText;
var builder = new StringBuilder(); var builder = new StringBuilder();
// this might not be completely correct due to varying platform line endings // this might not be completely correct due to varying platform line endings
while (lineBuffer.Count > 0) builder.AppendLine(peekedLine);
builder.AppendLine(lineBuffer.Dequeue());
builder.Append(remainingText); builder.Append(remainingText);
return builder.ToString(); return builder.ToString();
@ -68,7 +61,7 @@ namespace osu.Game.IO
public void Dispose() public void Dispose()
{ {
streamReader?.Dispose(); streamReader.Dispose();
} }
} }
} }