bytestring-encoding 0.1.1.0 → 0.1.2.0
raw patch · 5 files changed
+102/−13 lines, 5 filesdep ~basedep ~bytestringdep ~textPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, bytestring, text
API changes (from Hackage documentation)
Files
- ChangeLog.md +4/−0
- README.md +1/−3
- bytestring-encoding.cabal +11/−9
- src/Data/ByteString/Encoding.hs +1/−1
- src/Data/ByteString/Lazy/Encoding/Internal.hs +85/−0
ChangeLog.md view
@@ -2,6 +2,10 @@ ## Unreleased changes +## 0.1.2.0++* [Support text package 2.0](https://github.com/msakai/bytestring-encoding/pull/8)+ ## 0.1.1.0 * [Fix memory corruption bug](https://github.com/msakai/bytestring-encoding/pull/3)
README.md view
@@ -1,8 +1,6 @@ # bytestring-encoding: ByteString ↔ Text converter based on GHC.IO.Encoding -[](https://travis-ci.org/msakai/bytestring-encoding)-[](https://ci.appveyor.com/project/msakai/bytestring-encoding/branch/master)-[](https://github.com/msakai/bytestring-encoding/actions)+[](https://github.com/msakai/bytestring-encoding/actions) [](https://coveralls.io/github/msakai/bytestring-encoding?branch=master) [](https://hackage.haskell.org/package/bytestring-encoding) [](https://packdeps.haskellers.com/feed?needle=bytestring-encoding)
bytestring-encoding.cabal view
@@ -1,13 +1,13 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.33.0.+-- This file has been generated from package.yaml by hpack version 0.34.4. -- -- see: https://github.com/sol/hpack ----- hash: 07afcc60ba7089a37925641b27313f86f9aa4e44f8ce1f4565cee5a74e09e58d+-- hash: 16490c648a555bee319cb103d3456c72ddb655413e3e134ccdf466c100ccb146 name: bytestring-encoding-version: 0.1.1.0+version: 0.1.2.0 synopsis: ByteString ↔ Text converter based on GHC.IO.Encoding description: Please see the README on GitHub at <https://github.com/msakai/bytestring-encoding#readme> category: Data, Text@@ -19,6 +19,8 @@ license: BSD3 license-file: LICENSE build-type: Simple+tested-with:+ GHC==7.10.3 GHC==8.0.2 GHC==8.2.2 GHC==8.4.4 GHC==8.6.5 GHC==8.8.4 GHC==8.10.7 GHC==9.0.2 extra-source-files: README.md ChangeLog.md@@ -37,9 +39,9 @@ hs-source-dirs: src build-depends:- base >=4.7 && <5- , bytestring- , text+ base >=4.8 && <5+ , bytestring >=0.10.6.0 && <0.12+ , text >=1.2.2.2 && <2.1 default-language: Haskell2010 test-suite bytestring-encoding-test@@ -55,13 +57,13 @@ ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: QuickCheck- , base >=4.7 && <5- , bytestring+ , base >=4.8 && <5+ , bytestring >=0.10.6.0 && <0.12 , bytestring-encoding , deepseq , tasty >=0.10.1 , tasty-hunit , tasty-quickcheck , tasty-th- , text+ , text >=1.2.2.2 && <2.1 default-language: Haskell2010
src/Data/ByteString/Encoding.hs view
@@ -44,7 +44,7 @@ encode :: TextEncoding -> T.Text -> B.ByteString encode enc = BL.toStrict . L.encode enc . TL.fromStrict --- | Decode a strict 'B.ByteString' to a strit 'T.Text' using a given 'Enc.TextEncoding'.+-- | Decode a strict 'B.ByteString' to a strict 'T.Text' using a given 'Enc.TextEncoding'. decode :: TextEncoding -> B.ByteString -> T.Text decode enc = TL.toStrict . L.decode enc . BL.fromStrict
src/Data/ByteString/Lazy/Encoding/Internal.hs view
@@ -1,6 +1,7 @@ {-# OPTIONS_GHC -Wall #-} {-# OPTIONS_HADDOCK hide #-} {-# LANGUAGE BangPatterns #-}+{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} module Data.ByteString.Lazy.Encoding.Internal@@ -15,6 +16,7 @@ import qualified Data.ByteString as B import qualified Data.ByteString.Unsafe as B import qualified Data.ByteString.Lazy as BL+import Data.Char (ord) import qualified Data.Text as T import qualified Data.Text.Foreign as T import qualified Data.Text.Lazy as TL@@ -127,6 +129,23 @@ moveBytes (q `plusPtr` bufR buf) p (bufferAvailable buf) go (B.drop (bufferAvailable buf) b : bs) buf{ bufR = bufR buf + bufferAvailable buf } +#if MIN_VERSION_text(2,0,0)+ flushOutBuf :: CharBuffer -> ForeignPtr Word8 -> IO ([T.Text], CharBuffer)+ flushOutBuf buf workspace+ | isEmptyBuffer buf = return ([], buf{ bufL=0, bufR=0 })+ | otherwise =+ withForeignPtr workspace $ \workspace' ->+ withBuffer buf $ \p -> do+ let f !i !j+ | bufR buf <= i = return j+ | otherwise = do+ (c, i') <- readCharBufPtr (castPtr p) i+ j' <- writeUTF8 workspace' j c+ f i' j'+ n <- f (bufL buf) 0+ t <- T.fromPtr workspace' (fromIntegral n)+ return ([t], buf{ bufL=0, bufR=0 })+#else flushOutBuf :: CharBuffer -> ForeignPtr Word16 -> IO ([T.Text], CharBuffer) flushOutBuf buf workspace | isEmptyBuffer buf = return ([], buf{ bufL=0, bufR=0 })@@ -156,8 +175,13 @@ n <- f (bufL buf) 0 t <- T.fromPtr workspace' (fromIntegral n) return ([t], buf{ bufL=0, bufR=0 })+#endif +#if MIN_VERSION_text(2,0,0)+ loop :: [B.ByteString] -> Buffer Word8 -> CharBuffer -> ForeignPtr Word8 -> IO [T.Text]+#else loop :: [B.ByteString] -> Buffer Word8 -> CharBuffer -> ForeignPtr Word16 -> IO [T.Text]+#endif loop bs inBuf outBuf workspace = do (bs', inBuf1) <- fillInBuf bs inBuf if isEmptyBuffer inBuf1 then do@@ -191,5 +215,66 @@ inBuf <- newByteBuffer inBufSize ReadBuffer outBuf <- newCharBuffer outBufSize WriteBuffer+#if MIN_VERSION_text(2,0,0)+ workspace <- mallocForeignPtrArray (outBufSize * 4)+#else workspace <- if charSize == 2 then newForeignPtr_ nullPtr else mallocForeignPtrArray (outBufSize * 2)+#endif loop (BL.toChunks b) inBuf outBuf workspace+++#if MIN_VERSION_text(2,0,0)++writeUTF8 :: Ptr Word8 -> Int -> Char -> IO Int+writeUTF8 p i c = do+ let x = ord c+ if x <= 0x7F then do+ pokeElemOff p i (fromIntegral x)+ return $! i+1+ else if x <= 0x07FF then do+ let (c1,c2) = ord2 c+ pokeElemOff p i c1+ pokeElemOff p (i+1) c2+ return $! i+2+ else if x <= 0xFFFF then do+ let (c1,c2,c3) = ord3 c+ pokeElemOff p i c1+ pokeElemOff p (i+1) c2+ pokeElemOff p (i+2) c3+ return $! i+3+ else do+ let (c1,c2,c3,c4) = ord4 c+ pokeElemOff p i c1+ pokeElemOff p (i+1) c2+ pokeElemOff p (i+2) c3+ pokeElemOff p (i+3) c4+ return $! i+4++-- -----------------------------------------------------------------------------+-- UTF-8 primitives, lifted from Data.Text.Fusion.Utf8++ord2 :: Char -> (Word8,Word8)+ord2 c = assert (n >= 0x80 && n <= 0x07ff) (x1,x2)+ where+ n = ord c+ x1 = fromIntegral $ (n `shiftR` 6) + 0xC0+ x2 = fromIntegral $ (n .&. 0x3F) + 0x80++ord3 :: Char -> (Word8,Word8,Word8)+ord3 c = assert (n >= 0x0800 && n <= 0xffff) (x1,x2,x3)+ where+ n = ord c+ x1 = fromIntegral $ (n `shiftR` 12) + 0xE0+ x2 = fromIntegral $ ((n `shiftR` 6) .&. 0x3F) + 0x80+ x3 = fromIntegral $ (n .&. 0x3F) + 0x80++ord4 :: Char -> (Word8,Word8,Word8,Word8)+ord4 c = assert (n >= 0x10000) (x1,x2,x3,x4)+ where+ n = ord c+ x1 = fromIntegral $ (n `shiftR` 18) + 0xF0+ x2 = fromIntegral $ ((n `shiftR` 12) .&. 0x3F) + 0x80+ x3 = fromIntegral $ ((n `shiftR` 6) .&. 0x3F) + 0x80+ x4 = fromIntegral $ (n .&. 0x3F) + 0x80++#endif