{-# LANGUAGE CPP #-}
module Darcs.Util.Encoding
( encode, decode
, encodeUtf8, decodeUtf8
) where
import Darcs.Prelude
import qualified Data.ByteString as B
import GHC.IO.Encoding ( TextEncoding, mkTextEncoding )
import GHC.Foreign ( withCStringLen, peekCStringLen )
#ifdef WIN32
import Darcs.Util.Encoding.Win32 ( encode, decode )
#else
import GHC.IO.Encoding ( getFileSystemEncoding )
encode :: String -> IO B.ByteString
encode :: String -> IO ByteString
encode String
s = IO TextEncoding
getFileSystemEncoding IO TextEncoding -> (TextEncoding -> IO ByteString) -> IO ByteString
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> TextEncoding -> IO ByteString
textEncode String
s
decode :: B.ByteString -> IO String
decode :: ByteString -> IO String
decode ByteString
bs = IO TextEncoding
getFileSystemEncoding IO TextEncoding -> (TextEncoding -> IO String) -> IO String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> TextEncoding -> IO String
textDecode ByteString
bs
#endif
encodeUtf8 :: String -> IO B.ByteString
encodeUtf8 :: String -> IO ByteString
encodeUtf8 String
s = String -> IO TextEncoding
mkTextEncoding String
"UTF-8//TRANSLIT" IO TextEncoding -> (TextEncoding -> IO ByteString) -> IO ByteString
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> TextEncoding -> IO ByteString
textEncode String
s
decodeUtf8 :: B.ByteString -> IO String
decodeUtf8 :: ByteString -> IO String
decodeUtf8 ByteString
bs = String -> IO TextEncoding
mkTextEncoding String
"UTF-8//TRANSLIT" IO TextEncoding -> (TextEncoding -> IO String) -> IO String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> TextEncoding -> IO String
textDecode ByteString
bs
textEncode :: String -> TextEncoding -> IO B.ByteString
textEncode :: String -> TextEncoding -> IO ByteString
textEncode String
s TextEncoding
enc = TextEncoding
-> String -> (CStringLen -> IO ByteString) -> IO ByteString
forall a. TextEncoding -> String -> (CStringLen -> IO a) -> IO a
withCStringLen TextEncoding
enc String
s CStringLen -> IO ByteString
B.packCStringLen
textDecode :: B.ByteString -> TextEncoding -> IO String
textDecode :: ByteString -> TextEncoding -> IO String
textDecode ByteString
bs TextEncoding
enc = ByteString -> (CStringLen -> IO String) -> IO String
forall a. ByteString -> (CStringLen -> IO a) -> IO a
B.useAsCStringLen ByteString
bs (TextEncoding -> CStringLen -> IO String
peekCStringLen TextEncoding
enc)