{-# LANGUAGE CPP #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Universum.String.Conversion
(
LText
, LByteString
, ConvertUtf8 (..)
, ToString (..)
, ToLText (..)
, ToText (..)
, readEither
, show
) where
import Data.Bifunctor (first)
import Data.Either (Either)
import Data.Function (id, (.))
import Data.String (String)
import qualified Data.Text.Internal as T
import qualified Data.Text.Internal.Fusion.Common as TF
import Universum.Functor ((<$>))
import Universum.String.Reexport (ByteString, IsString, Read, Text, fromString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB
import qualified Data.ByteString.Lazy.UTF8 as LBU
import qualified Data.ByteString.UTF8 as BU
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Encoding.Error as T
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Encoding as LT
import qualified Text.Read (readEither)
import qualified GHC.Show as Show (Show (show))
type LText = LT.Text
type LByteString = LB.ByteString
class ConvertUtf8 a b where
encodeUtf8 :: a -> b
decodeUtf8 :: b -> a
decodeUtf8Strict :: b -> Either T.UnicodeException a
instance ConvertUtf8 String B.ByteString where
encodeUtf8 :: String -> ByteString
encodeUtf8 = String -> ByteString
BU.fromString
decodeUtf8 :: ByteString -> String
decodeUtf8 = ByteString -> String
BU.toString
decodeUtf8Strict :: ByteString -> Either UnicodeException String
decodeUtf8Strict = (Text -> String
T.unpack (Text -> String)
-> Either UnicodeException Text -> Either UnicodeException String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) (Either UnicodeException Text -> Either UnicodeException String)
-> (ByteString -> Either UnicodeException Text)
-> ByteString
-> Either UnicodeException String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
forall a b. ConvertUtf8 a b => b -> Either UnicodeException a
decodeUtf8Strict
instance ConvertUtf8 T.Text B.ByteString where
encodeUtf8 :: Text -> ByteString
encodeUtf8 = Text -> ByteString
T.encodeUtf8
decodeUtf8 :: ByteString -> Text
decodeUtf8 = OnDecodeError -> ByteString -> Text
T.decodeUtf8With OnDecodeError
T.lenientDecode
decodeUtf8Strict :: ByteString -> Either UnicodeException Text
decodeUtf8Strict = ByteString -> Either UnicodeException Text
T.decodeUtf8'
instance ConvertUtf8 LT.Text B.ByteString where
encodeUtf8 :: Text -> ByteString
encodeUtf8 = ByteString -> ByteString
LB.toStrict (ByteString -> ByteString)
-> (Text -> ByteString) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8
decodeUtf8 :: ByteString -> Text
decodeUtf8 = OnDecodeError -> ByteString -> Text
LT.decodeUtf8With OnDecodeError
T.lenientDecode (ByteString -> Text)
-> (ByteString -> ByteString) -> ByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
LB.fromStrict
decodeUtf8Strict :: ByteString -> Either UnicodeException Text
decodeUtf8Strict = ByteString -> Either UnicodeException Text
forall a b. ConvertUtf8 a b => b -> Either UnicodeException a
decodeUtf8Strict (ByteString -> Either UnicodeException Text)
-> (ByteString -> ByteString)
-> ByteString
-> Either UnicodeException Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
LB.fromStrict
instance ConvertUtf8 String LB.ByteString where
encodeUtf8 :: String -> ByteString
encodeUtf8 = String -> ByteString
LBU.fromString
decodeUtf8 :: ByteString -> String
decodeUtf8 = ByteString -> String
LBU.toString
decodeUtf8Strict :: ByteString -> Either UnicodeException String
decodeUtf8Strict = (Text -> String
T.unpack (Text -> String)
-> Either UnicodeException Text -> Either UnicodeException String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) (Either UnicodeException Text -> Either UnicodeException String)
-> (ByteString -> Either UnicodeException Text)
-> ByteString
-> Either UnicodeException String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
forall a b. ConvertUtf8 a b => b -> Either UnicodeException a
decodeUtf8Strict
instance ConvertUtf8 T.Text LB.ByteString where
encodeUtf8 :: Text -> ByteString
encodeUtf8 = ByteString -> ByteString
LB.fromStrict (ByteString -> ByteString)
-> (Text -> ByteString) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8
decodeUtf8 :: ByteString -> Text
decodeUtf8 = OnDecodeError -> ByteString -> Text
T.decodeUtf8With OnDecodeError
T.lenientDecode (ByteString -> Text)
-> (ByteString -> ByteString) -> ByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
LB.toStrict
decodeUtf8Strict :: ByteString -> Either UnicodeException Text
decodeUtf8Strict = ByteString -> Either UnicodeException Text
T.decodeUtf8' (ByteString -> Either UnicodeException Text)
-> (ByteString -> ByteString)
-> ByteString
-> Either UnicodeException Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
LB.toStrict
instance ConvertUtf8 LT.Text LB.ByteString where
encodeUtf8 :: Text -> ByteString
encodeUtf8 = Text -> ByteString
LT.encodeUtf8
decodeUtf8 :: ByteString -> Text
decodeUtf8 = OnDecodeError -> ByteString -> Text
LT.decodeUtf8With OnDecodeError
T.lenientDecode
decodeUtf8Strict :: ByteString -> Either UnicodeException Text
decodeUtf8Strict = ByteString -> Either UnicodeException Text
LT.decodeUtf8'
class ToText a where
toText :: a -> T.Text
instance ToText String where
toText :: String -> Text
toText = String -> Text
T.pack
instance ToText T.Text where
toText :: Text -> Text
toText = Text -> Text
forall a. a -> a
id
instance ToText LT.Text where
toText :: Text -> Text
toText = Text -> Text
LT.toStrict
class ToLText a where
toLText :: a -> LT.Text
instance ToLText String where
toLText :: String -> Text
toLText = String -> Text
LT.pack
instance ToLText T.Text where
toLText :: Text -> Text
toLText = Text -> Text
LT.fromStrict
instance ToLText LT.Text where
toLText :: Text -> Text
toLText = Text -> Text
forall a. a -> a
id
class ToString a where
toString :: a -> String
instance ToString String where
toString :: String -> String
toString = String -> String
forall a. a -> a
id
instance ToString T.Text where
toString :: Text -> String
toString = Text -> String
T.unpack
instance ToString LT.Text where
toString :: Text -> String
toString = Text -> String
LT.unpack
{-# RULES "pack/unpack" [~0]
forall s. T.unpack (T.pack s) = s
#-}
{-# RULES "pack/unpack internal" [1]
forall s. TF.unstreamList (TF.map T.safe (TF.streamList s)) = s
#-}
{-# RULES "unpack/pack" [~0]
forall s. T.pack (T.unpack s) = s
#-}
readEither :: (ToString a, Read b) => a -> Either Text b
readEither :: a -> Either Text b
readEither = (String -> Text) -> Either String b -> Either Text b
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first String -> Text
forall a. ToText a => a -> Text
toText (Either String b -> Either Text b)
-> (a -> Either String b) -> a -> Either Text b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either String b
forall a. Read a => String -> Either String a
Text.Read.readEither (String -> Either String b)
-> (a -> String) -> a -> Either String b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. ToString a => a -> String
toString
show :: forall b a . (Show.Show a, IsString b) => a -> b
show :: a -> b
show a
x = String -> b
forall a. IsString a => String -> a
fromString (a -> String
forall a. Show a => a -> String
Show.show a
x)
{-# SPECIALIZE show :: Show.Show a => a -> Text #-}
{-# SPECIALIZE show :: Show.Show a => a -> LText #-}
{-# SPECIALIZE show :: Show.Show a => a -> ByteString #-}
{-# SPECIALIZE show :: Show.Show a => a -> LByteString #-}
{-# SPECIALIZE show :: Show.Show a => a -> String #-}