DevSpatium.IO.TextReader 0.0.9-alpha

.NET Standard 2.0 .NET Framework 4.5
This is a prerelease version of DevSpatium.IO.TextReader.
dotnet add package DevSpatium.IO.TextReader --version 0.0.9-alpha
NuGet\Install-Package DevSpatium.IO.TextReader -Version 0.0.9-alpha
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="DevSpatium.IO.TextReader" Version="0.0.9-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DevSpatium.IO.TextReader --version 0.0.9-alpha
#r "nuget: DevSpatium.IO.TextReader, 0.0.9-alpha"
#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 DevSpatium.IO.TextReader as a Cake Addin
#addin nuget:?package=DevSpatium.IO.TextReader&version=0.0.9-alpha&prerelease

// Install DevSpatium.IO.TextReader as a Cake Tool
#tool nuget:?package=DevSpatium.IO.TextReader&version=0.0.9-alpha&prerelease

TextReaderWrapper - Reads Text by Specified Pattern

A wrapper on top of the standard System.IO.TextReader, which supports sequential asynchronous reading according to a given pattern, and extracts the required pieces of text.

This reader can be useful for parsing texts of custom predefined formats, for automating manual time-consuming copy-paste tasks, or if you are going to implement some code that should have non-trivial reading logic.

Besides, the reader provides an API for extending its internal operations and the pattern syntax.

Getting Started

The pattern can essentially contain two commands: R and S (leaving all their varieties aside for now). R stands for a Read operation. Such an operation reads characters from the source text, converts them into a single string, and adds the string to the output collection. S, in its turn, stands for a Skip operation, which simply skips the required characters.

Example #1

Input

Hello World

Pattern

// R[5] - read 5 chars
// S.   - skip 1 char
// R>   - read remaining chars
const string pattern = @"R[5] S. R>";

Output

["Hello", "World"]

Example #2

Input

* Apple
* Lemon
* Pear
* Kiwi

Pattern

// S>     - skip line
// S+'* ' - skip * and space
// R>     - read remaining chars in the line
// {2}    - repeat twice
const string pattern = @"(S> S+'* ' R>){2}";

Output

["Lemon", "Kiwi"]

Example #3

Input

* 7:00  Wake-up
* 9:00  At work
* 10:00 Stand-up meeting
* 12:00 Lunch
* 16:00 Yet another meeting
...

Pattern

// S|/rgx/ - skip all characters up to the string that matches the specified regex
// {&R}    - and read the matched string
// S+/\s*/ - skip whitespace
// R>      - read remaining chars in the line
// *       - repeat till the end of the text
const string pattern = @"
(
    S| /\d{1,2}:\d{2}/ {&R}
    S+ /\s*/
    R>
)*
";

Output

[
    "7:00",
    "Wake-up",
    "9:00",
    "At work",
    "10:00",
    "Stand-up meeting",
    "12:00",
    "Lunch",
    "16:00",
    "Yet another meeting",
    ...
]

TextReaderWrapper

This is the "entry-point" class that wraps System.IO.TextReader using the following constructor:

public TextReaderWrapper(System.IO.TextReader underlyingReader)
ReadAsync()
// `pattern` - defines how to read the source text.
// `output` - contains the strings read from the source text.
public async Task ReadAsync(string pattern, ICollection<string> output, CancellationToken cancelToken = default)
const string pattern = @"(R.)*";
const string text = "Lorem ipsum";
var output = new List<string>();
var stringReader = new StringReader(text);
using (var textReaderWrapper = new TextReaderWrapper(stringReader))
{
    textReaderWrapper.ReadAsync(pattern, output).Wait();
}

// output: ["L", "o", "r", "e", "m", " ", "i", "p", "s", "u", "m"]

See more here!


Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5

    • No dependencies.
  • .NETStandard 2.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.0.9-alpha 112 2/3/2022