Skip to content

Commit

Permalink
🐛 Fix long input position commands parsing (#650)
Browse files Browse the repository at this point in the history
Allow input parsing of `position` commands with up to 500 moves
  • Loading branch information
eduherminio authored Feb 10, 2024
1 parent a0d8634 commit 1af8315
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Lynx.Cli/Listener.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NLog;
using System.Text;
using System.Threading.Channels;

namespace Lynx.Cli;
Expand All @@ -23,6 +24,8 @@ public async Task Run(CancellationToken cancellationToken, params string[] args)
await _guiInputReader.Writer.WriteAsync(arg, cancellationToken);
}

IncreaseInputBufferSize();

while (!cancellationToken.IsCancellationRequested)
{
var input = Console.ReadLine();
Expand All @@ -38,4 +41,29 @@ public async Task Run(CancellationToken cancellationToken, params string[] args)
_logger.Info("Finishing {0}", nameof(Listener));
}
}

/// <summary>
/// By default, the method reads input from a 256-character input buffer.
/// Because this includes the Environment.NewLine character(s), the method can read lines that contain up to 254 characters.
/// To read longer lines, call the OpenStandardInput(Int32) method.
/// Source: https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=net-8.0
/// </summary>
private static void IncreaseInputBufferSize()
{
// Based on Lizard's solution. 4096 * 4 is enough to accept position commands with at least 500 moves
Console.SetIn(new StreamReader(Console.OpenStandardInput(), Encoding.UTF8, false, 4096 * 4));
}

/// <summary>
/// Something like this would work as well to read input without the input buffer limitation
/// </summary>
/// <returns></returns>
private static string ReadInput()
{
Span<byte> bytes = stackalloc byte[4096 * 4];
Stream inputStream = Console.OpenStandardInput();
int outputLength = inputStream.Read(bytes);

return Encoding.UTF8.GetString(bytes[..outputLength]);
}
}

0 comments on commit 1af8315

Please sign in to comment.