@@ -111,7 +111,7 @@ import qualified Data.List as L
111111import Data.Word (Word16)
112112import GHC.Exts (Int(..), (+#))
113113import Data.Text.Internal (Text(..))
114−import Data.Text.UnsafeChar (unsafeChr)
114+import Data.Text.UnsafeChar (unsafeChr, unsafeWrite)
115115import qualified Data.Text.Array as A
116116import qualified Data.Text.Internal as I
117117import qualified Data.Text.Utf16 as U16
@@ -149,29 +149,17 @@ unstream :: Stream Char -> Text
149149unstream (Stream _next0 _s0 0) = I.empty
150150unstream (Stream next0 s0 len) = Text (fst a) 0 (snd a)
151151 where
152− a = runST (A.unsafeNew len >>= (\arr -> loop arr 0 len s0))
153− loop arr !i !top !s
152+ a = runST (A.unsafeNew len >>= (\arr -> loop arr len s0 0))
153+ loop arr !top !s !i
154154 | i + 1 >= top = case next0 s of
155155 Done -> liftM2 (,) (A.unsafeFreeze arr) (return i)
156156 _ -> do
157157 arr' <- A.unsafeNew (top*2)
158− copy arr arr' >> loop arr' i (top*2) s
158+ copy arr arr' >> loop arr' (top*2) s i
159159 | otherwise = case next0 s of
160160 Done -> liftM2 (,) (A.unsafeFreeze arr) (return i)
161− Skip s' -> loop arr i top s'
162− Yield x s'
163− | n < 0x10000 -> do
164− A.unsafeWrite arr i (fromIntegral n)
165− loop arr (i+1) top s'
166− | otherwise -> do
167− A.unsafeWrite arr i l
168− A.unsafeWrite arr (i + 1) r
169− loop arr (i+2) top s'
170− where
171− n = ord x
172− m = n - 0x10000
173− l = fromIntegral $ (m `shiftR` 10) + 0xD800
174− r = fromIntegral $ (m .&. 0x3FF) + 0xDC00
161+ Skip s' -> loop arr top s' i
162+ Yield x s' -> unsafeWrite arr i x >>= loop arr top s'
175163{-# INLINE [0] unstream #-}
176164{-# RULES "STREAM stream/unstream fusion" forall s. stream (unstream s) = s #-}
177165