* Add total ConvertText alternative to Conv * Explain ConvertText's meaning * Rename convertText to toS for compatibility * 0.3.0 Refactor (#111) * Begin work on 0.3 refactor * Remove old docs folder * Fix dodgy exports * 4.8 constraint for null and length * Guards for Semigroup export for base<4.9 * Fix Data.Bits exports * Guards for typeable exports * Remove unused pragmas * Update hlint.yaml * Use liftM because of pre-AMP quirkk * Data.Char exports * Rework Exception exports * Fix bounds * Adding Monad.Fail shim * Fix displayException export * Explicit GHC.Prim import for ancient ghc * Explicit raise# for base-4.7 * Flush legacy testing infrastructure * Update git location * Update README * Add Partial module * Add concurrency exports * Explicit GHC.Float exports * Bounds for threadWaitReadSTM, threadWaitWriteSTM, forkOSWithUnmask * Fix for weird undocumented underflowError weirdness * forkOSWithUnask for base>4.8 * Fix GHC.Real exports pre base-4.7 * Fix withMVarMasked for base-4.7 * Update nix derivation * Update derivation * Update derivation * Explicit functor exports * <$ export * Overwrite binary * Fix some missing exports * Update Changelog * Minor export fixes base<4.9 * Explicit exports * Export handler * Generate export lists for multiple versions (#114) * Generate export lists for #112 * Make export lists format OccName uniformly * Make compile on ancient GHC * Use Foldable.concat * Hacks to make ghc-7.6 API happy * liftIO shim * Fix sortOn warning * Update ChangeLog * Update base bounds * Export conventions * Update Haddocks * Document GHC magic
79 lines
3.0 KiB
Haskell
79 lines
3.0 KiB
Haskell
{-# LANGUAGE Trustworthy #-}
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
{-# LANGUAGE TypeSynonymInstances #-}
|
|
|
|
-- | An alternative to 'Protolude.ConvertText' that includes
|
|
-- partial conversions. Not re-exported by 'Protolude'.
|
|
module Protolude.Conv (
|
|
StringConv
|
|
, strConv
|
|
, toS
|
|
, toSL
|
|
, Leniency (Lenient, Strict)
|
|
) where
|
|
|
|
import Data.ByteString.Char8 as B
|
|
import Data.ByteString.Lazy.Char8 as LB
|
|
import Data.Text as T
|
|
import Data.Text.Encoding as T
|
|
import Data.Text.Encoding.Error as T
|
|
import Data.Text.Lazy as LT
|
|
import Data.Text.Lazy.Encoding as LT
|
|
|
|
import Protolude.Base
|
|
import Data.Eq (Eq)
|
|
import Data.Ord (Ord)
|
|
import Data.Function ((.), id)
|
|
import Data.String (String)
|
|
import Control.Applicative (pure)
|
|
|
|
data Leniency = Lenient | Strict
|
|
deriving (Eq,Show,Ord,Enum,Bounded)
|
|
|
|
class StringConv a b where
|
|
strConv :: Leniency -> a -> b
|
|
|
|
toS :: StringConv a b => a -> b
|
|
toS = strConv Strict
|
|
|
|
toSL :: StringConv a b => a -> b
|
|
toSL = strConv Lenient
|
|
|
|
instance StringConv String String where strConv _ = id
|
|
instance StringConv String B.ByteString where strConv _ = B.pack
|
|
instance StringConv String LB.ByteString where strConv _ = LB.pack
|
|
instance StringConv String T.Text where strConv _ = T.pack
|
|
instance StringConv String LT.Text where strConv _ = LT.pack
|
|
|
|
instance StringConv B.ByteString String where strConv _ = B.unpack
|
|
instance StringConv B.ByteString B.ByteString where strConv _ = id
|
|
instance StringConv B.ByteString LB.ByteString where strConv _ = LB.fromChunks . pure
|
|
instance StringConv B.ByteString T.Text where strConv = decodeUtf8T
|
|
instance StringConv B.ByteString LT.Text where strConv l = strConv l . LB.fromChunks . pure
|
|
|
|
instance StringConv LB.ByteString String where strConv _ = LB.unpack
|
|
instance StringConv LB.ByteString B.ByteString where strConv _ = B.concat . LB.toChunks
|
|
instance StringConv LB.ByteString LB.ByteString where strConv _ = id
|
|
instance StringConv LB.ByteString T.Text where strConv l = decodeUtf8T l . strConv l
|
|
instance StringConv LB.ByteString LT.Text where strConv = decodeUtf8LT
|
|
|
|
instance StringConv T.Text String where strConv _ = T.unpack
|
|
instance StringConv T.Text B.ByteString where strConv _ = T.encodeUtf8
|
|
instance StringConv T.Text LB.ByteString where strConv l = strConv l . T.encodeUtf8
|
|
instance StringConv T.Text LT.Text where strConv _ = LT.fromStrict
|
|
instance StringConv T.Text T.Text where strConv _ = id
|
|
|
|
instance StringConv LT.Text String where strConv _ = LT.unpack
|
|
instance StringConv LT.Text T.Text where strConv _ = LT.toStrict
|
|
instance StringConv LT.Text LT.Text where strConv _ = id
|
|
instance StringConv LT.Text LB.ByteString where strConv _ = LT.encodeUtf8
|
|
instance StringConv LT.Text B.ByteString where strConv l = strConv l . LT.encodeUtf8
|
|
|
|
decodeUtf8T :: Leniency -> B.ByteString -> T.Text
|
|
decodeUtf8T Lenient = T.decodeUtf8With T.lenientDecode
|
|
decodeUtf8T Strict = T.decodeUtf8With T.strictDecode
|
|
|
|
decodeUtf8LT :: Leniency -> LB.ByteString -> LT.Text
|
|
decodeUtf8LT Lenient = LT.decodeUtf8With T.lenientDecode
|
|
decodeUtf8LT Strict = LT.decodeUtf8With T.strictDecode
|