@@ -1,4 +1,4 @@
1−{-# LANGUAGE CPP, MagicHash, RankNTypes, RecordWildCards, UnboxedTuples #-}
1+{-# LANGUAGE MagicHash, RankNTypes, RecordWildCards, UnboxedTuples #-}
22
33-- |
44-- Module : Data.Attoparsec.Text.Buffer
@@ -34,10 +34,7 @@ module Data.Attoparsec.Text.Buffer
3434 , dropWord16
3535 ) where
3636
37−import Control.Applicative ((<$>))
3837import Control.Exception (assert)
39−import Data.Attoparsec.Internal.Fhthagn (inlinePerformIO)
40−import Data.IORef (IORef, atomicModifyIORef, newIORef)
4138import Data.List (foldl1')
4239import Data.Monoid (Monoid(..))
4340import Data.Text ()
@@ -48,22 +45,13 @@ import Data.Text.Unsafe (Iter(..))
4845import GHC.Base (unsafeCoerce#)
4946import GHC.ST (ST(..), runST)
5047import Prelude hiding (length)
51−import System.IO.Unsafe (unsafePerformIO)
5248import qualified Data.Text.Array as A
5349
54−#if __GLASGOW_HASKELL__ >= 702
55−import Control.Monad.ST.Unsafe (unsafeIOToST)
56−#else
57−import Control.Monad.ST (unsafeIOToST)
58−#endif
59−
6050data Buffer = Buf {
6151 _arr :: {-# UNPACK #-} !A.Array
6252 , _off :: {-# UNPACK #-} !Int
6353 , _len :: {-# UNPACK #-} !Int
6454 , _cap :: {-# UNPACK #-} !Int
65− , _gen :: {-# UNPACK #-} !Int
66− , _ref :: {-# UNPACK #-} !(IORef Int)
6755 }
6856
6957instance Show Buffer where
@@ -73,53 +61,48 @@ instance Show Buffer where
7361-- copies in the (hopefully) common case of no further input being fed
7462-- to us.
7563buffer :: Text -> Buffer
76−buffer (Text arr off len) = inlinePerformIO $
77− Buf arr off len len 0 <$> newIORef 0
64+buffer (Text arr off len) = Buf arr off len len
7865
7966unbuffer :: Buffer -> Text
80−unbuffer (Buf arr off len _ _ _) = Text arr off len
67+unbuffer (Buf arr off len _cap) = Text arr off len
8168
8269instance Monoid Buffer where
83− mempty = unsafePerformIO $ Buf A.empty 0 0 0 0 <$> newIORef 0
84− {-# NOINLINE mempty #-}
85−
86− mappend (Buf _ _ _ 0 _ _) b = b
87− mappend a (Buf _ _ _ 0 _ _) = a
88− mappend (Buf arr0 off0 len0 cap0 gen ref)
89− (Buf arr1 off1 len1 _ _ _) = runST $ do
90− gen' <- unsafeIOToST $
91− atomicModifyIORef ref (\a -> let a' = a+1 in (a',a'))
70+ mempty = Buf A.empty 0 0 0
71+
72+ mappend (Buf _ _ _ 0) b = b
73+ mappend a (Buf _ _ _ 0) = a
74+ mappend (Buf arr0 off0 len0 cap0) (Buf arr1 off1 len1 _cap1) = runST $ do
9275 let newlen = len0 + len1
93− if gen' == gen+1 && newlen <= cap0
76+ if newlen <= cap0
9477 then do
9578 marr <- unsafeThaw arr0
9679 A.copyI marr (off0+len0) arr1 off1 (off0+newlen)
9780 arr2 <- A.unsafeFreeze marr
98− return (Buf arr2 off0 newlen cap0 gen' ref)
81+ return (Buf arr2 off0 newlen cap0)
9982 else do
10083 let newcap = newlen * 2
10184 marr <- A.new newcap
10285 A.copyI marr 0 arr0 off0 len0
10386 A.copyI marr len0 arr1 off1 newlen
10487 arr2 <- A.unsafeFreeze marr
105− return (Buf arr2 0 newlen newcap gen' ref)
88+ return (Buf arr2 0 newlen newcap)
10689
10790 mconcat [] = mempty
10891 mconcat xs = foldl1' mappend xs
10992
11093length :: Buffer -> Int
111−length (Buf _ _ len _ _ _) = len
94+length (Buf _ _ len _) = len
11295{-# INLINE length #-}
11396
11497substring :: Int -> Int -> Buffer -> Text
115−substring s l (Buf arr off len _ _ _) =
98+substring s l (Buf arr off len _cap) =
11699 assert (s >= 0 && s <= len) .
117100 assert (l >= 0 && l <= len-s) $
118101 Text arr (off+s) l
119102{-# INLINE substring #-}
120103
121104dropWord16 :: Int -> Buffer -> Text
122−dropWord16 s (Buf arr off len _ _ _) =
105+dropWord16 s (Buf arr off len _cap) =
123106 assert (s >= 0 && s <= len) $
124107 Text arr (off+s) (len-s)
125108{-# INLINE dropWord16 #-}
@@ -128,7 +111,7 @@ dropWord16 s (Buf arr off len _ _ _) =
128111-- array, returning the current character and the delta to add to give
129112-- the next offset to iterate at.
130113iter :: Buffer -> Int -> Iter
131−iter (Buf arr off _len _ _ _) i
114+iter (Buf arr off _len _cap) i
132115 | m < 0xD800 || m > 0xDBFF = Iter (unsafeChr m) 1
133116 | otherwise = Iter (chr2 m n) 2
134117 where m = A.unsafeIndex arr j
@@ -140,7 +123,7 @@ iter (Buf arr off _len _ _ _) i
140123-- | /O(1)/ Iterate one step through a UTF-16 array, returning the
141124-- delta to add to give the next offset to iterate at.
142125iter_ :: Buffer -> Int -> Int
143−iter_ (Buf arr off _len _ _ _) i | m < 0xD800 || m > 0xDBFF = 1
126+iter_ (Buf arr off _len _cap) i | m < 0xD800 || m > 0xDBFF = 1
144127 | otherwise = 2
145128 where m = A.unsafeIndex arr (off+i)
146129{-# INLINE iter_ #-}