| Copyright | (c) 2016 Stephen Diehl (c) 2016-2018 Serokell (c) 2018-2021 Kowainik |
|---|---|
| License | MIT |
| Maintainer | Kowainik <xrom.xkov@gmail.com> |
| Stability | Stable |
| Portability | Portable |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Relude.String.Conversion
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
- type LText = Text
- type LByteString = ByteString
- class ConvertUtf8 a b where
- encodeUtf8 :: a -> b
- decodeUtf8 :: b -> a
- decodeUtf8Strict :: b -> Either UnicodeException a
- class ToText a where
- class ToLText a where
- class ToString a where
- class LazyStrict l s | l -> s, s -> l where
- fromLazy :: LazyStrict l s => l -> s
- fromStrict :: LazyStrict l s => s -> l
- readEither :: Read a => String -> Either Text a
- show :: forall b a. (Show a, IsString b) => a -> b
Convenient type aliases
type LByteString = ByteString Source #
Type synonym for ByteString.
Conversion type classes
class ConvertUtf8 a b where Source #
Type class for conversion to utf8 representation of text.
Methods
encodeUtf8 :: a -> b Source #
Encode as utf8 string (usually ByteString).
>>>encodeUtf8 @Text @ByteString "патак""\208\191\208\176\209\130\208\176\208\186"
decodeUtf8 :: b -> a Source #
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 Source #
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
Type class for converting other strings to Text.
Instances
| ToText String Source # | |
| EncodingError ToText "ShortByteString" "Text" => ToText ShortByteString Source # | ⚠️CAUTION⚠️ This instance is for custom error display only. You should always specify encoding of bytes explicitly. In case it is used by mistake, the user will see the following:
Since: 0.6.0.0 |
Defined in Relude.String.Conversion Methods toText :: ShortByteString -> Text Source # | |
| EncodingError ToText "ByteString" "Text" => ToText ByteString Source # | ⚠️CAUTION⚠️ This instance is for custom error display only. You should always specify encoding of bytes explicitly. In case it is used by mistake, the user will see the following:
Since: 0.6.0.0 |
Defined in Relude.String.Conversion Methods toText :: ByteString -> Text Source # | |
| ToText Text Source # | |
| EncodingError ToText "LByteString" "Text" => ToText LByteString Source # | ⚠️CAUTION⚠️ This instance is for custom error display only. You should always specify encoding of bytes explicitly. In case it is used by mistake, the user will see the following:
Since: 0.6.0.0 |
Defined in Relude.String.Conversion Methods toText :: LByteString -> Text Source # | |
| ToText LText Source # | |
class ToLText a where Source #
Type class for converting other strings to Text.
Instances
| ToLText String Source # | |
| EncodingError ToLText "ShortByteString" "LText" => ToLText ShortByteString Source # | ⚠️CAUTION⚠️ This instance is for custom error display only. You should always specify encoding of bytes explicitly. In case it is used by mistake, the user will see the following:
Since: 0.6.0.0 |
Defined in Relude.String.Conversion Methods toLText :: ShortByteString -> LText Source # | |
| EncodingError ToLText "ByteString" "LText" => ToLText ByteString Source # | ⚠️CAUTION⚠️ This instance is for custom error display only. You should always specify encoding of bytes explicitly. In case it is used by mistake, the user will see the following:
Since: 0.6.0.0 |
Defined in Relude.String.Conversion Methods toLText :: ByteString -> LText Source # | |
| ToLText Text Source # | |
| ToLText Text Source # | |
| EncodingError ToLText "LByteString" "LText" => ToLText LByteString Source # | ⚠️CAUTION⚠️ This instance is for custom error display only. You should always specify encoding of bytes explicitly. In case it is used by mistake, the user will see the following:
Since: 0.6.0.0 |
Defined in Relude.String.Conversion Methods toLText :: LByteString -> LText Source # | |
class ToString a where Source #
Type class for converting other strings to String.
Instances
| ToString String Source # | |
| EncodingError ToString "ShortByteString" "String" => ToString ShortByteString Source # | ⚠️CAUTION⚠️ This instance is for custom error display only. You should always specify encoding of bytes explicitly. In case it is used by mistake, the user will see the following:
Since: 0.6.0.0 |
Defined in Relude.String.Conversion Methods toString :: ShortByteString -> String Source # | |
| EncodingError ToString "ByteString" "String" => ToString ByteString Source # | ⚠️CAUTION⚠️ This instance is for custom error display only. You should always specify encoding of bytes explicitly. In case it is used by mistake, the user will see the following:
Since: 0.6.0.0 |
Defined in Relude.String.Conversion Methods toString :: ByteString -> String Source # | |
| ToString Text Source # | |
| EncodingError ToString "LByteString" "String" => ToString LByteString Source # | ⚠️CAUTION⚠️ This instance is for custom error display only. You should always specify encoding of bytes explicitly. In case it is used by mistake, the user will see the following:
Since: 0.6.0.0 |
Defined in Relude.String.Conversion Methods toString :: LByteString -> String Source # | |
| ToString LText Source # | |
class LazyStrict l s | l -> s, s -> l where Source #
Type class for lazy-strict conversions.
Since: 0.1.0
Instances
fromLazy :: LazyStrict l s => l -> s Source #
Alias for toStrict function.
fromStrict :: LazyStrict l s => s -> l Source #
Alias for toLazy function.
Show and read functions
readEither :: Read a => String -> Either Text a Source #
Version of readEither that returns Text in case of the parse
error.
>>>readEither @Int "123"Right 123>>>readEither @Int "aa"Left "Prelude.read: no parse"
show :: forall b a. (Show a, IsString b) => a -> b Source #
Generalized version of show. Unlike show this function
is polymorphic in its result type. This makes it more convenient to work with
data types like Text or ByteString. However, if you
pass the result of show to a function that expects polymorphic argument, this
can break type inference, so use -XTypeApplications to specify the textual
type explicitly.
>>>show (42 :: Int)"42">>>show (42 :: Double)"42.0">>>print (show @Text True)"True"