relude-0.5.0: Custom prelude from Kowainik

Copyright(c) 2016 Stephen Diehl
(c) 2016-2018 Serokell
(c) 2018-2019 Kowainik
LicenseMIT
MaintainerKowainik <xrom.xkov@gmail.com>
Safe HaskellSafe
LanguageHaskell2010

Relude.String.Conversion

Contents

Description

This module implements type class which allow to have conversion to and from Text, String and ByteString types (including both strict and lazy versions). Usually you need to export Text modules qualified and use pack / unpack functions to convert to/from Text. Now you can just use toText / toString functions.

Synopsis

Convenient type aliases

type LText = Text #

Type synonym for Text.

type LByteString = ByteString #

Type synonym for ByteString.

Conversion type classes

class ConvertUtf8 a b where #

Type class for conversion to utf8 representation of text.

Methods

encodeUtf8 :: a -> b #

Encode as utf8 string (usually ByteString).

>>> encodeUtf8 @Text @ByteString "патак"
"\208\191\208\176\209\130\208\176\208\186"

decodeUtf8 :: b -> a #

Decode from utf8 string.

>>> decodeUtf8 @Text @ByteString "\208\191\208\176\209\130\208\176\208\186"
"\1087\1072\1090\1072\1082"
>>> putTextLn $ decodeUtf8 @Text @ByteString "\208\191\208\176\209\130\208\176\208\186"
патак

decodeUtf8Strict :: b -> Either UnicodeException a #

Decode as utf8 string but returning execption if byte sequence is malformed.

>>> decodeUtf8 @Text @ByteString "\208\208\176\209\130\208\176\208\186"
"\65533\1072\1090\1072\1082"
>>> decodeUtf8Strict @Text @ByteString "\208\208\176\209\130\208\176\208\186"
Left Cannot decode byte '\xd0': Data.Text.Internal.Encoding.decodeUtf8: Invalid UTF-8 stream
Instances
ConvertUtf8 String ByteString # 
Instance details

Defined in Relude.String.Conversion

ConvertUtf8 String ByteString #

Converting String to ByteString might be a slow operation. Consider using lazy bytestring at first place.

Instance details

Defined in Relude.String.Conversion

Methods

encodeUtf8 :: String -> ByteString #

decodeUtf8 :: ByteString -> String #

decodeUtf8Strict :: ByteString -> Either UnicodeException String #

ConvertUtf8 Text ByteString # 
Instance details

Defined in Relude.String.Conversion

ConvertUtf8 Text ByteString # 
Instance details

Defined in Relude.String.Conversion

Methods

encodeUtf8 :: Text -> ByteString #

decodeUtf8 :: ByteString -> Text #

decodeUtf8Strict :: ByteString -> Either UnicodeException Text #

ConvertUtf8 Text ByteString # 
Instance details

Defined in Relude.String.Conversion

ConvertUtf8 Text ByteString # 
Instance details

Defined in Relude.String.Conversion

Methods

encodeUtf8 :: Text -> ByteString #

decodeUtf8 :: ByteString -> Text #

decodeUtf8Strict :: ByteString -> Either UnicodeException Text #

class ToString a where #

Type class for converting other strings to String.

Methods

toString :: a -> String #

Instances
ToString String # 
Instance details

Defined in Relude.String.Conversion

Methods

toString :: String -> String #

ToString Text # 
Instance details

Defined in Relude.String.Conversion

Methods

toString :: Text -> String #

ToString Text # 
Instance details

Defined in Relude.String.Conversion

Methods

toString :: Text -> String #

class ToLText a where #

Type class for converting other strings to Text.

Methods

toLText :: a -> Text #

Instances
ToLText String # 
Instance details

Defined in Relude.String.Conversion

Methods

toLText :: String -> Text #

ToLText Text # 
Instance details

Defined in Relude.String.Conversion

Methods

toLText :: Text -> Text #

ToLText Text # 
Instance details

Defined in Relude.String.Conversion

Methods

toLText :: Text -> Text0 #

class ToText a where #

Type class for converting other strings to Text.

Methods

toText :: a -> Text #

Instances
ToText String # 
Instance details

Defined in Relude.String.Conversion

Methods

toText :: String -> Text #

ToText Text # 
Instance details

Defined in Relude.String.Conversion

Methods

toText :: Text -> Text0 #

ToText Text # 
Instance details

Defined in Relude.String.Conversion

Methods

toText :: Text -> Text #

class LazyStrict l s | l -> s, s -> l where #

Type class for lazy-strict conversions.

Methods

toLazy :: s -> l #

toStrict :: l -> s #

fromLazy :: LazyStrict l s => l -> s #

Alias for toStrict function.

fromStrict :: LazyStrict l s => s -> l #

Alias for toLazy function.

Show and read functions

readEither :: (ToString a, Read b) => a -> Either Text b #

Polymorhpic version of readEither.

>>> readEither @Text @Int "123"
Right 123
>>> readEither @Text @Int "aa"
Left "Prelude.read: no parse"

show :: forall b a. (Show a, IsString b) => a -> b #

Generalized version of show.