on master help sign in
Another try at improving Text generation.
d52fb2aBryan O'Sullivan
Data/Text/Fusion.hs+6−5
@@ -51,6 +51,7 @@ import Prelude (Bool(..), Char, Maybe(..), Monad(..), Int,
51 fromIntegral, otherwise)
51 fromIntegral, otherwise)
52import Data.Bits ((.&.))
52import Data.Bits ((.&.))
53import Data.Text.Internal (Text(..))
53import Data.Text.Internal (Text(..))
54+import Data.Text.Private (runText)
54import Data.Text.UnsafeChar (ord, unsafeChr, unsafeWrite)
55import Data.Text.UnsafeChar (ord, unsafeChr, unsafeWrite)
55import Data.Text.UnsafeShift (shiftL, shiftR)
56import Data.Text.UnsafeShift (shiftL, shiftR)
56import qualified Data.Text.Array as A
57import qualified Data.Text.Array as A
@@ -94,22 +95,22 @@ reverseStream (Text arr off len) = Stream next (off+len-1) (maxSize len)
94
95
95-- | /O(n)/ Convert a 'Stream Char' into a 'Text'.
96-- | /O(n)/ Convert a 'Stream Char' into a 'Text'.
96unstream :: Stream Char -> Text
97unstream :: Stream Char -> Text
97−unstream (Stream next0 s0 len) = I.textP (P.fst a) 0 (P.snd a)
98+unstream (Stream next0 s0 len) = a
98 where
99 where
99− a = A.run2 (A.new mlen >>= \arr -> outer arr mlen s0 0)
100+ a = runText (\done -> A.new mlen >>= \arr -> outer done arr mlen s0 0)
100 where mlen = upperBound 4 len
101 where mlen = upperBound 4 len
101− outer arr top = loop
102+ outer done arr top = loop
102 where
103 where
103 loop !s !i =
104 loop !s !i =
104 case next0 s of
105 case next0 s of
105− Done -> return (arr, i)
106+ Done -> done arr i
106 Skip s' -> loop s' i
107 Skip s' -> loop s' i
107 Yield x s'
108 Yield x s'
108 | j >= top -> {-# SCC "unstream/resize" #-} do
109 | j >= top -> {-# SCC "unstream/resize" #-} do
109 let top' = (top + 1) `shiftL` 1
110 let top' = (top + 1) `shiftL` 1
110 arr' <- A.new top'
111 arr' <- A.new top'
111 A.copyM arr' 0 arr 0 top
112 A.copyM arr' 0 arr 0 top
112− outer arr' top' s i
113+ outer done arr' top' s i
113 | otherwise -> do d <- unsafeWrite arr i x
114 | otherwise -> do d <- unsafeWrite arr i x
114 loop s' (i+d)
115 loop s' (i+d)
115 where j | ord x < 0x10000 = i
116 where j | ord x < 0x10000 = i
Data/Text/Private.hs+11−2
@@ -1,4 +1,4 @@
1−{-# LANGUAGE BangPatterns, UnboxedTuples #-}
1+{-# LANGUAGE BangPatterns, Rank2Types, UnboxedTuples #-}
2
2
3-- |
3-- |
4-- Module : Data.Text.Private
4-- Module : Data.Text.Private
@@ -11,11 +11,14 @@
11
11
12module Data.Text.Private
12module Data.Text.Private
13 (
13 (
14+ runText
14− span_
15+ , span_
15 ) where
16 ) where
16
17
18+import Control.Monad.ST (ST, runST)
17import Data.Text.Internal (Text(..), textP)
19import Data.Text.Internal (Text(..), textP)
18import Data.Text.Unsafe (Iter(..), iter)
20import Data.Text.Unsafe (Iter(..), iter)
21+import qualified Data.Text.Array as A
19
22
20span_ :: (Char -> Bool) -> Text -> (# Text, Text #)
23span_ :: (Char -> Bool) -> Text -> (# Text, Text #)
21span_ p t@(Text arr off len) = (# hd,tl #)
24span_ p t@(Text arr off len) = (# hd,tl #)
@@ -26,3 +29,9 @@ span_ p t@(Text arr off len) = (# hd,tl #)
26 | otherwise = i
29 | otherwise = i
27 where Iter c d = iter t i
30 where Iter c d = iter t i
28{-# INLINE span_ #-}
31{-# INLINE span_ #-}
32+
33+runText :: (forall s. (A.MArray s -> Int -> ST s Text) -> ST s Text) -> Text
34+runText act = runST (act $ \ !marr !len -> do
35+ arr <- A.unsafeFreeze marr
36+ return $! textP arr 0 len)
37+{-# INLINE runText #-}