MyNixOS website logo
Description

Full-weight string formatting library, analog of Python's string.format.

This package contains full-featured string formatting function, similar to Python's string.format. Features include: * Automatically numbered variable placeholders; * Positional variable placeholders; * Named variable placeholders; * Placeholders can be used in any order; one variable can be used several times or not used at all.

  • Specific format can be used for each variable substitution. This package prefers functionality over "light weight" and (probably) performance. It also exposes all required interfaces to extend and customize it. For more details, please refer to Wiki. See also the examples/ directory.

text-format-heavy README

Build Status

This is Haskell string formatting library, which prefers functionality and extendability over light weight and (probably, in some cases) performance. This library is more or less analog of Python's string.format function, and has similar syntax. It also exposes all required interfaces to extend and customize it.

Most notable features are:

  • Automatically numbered variable placeholders ({});
  • Positional variable placeholders ({1});
  • Named variable placeholders ({name});
  • Placeholders can be used in any order; one variable can be used several times or not used at all.
  • Specific format can be used for each variable substitution ({0:+8.4}).

Formatting strings are present by Format type. Values of this type can be parsed from lazy Text, or can be entered as string literals, since Format implements IsString.

There are two syntaxes of formatting strings defined by this package:

  • Default Python-like syntax, which is generally described as "anything in braces is a variable substitution". instance IsString Format uses this syntax.
  • Alternative Shell-like syntax, which is generally described as "anything after dollar sign is a variable substitution".

It is possible to implement custom syntaxes of format strings: you just need to parse instances of Format type from some sort of strings.

The format function takes a Format specification and a container with variables. Container types are generalized by VarContainer type class. Standard container implementations include:

  • Single type for case when you need to pass only one variable.
  • Tuples and lists. These contain numbered variables, i.e. {0}, {1}, etc.
  • [(Text, a)] and Map Text a. These contain named variables, i.e. {name}.

One can implement custom variable containers, for example some record types.

Types of variables that can be used for subsitiution are generalized by Formatable type class. Each implementation defines default value formatting rules, and a syntax of variable format specification. For example, for integers, floats and strings, python-like syntax is used. Standard set of variable types includes:

  • Integers (Int, Integer, Int8..64, Word8..64, others can be easily added);
  • Floats (Float and Double);
  • Strings (String, lazy and strict ByteString, lazy and strict Text);
  • Booleans;
  • Time/date values from Data.Time.
  • Any instance of Show type class can be used by packing it into Shown constructor.

One can implement custom variable types.

For examples, please refer to GitHub wiki and examples/ directory in this repo. There are also some examples in haddock documentation.

License: BSD3.

Metadata

Version

0.1.5.3

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows