on master help sign in
Various fixes for Data.Text.Lazy and *word16
commitf7ba53c8b865cdd861893c8e073aef275885fa13 authorLuigy Leon <luigy@outlook.com> dateFri 17 Aug 2018, 23:01 (8 years ago) parent1ce21ad
4 files changed+23−13
Data/Text/Internal/Fusion.hs+1−1 Data/Text/Internal/Lazy/Fusion.hs+6−2 Data/Text/Lazy.hs+10−5 Data/Text/Unsafe.hs+6−5
Data/Text/Internal/Fusion.hs+1−1
@@ -84,7 +84,7 @@ unstream = coerce JSS.unstream
8484{-# RULES "STREAM stream/unstream fusion" forall s. stream (unstream s) = s #-}
8585
8686length :: Stream Char -> Int
87−length = JSS.length
87+length = S.lengthI
8888{-# INLINE [0] length #-}
8989
9090reverse :: Stream Char -> Text
Data/Text/Internal/Lazy/Fusion.hs+6−2
@@ -63,17 +63,21 @@ stream text = Stream next (text :*: 0) unknownSize
6363 | i >= len = next (ts :*: 0)
6464 | otherwise = Yield c (txt :*: i+d)
6565 where Iter c d = iter t i
66+{-# INLINE [0] stream #-}
6667#else
6768stream text = Stream next (text :*: 0) -- unknownSize
6869 where
6970 next (Empty :*: _) = Done
7071 next (txt@(Chunk t@(I.Text jst) ts) :*: i)
71− | i >= JSS.length jst = next (ts :*: 0)
72+ | i >= I# (js_length jst) = next (ts :*: 0)
7273 | otherwise = Yield c (txt :*: i+d)
7374 where Iter c d = iter t i
74−#endif
7575{-# INLINE [0] stream #-}
7676
77+foreign import javascript unsafe
78+ "$1.length" js_length :: JSString -> Int#
79+#endif
80+
7781-- TODO
7882-- | /O(n)/ Convert a 'Stream Char' into a 'Text', using the given
7983-- chunk size.
Data/Text/Lazy.hs+10−5
@@ -8,6 +8,8 @@
88#endif
99#ifdef __GHCJS__
1010{-# LANGUAGE ForeignFunctionInterface #-}
11+{-# LANGUAGE MagicHash #-}
12+{-# LANGUAGE UnliftedFFITypes #-}
1113#endif
1214-- |
1315-- Module : Data.Text.Lazy
@@ -248,6 +250,8 @@ import Text.Printf (PrintfArg, formatArg, formatString)
248250#endif
249251#ifdef __GHCJS__
250252import Data.JSString (JSString)
253+import GHC.Base (Int(..))
254+import GHC.Exts (Int#)
251255#endif
252256
253257-- $fusion
@@ -353,8 +357,8 @@ compareText (Chunk a0 as) (Chunk b0 bs) = outer a0 b0
353357 | otherwise = go (i+di) (j+dj)
354358 where T.Iter a di = T.iter ta i
355359 T.Iter b dj = T.iter tb j
356− lenA = T.length ta
357− lenB = T.length tb
360+ lenA = I# (js_length jsa)
361+ lenB = I# (js_length jsb)
358362#endif
359363
360364instance Show Text where
@@ -1181,7 +1185,7 @@ dropWords i t0
11811185 | n < len' = chunk (T.Text $ js_substr1 n' jst) ts
11821186 | otherwise = drop' (n - len') ts
11831187 where len' = fromIntegral len
1184− len = T.length t
1188+ len = I# (js_length jst)
11851189 n' = fromIntegral n
11861190#endif
11871191
@@ -1321,7 +1325,7 @@ splitAtWord x (Chunk c@(T.Text jst) cs)
13211325 | otherwise = chunk (T.Text (js_substr 0 (y-1) jst)) empty :*:
13221326 chunk (T.Text (js_substr1 y jst)) cs
13231327 where y = fromIntegral x
1324− len = T.length c
1328+ len = I# (js_length jst)
13251329#endif
13261330
13271331-- | /O(n+m)/ Find the first instance of @needle@ (which must be
@@ -1505,7 +1509,7 @@ splitOn pat src
15051509#ifndef __GHCJS__
15061510 l = foldlChunks (\a (T.Text _ _ b) -> a + fromIntegral b) 0 pat
15071511#else
1508− l = foldlChunks (\a t -> a + fromIntegral (T.length t)) 0 pat
1512+ l = foldlChunks (\a (T.Text t) -> a + fromIntegral (I# (js_length t))) 0 pat
15091513#endif
15101514{-# INLINE [1] splitOn #-}
15111515
@@ -1774,4 +1778,5 @@ impossibleError fun = P.error ("Data.Text.Lazy." ++ fun ++ ": impossible case")
17741778#ifdef __GHCJS__
17751779foreign import javascript unsafe "$2.substr($1)" js_substr1 :: Exts.Int -> JSString -> JSString
17761780foreign import javascript unsafe "$3.substr($1,$2)" js_substr :: Exts.Int -> Exts.Int -> JSString -> JSString
1781+foreign import javascript unsafe "$1.length" js_length :: JSString -> Int#
17771782#endif
Data/Text/Unsafe.hs+6−5
@@ -183,7 +183,7 @@ takeWord16 :: Int -> Text -> Text
183183#ifndef __GHCJS__
184184takeWord16 k (Text arr off _len) = Text arr off k
185185#else
186−takeWord16 k (Text txt) = Text $ js_take k txt
186+takeWord16 (I# k) (Text txt) = Text $ js_substr 0# k txt
187187#endif
188188{-# INLINE takeWord16 #-}
189189
@@ -192,7 +192,7 @@ dropWord16 :: Int -> Text -> Text
192192#ifndef __GHCJS__
193193dropWord16 k (Text arr off len) = Text arr (off+k) (len-k)
194194#else
195−dropWord16 k (Text txt) = Text $ js_drop k txt
195+dropWord16 (I# k) (Text txt) = Text $ js_substr1 k txt
196196#endif
197197{-# INLINE dropWord16 #-}
198198
@@ -201,11 +201,12 @@ foreign import javascript unsafe
201201 "$2.charCodeAt($1)" js_charCodeAt :: Int -> JSString -> Int#
202202
203203foreign import javascript unsafe
204− "h$jsstringTake" js_take :: Int -> JSString -> JSString
204+ "$1.length" js_length :: JSString -> Int
205205
206206foreign import javascript unsafe
207− "h$jsstringDrop" js_drop :: Int -> JSString -> JSString
207+ "$3.substr($1,$2)" js_substr :: Int# -> Int# -> JSString -> JSString
208208
209209foreign import javascript unsafe
210− "$1.length" js_length :: JSString -> Int
210+ "$2.substr($1)" js_substr1 :: Int# -> JSString -> JSString
211+
211212#endif