on master help sign in
Tidy up unstream
c170c02Bryan O'Sullivan
Data/Text/Fusion.hs+9−14
@@ -136,34 +136,29 @@ stream (Text arr off len) = Stream next off len
136unstream :: Stream Char -> Text
136unstream :: Stream Char -> Text
137unstream (Stream next0 s0 len) = Text (fst a) 0 (snd a)
137unstream (Stream next0 s0 len) = Text (fst a) 0 (snd a)
138 where
138 where
139− a :: ((A.Array Word16),Int)
140− a = runST ((A.unsafeNew len :: ST s (A.MArray s Word16))
141− >>= (\arr -> loop arr 0 len s0))
139+ a = runST (A.unsafeNew len >>= (\arr -> loop arr 0 len s0))
142 loop arr !i !top !s
140 loop arr !i !top !s
143− | i + 1 > top = do arr' <- A.unsafeNew (top*2)
144− case next0 s of
141+ | i + 1 > top = case next0 s of
145− Done -> liftM2 (,) (A.unsafeFreeze arr) (return i)
142+ Done -> liftM2 (,) (A.unsafeFreeze arr) (return i)
143+ _ -> do
144+ arr' <- A.unsafeNew (top*2)
146− _ -> copy arr arr' >> loop arr' i (top*2) s
145+ copy arr arr' >> loop arr' i (top*2) s
147 | otherwise = case next0 s of
146 | otherwise = case next0 s of
148 Done -> liftM2 (,) (A.unsafeFreeze arr) (return i)
147 Done -> liftM2 (,) (A.unsafeFreeze arr) (return i)
149 Skip s' -> loop arr i top s'
148 Skip s' -> loop arr i top s'
150 Yield x s'
149 Yield x s'
151 | n < 0x10000 -> do
150 | n < 0x10000 -> do
152− A.unsafeWrite arr i (fromIntegral n :: Word16)
151+ A.unsafeWrite arr i (fromIntegral n)
153 loop arr (i+1) top s'
152 loop arr (i+1) top s'
154 | otherwise -> do
153 | otherwise -> do
155 A.unsafeWrite arr i l
154 A.unsafeWrite arr i l
156 A.unsafeWrite arr (i + 1) r
155 A.unsafeWrite arr (i + 1) r
157 loop arr (i+2) top s'
156 loop arr (i+2) top s'
158 where
157 where
159− n :: Int
160 n = ord x
158 n = ord x
161− m :: Int
162 m = n - 0x10000
159 m = n - 0x10000
163− l :: Word16
164− l = fromIntegral $ (shiftR m 10) + (0xD800 :: Int)
160+ l = fromIntegral $ (m `shiftR` 10) + 0xD800
165− r :: Word16
166− r = fromIntegral $ (m .&. (0x3FF :: Int)) + (0xDC00 :: Int)
161+ r = fromIntegral $ (m .&. 0x3FF) + 0xDC00
167{-# INLINE [0] unstream #-}
162{-# INLINE [0] unstream #-}
168{-# RULES "STREAM stream/unstream fusion" forall s. stream (unstream s) = s #-}
163{-# RULES "STREAM stream/unstream fusion" forall s. stream (unstream s) = s #-}
169
164