module Bio.Util.Text
( Unpack(..)
, w2c, c2w
, decodeBytes
, encodeBytes
, decompressGzip
) where
import BasePrelude
import Data.ByteString.Internal ( c2w, w2c )
import Data.Text.Encoding ( encodeUtf8, decodeUtf8With )
import qualified Codec.Compression.Zlib.Internal as Z
import qualified Data.ByteString.Char8 as S
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as C ( unpack )
import qualified Data.ByteString.Lazy.Internal as L ( ByteString(..) )
import qualified Data.Text as T
class Unpack s where unpack :: s -> String
instance Unpack L.ByteString where unpack = C.unpack
instance Unpack S.ByteString where unpack = S.unpack
instance Unpack T.Text where unpack = T.unpack
instance Unpack String where unpack = id
decodeBytes :: S.ByteString -> T.Text
decodeBytes = decodeUtf8With (const $ fmap w2c)
encodeBytes :: T.Text -> S.ByteString
encodeBytes = encodeUtf8
decompressGzip :: L.ByteString -> L.ByteString
decompressGzip s = case L.uncons s of
Just (31, s') -> case L.uncons s' of
Just (139,_) -> Z.foldDecompressStreamWithInput L.Chunk decompressGzip (const s)
(Z.decompressST Z.gzipOrZlibFormat Z.defaultDecompressParams) s
_ -> s
_ -> s