LazyFormatter 1.0.1

dotnet add package LazyFormatter --version 1.0.1
NuGet\Install-Package LazyFormatter -Version 1.0.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="LazyFormatter" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LazyFormatter --version 1.0.1
#r "nuget: LazyFormatter, 1.0.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 LazyFormatter as a Cake Addin
#addin nuget:?package=LazyFormatter&version=1.0.1

// Install LazyFormatter as a Cake Tool
#tool nuget:?package=LazyFormatter&version=1.0.1

lazy_formatter

The LazyFormatter has the following features:

  • Format content with variables into stream without writing complex xml or json writing routines.
  • Separate format declaration from the execution.
  • The Utf8LazyFormatter.Format method has similar interface to string.Format method
  • It reduces the memory footprint to minumum.
  • The Utf8LazyFormatter.Format method is thread-safe, can be called by multiple threads at once.

How to use it

Include the nuget package in the project

https://www.nuget.org/packages/LazyFormatter/1.0.0

Declare once as singleton or static variable:

    var pattern = "<test>{2}</test>{1}<test2>{0}<test2>";
    var formatter = Utf8LazyFormatter.Create(
      pattern, '{', '}'
    );

Use multiple times with variables provided as input to format method

  var arr = ArrayPool<byte>.Shared.Rent(pattern.Length * 2)
  try
  {
    using var stream = new MemoryStream(arr);
    formatter.Format(stream, 234, 12234.222, new DateTime(2022,11,11));
    // do something with stream
  }
  finally
  {
    ArrayPool<byte>.Shared.Return(arr);
  }

Benchmarks

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
Benchmark_Formatter 352.5 ns 150.7 ns 8.26 ns 1.00 0.00 0.0787 - 496 B 1.00
Benchmrk_StringInterpolation 465.7 ns 915.2 ns 50.17 ns 1.32 0.12 0.6332 0.0086 3976 B 8.02

How to run

run unit tests

dotnet test

run Benchmark.Net

cd LazyFormatterTest
dotnet run -c Release -- --job short --runtimes net7.0 --filter "*"

TODO:

  • Provide format into {1} placeholders like {1:C2}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  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.
  • net6.0

    • No dependencies.
  • 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
1.0.1 218 3/3/2023
1.0.0 211 2/14/2023

Add micro optimisations