#ifdef TRUSTWORTHY
#endif
module Data.Text.Lazy.Encoding.Locale
(decodeLocale
,encodeLocale
,decodeLocale'
,encodeLocale'
,decodeFromHandle
,encodeFromHandle
) where
import Import (getLocale'
,whenJust'
,hGetEncAndNlMode')
import qualified Data.ByteString.Lazy as L
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TLIO
import qualified Data.Text.Lazy.Encoding as TLE
import System.IO
import Data.ByteString.Handle
import qualified Data.List as L
import Data.Maybe
handleDecoder :: Maybe TextEncoding -> Maybe NewlineMode -> L.ByteString -> IO TL.Text
handleDecoder menc mnlmode = \bs -> do
h <- readHandle False bs
whenJust' menc $ hSetEncoding h
whenJust' mnlmode $ hSetNewlineMode h
TLIO.hGetContents h
handleEncoder :: Maybe TextEncoding -> Maybe NewlineMode -> TL.Text -> IO L.ByteString
handleEncoder menc mnlmode = \t -> do
(res, ()) <- writeHandle False $ \h -> do
whenJust' menc $ hSetEncoding h
whenJust' mnlmode $ hSetNewlineMode h
TLIO.hPutStr h t
return res
chooseDecoder :: Maybe TextEncoding -> Maybe NewlineMode -> L.ByteString -> IO TL.Text
chooseDecoder menc mnlmode = \bs -> do
if inputNL nlmode == LF
then do
enc <- maybe getLocale' return menc
case L.stripPrefix "UTF-" (show enc) of
Just s ->
case s of
"8" -> return (TLE.decodeUtf8 bs)
('1':'6':x:'E':_)
| 'L' == x -> return (TLE.decodeUtf16LE bs)
| 'B' == x -> return (TLE.decodeUtf16BE bs)
('3':'2':x:'E':_)
| 'L' == x -> return (TLE.decodeUtf32LE bs)
| 'B' == x -> return (TLE.decodeUtf32BE bs)
_ -> fallback bs
_ -> fallback bs
else
fallback bs
where
nlmode = fromMaybe nativeNewlineMode mnlmode
fallback = handleDecoder menc mnlmode
chooseEncoder :: Maybe TextEncoding -> Maybe NewlineMode -> TL.Text -> IO L.ByteString
chooseEncoder menc mnlmode = \bs ->
if outputNL nlmode == LF
then do
enc <- maybe getLocale' return menc
case L.stripPrefix "UTF-" (show enc) of
Just s ->
case s of
"8" -> return (TLE.encodeUtf8 bs)
('1':'6':x:'E':_)
| 'L' == x -> return (TLE.encodeUtf16LE bs)
| 'B' == x -> return (TLE.encodeUtf16BE bs)
('3':'2':x:'E':_)
| 'L' == x -> return (TLE.encodeUtf32LE bs)
| 'B' == x -> return (TLE.encodeUtf32BE bs)
_ -> fallback bs
_ -> fallback bs
else
fallback bs
where
nlmode = fromMaybe nativeNewlineMode mnlmode
fallback = handleEncoder menc mnlmode
decodeLocale :: L.ByteString -> IO TL.Text
decodeLocale = chooseDecoder Nothing Nothing
encodeLocale :: TL.Text -> IO L.ByteString
encodeLocale = chooseEncoder Nothing Nothing
decodeLocale' :: TextEncoding -> NewlineMode -> L.ByteString -> IO TL.Text
decodeLocale' enc nlmode = chooseDecoder (Just enc) (Just nlmode)
encodeLocale' :: TextEncoding -> NewlineMode -> TL.Text -> IO L.ByteString
encodeLocale' enc nlmode = chooseEncoder (Just enc) (Just nlmode)
decodeFromHandle :: Handle -> L.ByteString -> IO TL.Text
decodeFromHandle h bs = do
(enc, nlmode) <- hGetEncAndNlMode' h
decodeLocale' enc nlmode bs
encodeFromHandle :: Handle -> TL.Text -> IO L.ByteString
encodeFromHandle h t = do
(enc, nlmode) <- hGetEncAndNlMode' h
encodeLocale' enc nlmode t