BlackFox.MasterOfFoo 1.0.3

An F# library to allow using printf style strings in more places.

Install-Package BlackFox.MasterOfFoo -Version 1.0.3
dotnet add package BlackFox.MasterOfFoo --version 1.0.3
paket add BlackFox.MasterOfFoo --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

A library to allow using printf style strings in more places.

The code is essentially an extracted version of printf.fs where the environement can not only decide
what to do with the final blocks that compose the string (printf put them on the console, sprintf in a buffer, ...)
but also what to do with the parameters passed for each format specifier.

Sample usage

module MyModule =
    open System.Text
    open BlackFox.MasterOfFoo
    type private MySprintfEnv() =
        inherit PrintfEnv<unit, string, string>()
        let buf = StringBuilder()
        override this.Finalize() = buf.ToString ()
        override this.Write(s : PrintableElement) = ignore(buf.Append(s.FormatAsPrintF()))
        override this.WriteT(s : string) = ignore(buf.Append(s))

    let mysprintf (format: Format<'T, unit, string, string>) =
        doPrintfFromEnv format (MySprintfEnv())

MyModule.mysprintf "Hello %s." "World"

Mini-Doc

PrintableElement

PrintableElement represent an element in a string, for example sprintf &quot;Foo %s bar&quot; &quot;x&quot; produce 3
PrintableElement, the first contains the string &quot;Foo &quot; the second is a format specifier &#39;s&#39; with an associated
string value &quot;x&quot; and then there is the string the string &quot; Bar&quot;.

Members :

  • ElementType: Tell your if this is a string or a format specifier.
  • Value: give the value if it was a format specifier.
  • ValueType: give the type of value expected by the format specifier.
  • StarWidth: The width if specified via another parameter as in "%*i".
  • StarPrecision: The precision if specified via another parameter as in "%.*f".
  • FormatAsPrintF(): Get the string representation that printf would have normally generated.
  • Specifier: The format specification for format specifiers.

PrintfEnv

PrintfEnv is the type to implement to create a printf variant it has 3 type parameters:

  • &#39;State: The state of the printer, passed as argument when using '%t'.
  • &#39;Residue: The type that methods passed to '%t' must return.
  • &#39;Result: The final result type for the printer.

Members:

  • Finalize: Create the final result for this printer
  • Write: Write an element from the format string to the printer
  • WriteT: Write the result of the method provided by %t to the printer.

Functions

  • doPrintfFromEnv: Take a format and a PrintfEnv to create a printf-like function
  • doPrintf: Same as doPrintfFromEnv but allow to know the number of elements when the PrintfEnv is created.

FAQ

What does it allow exactly that can't be done with the original set of functions ?

  • Generating complex object that aren't only a string like an SqlCommand or structured logging.
  • Escaping parts in strings, like an xmlprintf that would escape &lt; to &amp;lt in parameters but not in the format
    string.

What are the limitations ?

The main limitation is that the F# compiler allow a strict set of things an you can't go differently.
The function signature that is the first argument to Format&lt;_,_,_,_,&gt; is generated from rules in the compiler and no
library can change them.

The consequence is that we're limited to what is present in the F# compiler, can't add a %Z or allow %0s to work.

Aren't you just replicating ksprintf ?

ksprintf allow you to run code on the final generated result, essentially allowing you to run code during
PrintfEnv.Finalize but you can't manipualte the format specifiers or their parameters.

What this Star syntax

When * is specified for either the width or the precision an additional parameter is taken by the format to get the
value.

>  sprintf "%*.*f";;
val it : (int -> int -> float -> string) = <fun:it@1>

A library to allow using printf style strings in more places.

The code is essentially an extracted version of printf.fs where the environement can not only decide
what to do with the final blocks that compose the string (printf put them on the console, sprintf in a buffer, ...)
but also what to do with the parameters passed for each format specifier.

Sample usage

module MyModule =
    open System.Text
    open BlackFox.MasterOfFoo
    type private MySprintfEnv() =
        inherit PrintfEnv<unit, string, string>()
        let buf = StringBuilder()
        override this.Finalize() = buf.ToString ()
        override this.Write(s : PrintableElement) = ignore(buf.Append(s.FormatAsPrintF()))
        override this.WriteT(s : string) = ignore(buf.Append(s))

    let mysprintf (format: Format<'T, unit, string, string>) =
        doPrintfFromEnv format (MySprintfEnv())

MyModule.mysprintf "Hello %s." "World"

Mini-Doc

PrintableElement

PrintableElement represent an element in a string, for example sprintf &quot;Foo %s bar&quot; &quot;x&quot; produce 3
PrintableElement, the first contains the string &quot;Foo &quot; the second is a format specifier &#39;s&#39; with an associated
string value &quot;x&quot; and then there is the string the string &quot; Bar&quot;.

Members :

  • ElementType: Tell your if this is a string or a format specifier.
  • Value: give the value if it was a format specifier.
  • ValueType: give the type of value expected by the format specifier.
  • StarWidth: The width if specified via another parameter as in "%*i".
  • StarPrecision: The precision if specified via another parameter as in "%.*f".
  • FormatAsPrintF(): Get the string representation that printf would have normally generated.
  • Specifier: The format specification for format specifiers.

PrintfEnv

PrintfEnv is the type to implement to create a printf variant it has 3 type parameters:

  • &#39;State: The state of the printer, passed as argument when using '%t'.
  • &#39;Residue: The type that methods passed to '%t' must return.
  • &#39;Result: The final result type for the printer.

Members:

  • Finalize: Create the final result for this printer
  • Write: Write an element from the format string to the printer
  • WriteT: Write the result of the method provided by %t to the printer.

Functions

  • doPrintfFromEnv: Take a format and a PrintfEnv to create a printf-like function
  • doPrintf: Same as doPrintfFromEnv but allow to know the number of elements when the PrintfEnv is created.

FAQ

What does it allow exactly that can't be done with the original set of functions ?

  • Generating complex object that aren't only a string like an SqlCommand or structured logging.
  • Escaping parts in strings, like an xmlprintf that would escape &lt; to &amp;lt in parameters but not in the format
    string.

What are the limitations ?

The main limitation is that the F# compiler allow a strict set of things an you can't go differently.
The function signature that is the first argument to Format&lt;_,_,_,_,&gt; is generated from rules in the compiler and no
library can change them.

The consequence is that we're limited to what is present in the F# compiler, can't add a %Z or allow %0s to work.

Aren't you just replicating ksprintf ?

ksprintf allow you to run code on the final generated result, essentially allowing you to run code during
PrintfEnv.Finalize but you can't manipualte the format specifiers or their parameters.

What this Star syntax

When * is specified for either the width or the precision an additional parameter is taken by the format to get the
value.

>  sprintf "%*.*f";;
val it : (int -> int -> float -> string) = <fun:it@1>

Release Notes

Remove ValueTuple from references

Version History

Version Downloads Last updated
1.0.3 60 9/9/2018
1.0.2 915 7/22/2018
1.0.1 113 2/11/2018
0.2.1 266 4/28/2017
0.2.0 198 10/23/2016
0.1.2 202 10/7/2016
0.1.1 134 10/7/2016