Skip to content

Commit

Permalink
Merge branch 'feature/finalizer'
Browse files Browse the repository at this point in the history
  • Loading branch information
msakai committed Aug 16, 2018
2 parents 6d7a35d + 6831a79 commit 732bfaf
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Data/ByteString/Encoding/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import qualified Data.Text.Foreign as T
import qualified Data.Text.Lazy as TL
import Data.Word
import Foreign
import qualified Foreign.Concurrent as Conc
import Foreign.ForeignPtr (touchForeignPtr)
import Foreign.Ptr (nullPtr)
import qualified GHC.IO.Encoding as Enc
import GHC.IO.Buffer
import System.IO.Unsafe
Expand All @@ -33,6 +36,7 @@ encodeWith enc inBufSize outBufSize = encodeStringWith enc inBufSize outBufSize
encodeStringWith :: Enc.TextEncoding -> Int -> Int -> String -> BL.ByteString
encodeStringWith Enc.TextEncoding{ .. } inBufSize outBufSize s = BL.fromChunks $ unsafePerformIO $ do
Enc.BufferCodec{ .. } <- mkTextEncoder
fp <- Conc.newForeignPtr nullPtr close

let fillInBuf :: String -> CharBuffer -> IO (String, CharBuffer)
fillInBuf s buf
Expand Down Expand Up @@ -61,7 +65,7 @@ encodeStringWith Enc.TextEncoding{ .. } inBufSize outBufSize s = BL.fromChunks $
if isEmptyBuffer inBuf1 then do
assert (null s') $ return ()
(m, _outBuf') <- flushOutBuf outBuf
close
touchForeignPtr fp
return m
else do
(ret, inBuf2, outBuf2) <- encode inBuf1 outBuf
Expand Down Expand Up @@ -99,6 +103,7 @@ decode enc b = decodeWith enc 1024 1024 b
decodeWith :: Enc.TextEncoding -> Int -> Int -> BL.ByteString -> TL.Text
decodeWith Enc.TextEncoding{ .. } inBufSize outBufSize b = TL.fromChunks $ unsafePerformIO $ do
Enc.BufferCodec{ .. } <- mkTextDecoder
fp <- Conc.newForeignPtr nullPtr close

let fillInBuf :: [B.ByteString] -> Buffer Word8 -> IO ([B.ByteString], Buffer Word8)
fillInBuf bs buf
Expand Down Expand Up @@ -155,7 +160,7 @@ decodeWith Enc.TextEncoding{ .. } inBufSize outBufSize b = TL.fromChunks $ unsaf
if isEmptyBuffer inBuf1 then do
assert (null bs') $ return ()
(m, _outBuf') <- flushOutBuf outBuf workspace
close
touchForeignPtr fp
return m
else do
(ret, inBuf2, outBuf2) <- encode inBuf1 outBuf
Expand Down

0 comments on commit 732bfaf

Please sign in to comment.