Scanner 0.1.1

dotnet add package Scanner --version 0.1.1                
NuGet\Install-Package Scanner -Version 0.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Scanner" Version="0.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Scanner --version 0.1.1                
#r "nuget: Scanner, 0.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Scanner as a Cake Addin
#addin nuget:?package=Scanner&version=0.1.1

// Install Scanner as a Cake Tool
#tool nuget:?package=Scanner&version=0.1.1                

Scanner

Tired of writing something like this?

var n = int.Parse(Console.ReadLine());
var input = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

Just replace it with:

var sc = new AsciiScanner(); // parses byte stream with UTF-8 or ASCII encoded text
var input = new int[sc.ReadInt32()];
for (int i = 0; i < input.Length; ++i)
  input[i] = sc.ReadInt32();

OR

var sc = new TextScanner(); // parses UTF-16 text via StreamReader
var input = new int[sc.Read<int>()];
for (int i = 0; i < input.Length; ++i)
  input[i] = sc.Read<int>();

And not forget to use StreamWriter.Write instead of Console.Write for output to avoid excessive flushing!

using var output = new StreamWriter(Console.OpenStandardOutput());

output.WriteLine(Process(line));

Embeddable version

An embeddable version in the form of a short and concise code snippet intended for use in programming contests.

class Scanner
{
  StreamReader input = new(Console.OpenStandardInput(), bufferSize: 16384);
  char[] buffer = new char[4096];

  public int ReadInt()
  {
    var length = PrepareToken();
    return int.Parse(buffer.AsSpan(0, length));
  }

  public long ReadLong()
  {
    var length = PrepareToken();
    return long.Parse(buffer.AsSpan(0, length));
  }

  public double ReadDouble()
  {
    var length = PrepareToken();
    return double.Parse(buffer.AsSpan(0, length), CultureInfo.InvariantCulture);
  }

  private int PrepareToken()
  {
    int length = 0;
    bool readStart = false;
    while (true)
    {
      int ch = input.Read();
      if (ch == -1)
        break;

      if (char.IsWhiteSpace((char)ch))
      {
        if (readStart) break;
        continue;
      }

      readStart = true;
      buffer[length++] = (char)ch;
    }

    return length;
  }
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.1 167 8/9/2023
0.1.0 115 8/9/2023