HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Moduletext-builder-dev-0.4.0.1Haskell2010

TextBuilderDev

  • 1 type
  • 1 class
  • 31 values
datadata TextBuilder
#

Composable specification of how to efficiently construct strict Text.

Provides instances of Semigroup and Monoid, which have complexity of O(1).

Instances7Eq, Show, IsString, Semigroup, Monoid, Arbitrary, …

Accessors

3 declarations

Constructors

0 declarations

Transformations

Run the builder and pack the produced text into a new builder.

Useful to have around builders that you reuse, because a forced builder is much faster, since it's virtually a single call to memcopy.

Pad a builder from the left side to the specified length with the specified character.

Example1 expression
padFromLeft 5 '0' "123""00123"
Example1 expression
padFromLeft 5 '0' "123456""123456"

Pad a builder from the right side to the specified length with the specified character.

Example1 expression
padFromRight 5 ' ' "123""123  "
Example1 expression
padFromRight 5 ' ' "123456""123456"

Textual

UTF-8 bytestring. You can use it for converting ASCII values as well.

Warning: It's your responsibility to ensure that the bytestring is properly encoded.

Example1 expression
unsafeUtf8ByteString "abc""abc"
Example2 expressions
import Data.Text.Encoding (encodeUtf8)unsafeUtf8ByteString (encodeUtf8 "фывапролдж") == "фывапролдж"True

Character

Safe Unicode codepoint with invalid values replaced by the � char (codepoint 0xfffd), which is the same as what Data.Text.pack does.

Data

Integers

Decimal

valuedecimal :: Integral a => a -> TextBuilder
#

Signed decimal representation of an integer.

Example1 expression
decimal 123456"123456"
Example1 expression
decimal (-123456)"-123456"
Example1 expression
decimal 0"0"
Example1 expression
decimal (-2 :: Int8)"-2"
Example1 expression
decimal (-128 :: Int8)"-128"
valuefixedLengthDecimal :: Integral a => Int -> a -> TextBuilder
#

Fixed-length decimal without sign. Padded with zeros or trimmed depending on whether it's shorter or longer than specified.

Example1 expression
fixedLengthDecimal 5 123"00123"
Example1 expression
fixedLengthDecimal 5 123456"23456"
Example1 expression
fixedLengthDecimal 5 (-123456)"23456"
Example1 expression
fixedLengthDecimal 7 (-123456)"0123456"
Example1 expression
fixedLengthDecimal 0 123""
Example1 expression
fixedLengthDecimal (-2) 123""

Decimal representation of an integral value with thousands separated by the specified character.

Example1 expression
thousandSeparatedDecimal ',' 1234567890"1,234,567,890"
Example1 expression
thousandSeparatedDecimal ' ' (-1234567890)"-1 234 567 890"

Binary

valuebinary :: FiniteBits a => a -> TextBuilder
#

Two's complement binary representation of a value.

Bits of a statically sized value padded from the left according to the size. If it's a negatable integer, the sign is reflected in the bits.

Example1 expression
binary @Int8 0"00000000"
Example1 expression
binary @Int8 4"00000100"
Example1 expression
binary @Int8 (-1)"11111111"
Example1 expression
binary @Word8 255"11111111"
Example1 expression
binary @Int16 4"0000000000000100"
Example1 expression
binary @Int16 (-4)"1111111111111100"

Octal

valueoctal :: (FiniteBits a, Integral a) => a -> TextBuilder
#

Octal representation of an integer.

Example1 expression
octal @Int32 123456"00000361100"
Example1 expression
octal @Int32 (-123456)"77777416700"

Hexadecimal

valuehexadecimal :: (FiniteBits a, Integral a) => a -> TextBuilder
#

Integer in hexadecimal notation with a fixed number of digits determined by the size of the type.

Example1 expression
hexadecimal @Int8 0"00"
Example1 expression
hexadecimal @Int8 4"04"
Example1 expression
hexadecimal @Int8 (-128)"80"
Example1 expression
hexadecimal @Int8 (-1)"ff"
Example1 expression
hexadecimal @Word8 255"ff"
Example1 expression
hexadecimal @Int32 123456"0001e240"
Example1 expression
hexadecimal @Int32 (-123456)"fffe1dc0"

Real

valuedoubleFixedPoint
  1. :: Int

    Amount of decimals after point.

  2. -> Double
  3. -> TextBuilder
#

Double with a fixed number of decimal places.

Example1 expression
doubleFixedPoint 4 0.123456"0.1235"
Example1 expression
doubleFixedPoint 2 2.1"2.10"
Example1 expression
doubleFixedPoint (-2) 2.1"2"
Example1 expression
doubleFixedPoint 2 (-2.1)"-2.10"
Example1 expression
doubleFixedPoint 2 0"0.00"
valuedoubleFixedPointPercent
  1. :: Int

    Amount of decimals after point.

  2. -> Double
  3. -> TextBuilder
#

Double multiplied by 100 with a fixed number of decimal places applied and followed by a percent-sign.

Example1 expression
doubleFixedPointPercent 3 0.123456"12.346%"
Example1 expression
doubleFixedPointPercent 0 2"200%"
Example1 expression
doubleFixedPointPercent 0 (-2)"-200%"

Time

valuerealFracDdHhMmSsInterval :: RealFrac seconds => seconds -> TextBuilder
#

Time interval in seconds. Directly applicable to DiffTime and NominalDiffTime.

The format is the following:

DD:HH:MM:SS
Example1 expression
realFracDdHhMmSsInterval @Double 59"00:00:00:59"
Example1 expression
realFracDdHhMmSsInterval @Double 90"00:00:01:30"
Example1 expression
realFracDdHhMmSsInterval @Double 86401"01:00:00:01"
Example1 expression
realFracDdHhMmSsInterval @Double (356 * 86400)"356:00:00:00"

Amount of picoseconds represented in a compact decimal format using suffixes.

E.g., the following is 1_230_000_000 picoseconds or 1.23 milliseconds or 1230 microseconds:

1230us

Other

Data size in decimal notation over amount of bytes.

Example1 expression
approximateDataSize 999"999B"
Example1 expression
approximateDataSize 9999"9.9kB"
Example1 expression
approximateDataSize (-9999)"-9.9kB"
Example1 expression
approximateDataSize 1234567890"1.2GB"
Example1 expression
approximateDataSize 10000000000000000000000000000000023"10,000,000,000YB"

Classes

1 declaration
classclass Isomorphic a where
#

Evidence that there exists an unambiguous way to convert a type to and from TextBuilder.

The laws are:

This class does not provide implicit rendering, such as from integer to its decimal representation. There are multiple ways of representing an integer as text (e.g., hexadecimal, binary). The non-ambiguity is further enforced by the presence of the inverse conversion. In the integer case there is no way to read it from a textual form without a possibility of failing (e.g., when the input string cannot be parsed as an integer).

Methods

Instances5Isomorphic