Z-Data-0.2.0.0: Array, vector and text
Copyright(c) Dong Han 2017-2019
LicenseBSD
Maintainerwinterland1989@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Z.Data.Text.ShowT

Description

This module re-exports some UTF8 compatible textual builders from Builder.

We also provide a faster alternative to Show class, i.e. ShowT, which can be deriving using Generic. For example to use ShowT class:


import qualified Z.Data.Text.ShowT as T

data Foo = Bar Bytes | Qux Text Int deriving Generic
                                    deriving anyclass T.ShowT

Synopsis

ShowT class

class ShowT a where Source #

A class similar to Show, serving the purpose that quickly convert a data type to a Text value.

You can use newtype or generic deriving to implement instance of this class quickly:

 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
 {-# LANGUAGE DeriveAnyClass #-}
 {-# LANGUAGE DeriveGeneric #-}
 {-# LANGUAGE DerivingStrategies #-}

 import GHC.Generics

 newtype FooInt = FooInt Int deriving (Generic)
                           deriving anyclass ShowT

> toText (FooInt 3)
> "FooInt 3"

 newtype FooInt = FooInt Int deriving (Generic)
                           deriving newtype ShowT

> toText (FooInt 3)
> "3"

Minimal complete definition

Nothing

Methods

toUTF8BuilderP :: Int -> a -> Builder () Source #

Convert data to Builder with precendence.

You should return a Builder writing in UTF8 encoding only, i.e.

Z.Data.Text.validateMaybe (Z.Data.Builder.buildBytes (toUTF8BuilderP p a)) /= Nothing

default toUTF8BuilderP :: (Generic a, GToText (Rep a)) => Int -> a -> Builder () Source #

Instances

Instances details
ShowT Bool Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Bool -> Builder () Source #

ShowT Char Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Char -> Builder () Source #

ShowT Double Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT Float Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Float -> Builder () Source #

ShowT Int Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Int -> Builder () Source #

ShowT Int8 Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Int8 -> Builder () Source #

ShowT Int16 Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Int16 -> Builder () Source #

ShowT Int32 Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Int32 -> Builder () Source #

ShowT Int64 Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Int64 -> Builder () Source #

ShowT Integer Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT Natural Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT Ordering Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT Word Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Word -> Builder () Source #

ShowT Word8 Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Word8 -> Builder () Source #

ShowT Word16 Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT Word32 Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT Word64 Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CallStack Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT () Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> () -> Builder () Source #

ShowT Version Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT ExitCode Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CChar Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> CChar -> Builder () Source #

ShowT CSChar Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CUChar Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CShort Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CUShort Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CInt Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> CInt -> Builder () Source #

ShowT CUInt Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> CUInt -> Builder () Source #

ShowT CLong Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> CLong -> Builder () Source #

ShowT CULong Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CLLong Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CULLong Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CBool Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> CBool -> Builder () Source #

ShowT CFloat Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CDouble Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CPtrdiff Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CSize Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> CSize -> Builder () Source #

ShowT CWchar Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CSigAtomic Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CClock Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CTime Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> CTime -> Builder () Source #

ShowT CUSeconds Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CSUSeconds Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CIntPtr Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CUIntPtr Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CIntMax Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT CUIntMax Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT Scientific Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT Text Source #

The escaping rules is same with Show instance: we reuse JSON escaping rules here, so it will be faster.

Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Text -> Builder () Source #

ShowT FlatIntSet Source # 
Instance details

Defined in Z.Data.Vector.FlatIntSet

ShowT RegexOpts Source # 
Instance details

Defined in Z.Data.Text.Regex

ShowT Regex Source # 
Instance details

Defined in Z.Data.Text.Regex

Methods

toUTF8BuilderP :: Int -> Regex -> Builder () Source #

ShowT Value Source # 
Instance details

Defined in Z.Data.JSON.Value

Methods

toUTF8BuilderP :: Int -> Value -> Builder () Source #

ShowT ConvertError Source # 
Instance details

Defined in Z.Data.JSON.Base

ShowT HexBytes Source # 
Instance details

Defined in Z.Data.Vector.Hex

ShowT Base64Bytes Source # 
Instance details

Defined in Z.Data.Vector.Base64

ShowT CBytes Source #

This instance provide UTF8 guarantee, illegal codepoints will be written as replacementChars.

Escaping rule is same with String.

Instance details

Defined in Z.Data.CBytes

ShowT a => ShowT [a] Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> [a] -> Builder () Source #

ShowT a => ShowT (Maybe a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Maybe a -> Builder () Source #

(ShowT a, Integral a) => ShowT (Ratio a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Ratio a -> Builder () Source #

ShowT (Ptr a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Ptr a -> Builder () Source #

ShowT (ForeignPtr a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT a => ShowT (Min a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Min a -> Builder () Source #

ShowT a => ShowT (Max a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Max a -> Builder () Source #

ShowT a => ShowT (First a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> First a -> Builder () Source #

ShowT a => ShowT (Last a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Last a -> Builder () Source #

ShowT a => ShowT (WrappedMonoid a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT a => ShowT (Identity a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Identity a -> Builder () Source #

ShowT a => ShowT (First a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> First a -> Builder () Source #

ShowT a => ShowT (Last a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Last a -> Builder () Source #

ShowT a => ShowT (Dual a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Dual a -> Builder () Source #

ShowT a => ShowT (NonEmpty a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> NonEmpty a -> Builder () Source #

(Prim a, ShowT a) => ShowT (PrimArray a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT a => ShowT (SmallArray a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT a => ShowT (Array a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Array a -> Builder () Source #

(Prim a, ShowT a) => ShowT (PrimVector a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

ShowT a => ShowT (Vector a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Vector a -> Builder () Source #

ShowT v => ShowT (FlatSet v) Source # 
Instance details

Defined in Z.Data.Vector.FlatSet

Methods

toUTF8BuilderP :: Int -> FlatSet v -> Builder () Source #

ShowT v => ShowT (FlatIntMap v) Source # 
Instance details

Defined in Z.Data.Vector.FlatIntMap

(ShowT a, ShowT b) => ShowT (Either a b) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Either a b -> Builder () Source #

(ShowT a, ShowT b) => ShowT (a, b) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> (a, b) -> Builder () Source #

HasResolution a => ShowT (Fixed a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Fixed a -> Builder () Source #

ShowT (Proxy a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Proxy a -> Builder () Source #

(PrimUnlifted a, ShowT a) => ShowT (UnliftedArray a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

(ShowT k, ShowT v) => ShowT (FlatMap k v) Source # 
Instance details

Defined in Z.Data.Vector.FlatMap

Methods

toUTF8BuilderP :: Int -> FlatMap k v -> Builder () Source #

(ShowT a, ShowT b, ShowT c) => ShowT (a, b, c) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> (a, b, c) -> Builder () Source #

ShowT a => ShowT (Const a b) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Const a b -> Builder () Source #

ShowT b => ShowT (Tagged a b) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Tagged a b -> Builder () Source #

(ShowT a, ShowT b, ShowT c, ShowT d) => ShowT (a, b, c, d) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> (a, b, c, d) -> Builder () Source #

(ShowT (f a), ShowT (g a)) => ShowT (Product f g a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Product f g a -> Builder () Source #

(ShowT (f a), ShowT (g a), ShowT a) => ShowT (Sum f g a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Sum f g a -> Builder () Source #

(ShowT a, ShowT b, ShowT c, ShowT d, ShowT e) => ShowT (a, b, c, d, e) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> (a, b, c, d, e) -> Builder () Source #

ShowT (f (g a)) => ShowT (Compose f g a) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> Compose f g a -> Builder () Source #

(ShowT a, ShowT b, ShowT c, ShowT d, ShowT e, ShowT f) => ShowT (a, b, c, d, e, f) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> (a, b, c, d, e, f) -> Builder () Source #

(ShowT a, ShowT b, ShowT c, ShowT d, ShowT e, ShowT f, ShowT g) => ShowT (a, b, c, d, e, f, g) Source # 
Instance details

Defined in Z.Data.Text.ShowT

Methods

toUTF8BuilderP :: Int -> (a, b, c, d, e, f, g) -> Builder () Source #

toText :: ShowT a => a -> Text Source #

Convert data to Text.

toString :: ShowT a => a -> String Source #

Convert data to String, faster show replacement.

toUTF8Builder :: ShowT a => a -> Builder () Source #

Convert data to Builder.

toUTF8Bytes :: ShowT a => a -> Bytes Source #

Convert data to Bytes in UTF8 encoding.

Basic UTF8 builders

escapeTextJSON :: Text -> Builder () Source #

Escape text using JSON string escaping rules and add double quotes, escaping rules:

   '\b':  "\b"
   '\f':  "\f"
   '\n':  "\n"
   '\r':  "\r"
   '\t':  "\t"
   '"':  "\""
   '\':  "\\"
   '/':  "\/"
   other chars <= 0x1F: "\u00XX"

stringUTF8 :: String -> Builder () Source #

Turn String into Builder with UTF8 encoding

Illegal codepoints will be written as replacementChars.

This is different from writing string literals builders via OverloadedStrings, because string literals do not provide UTF8 guarantees.

This function will be rewritten into a memcpy if possible, (running a fast UTF-8 validation at runtime first).

charUTF8 :: Char -> Builder () Source #

Turn Char into Builder with UTF8 encoding

Illegal codepoints will be written as replacementChars.

string7 :: String -> Builder () Source #

Turn String into Builder with ASCII7 encoding

Codepoints beyond 'x7F' will be chopped.

char7 :: Char -> Builder () Source #

Turn Char into Builder with ASCII7 encoding

Codepoints beyond 'x7F' will be chopped.

text :: Text -> Builder () Source #

Write UTF8 encoded Text using Builder.

Note, if you're trying to write string literals builders, please open OverloadedStrings and use Builders IsString instance, it will be rewritten into a memcpy.

Numeric builders

Integral type formatting

data IFormat Source #

Integral formatting options.

Constructors

IFormat 

Fields

Instances

Instances details
Eq IFormat Source # 
Instance details

Defined in Z.Data.Builder.Numeric

Methods

(==) :: IFormat -> IFormat -> Bool #

(/=) :: IFormat -> IFormat -> Bool #

Ord IFormat Source # 
Instance details

Defined in Z.Data.Builder.Numeric

Show IFormat Source # 
Instance details

Defined in Z.Data.Builder.Numeric

Arbitrary IFormat Source # 
Instance details

Defined in Z.Data.Builder.Numeric

CoArbitrary IFormat Source # 
Instance details

Defined in Z.Data.Builder.Numeric

Methods

coarbitrary :: IFormat -> Gen b -> Gen b #

defaultIFormat :: IFormat Source #

defaultIFormat = IFormat 0 NoPadding False

data Padding Source #

Instances

Instances details
Enum Padding Source # 
Instance details

Defined in Z.Data.Builder.Numeric

Eq Padding Source # 
Instance details

Defined in Z.Data.Builder.Numeric

Methods

(==) :: Padding -> Padding -> Bool #

(/=) :: Padding -> Padding -> Bool #

Ord Padding Source # 
Instance details

Defined in Z.Data.Builder.Numeric

Show Padding Source # 
Instance details

Defined in Z.Data.Builder.Numeric

Arbitrary Padding Source # 
Instance details

Defined in Z.Data.Builder.Numeric

CoArbitrary Padding Source # 
Instance details

Defined in Z.Data.Builder.Numeric

Methods

coarbitrary :: Padding -> Gen b -> Gen b #

int :: (Integral a, Bounded a) => a -> Builder () Source #

int = intWith defaultIFormat

intWith :: (Integral a, Bounded a) => IFormat -> a -> Builder () Source #

Format a Bounded Integral type like Int or Word16 into decimal ASCII digits.

import Z.Data.Builder as B
import Z.Data.Text    as T

> T.validate . B.buildBytes $ B.intWith defaultIFormat  (12345 :: Int)
"12345"
> T.validate . B.buildBytes $ B.intWith defaultIFormat{width=10, padding=RightSpacePadding} (12345 :: Int)
"12345     "
> T.validate . B.buildBytes $ B.intWith defaultIFormat{width=10, padding=ZeroPadding} (12345 :: Int)
"0000012345"

integer :: Integer -> Builder () Source #

Format a Integer into decimal ASCII digits.

Fixded size hexidecimal formatting

hex :: forall a. (FiniteBits a, Integral a) => a -> Builder () Source #

Format a FiniteBits Integral type into hex nibbles.

import Z.Data.Builder as B
import Z.Data.Text    as T
import Data.Word
import Data.Int

> T.validate . B.buildBytes $ B.hex (125 :: Int8)
"7d"
> T.validate . B.buildBytes $ B.hex (-1 :: Int8)
"ff"
> T.validate . B.buildBytes $ B.hex (125 :: Word16)
"007d"

hexUpper :: forall a. (FiniteBits a, Integral a) => a -> Builder () Source #

The UPPERCASED version of hex.

IEEE float formating

data FFormat Source #

Control the rendering of floating point numbers.

Constructors

Exponent

Scientific notation (e.g. 2.3e123).

Fixed

Standard decimal notation.

Generic

Use decimal notation for values between 0.1 and 9,999,999, and scientific notation otherwise.

double :: Double -> Builder () Source #

Decimal encoding of an IEEE Double.

Using standard decimal notation for arguments whose absolute value lies between 0.1 and 9,999,999, and scientific notation otherwise.

doubleWith Source #

Arguments

:: FFormat 
-> Maybe Int

Number of decimal places to render.

-> Double 
-> Builder () 

Format double-precision float using drisu3 with dragon4 fallback.

float :: Float -> Builder () Source #

Decimal encoding of an IEEE Float.

Using standard decimal notation for arguments whose absolute value lies between 0.1 and 9,999,999, and scientific notation otherwise.

floatWith Source #

Arguments

:: FFormat 
-> Maybe Int

Number of decimal places to render.

-> Float 
-> Builder () 

Format single-precision float using drisu3 with dragon4 fallback.

scientific :: Scientific -> Builder () Source #

A Builder which renders a scientific number to full precision, using standard decimal notation for arguments whose absolute value lies between 0.1 and 9,999,999, and scientific notation otherwise.

scientificWith Source #

Arguments

:: FFormat 
-> Maybe Int

Number of decimal places to render.

-> Scientific 
-> Builder () 

Like scientific but provides rendering options.

Helpers

paren :: Builder () -> Builder () Source #

add (...) to original builder.

curly :: Builder () -> Builder () Source #

add {...} to original builder.

square :: Builder () -> Builder () Source #

add [...] to original builder.

angle :: Builder () -> Builder () Source #

add <...> to original builder.

quotes :: Builder () -> Builder () Source #

add "..." to original builder.

squotes :: Builder () -> Builder () Source #

add '...' to original builder.

colon :: Builder () Source #

write an ASCII :

comma :: Builder () Source #

write an ASCII ,

intercalateVec Source #

Arguments

:: Vec v a 
=> Builder ()

the seperator

-> (a -> Builder ())

value formatter

-> v a

value vector

-> Builder () 

Use separator to connect a vector of builders.

import Z.Data.Builder as B
import Z.Data.Text    as T
import Z.Data.Vector  as V

> T.validate . B.build $ B.intercalateVec "," B.int (V.pack [1,2,3,4] :: V.PrimVector Int)
"1,2,3,4"

intercalateList Source #

Arguments

:: Builder ()

the seperator

-> (a -> Builder ())

value formatter

-> [a]

value list

-> Builder () 

Use separator to connect list of builders.

import Z.Data.Builder as B
import Z.Data.Text    as T
import Z.Data.Vector  as V

T.validate . B.build $ B.intercalateList "," B.int ([1,2,3,4] :: [Int])
"1,2,3,4"

parenWhen :: Bool -> Builder () -> Builder () Source #

Add "(..)" around builders when condition is met, otherwise add nothing.

This is useful when defining ShowT instances.