on master help sign in
Simplify code, and ensure that tests are sane.

--HG-- extra : convert_revision : 486de4595d4a05de90321a2f10cbd0b79f568f96

commit2e4c79c94740ff8843774efc61b3d9caff1310f2 authorBryan O'Sullivan <bos@serpentine.com> dateWed 7 Oct 2009, 02:29 (17 years ago) parentd3faa5d
3 files changed+14−33
Data/Text/Lazy/Search.hs+4−9 tests/Properties.hs+5−5 tests/SlowFunctions.hs+5−19
Data/Text/Lazy/Search.hs+4−9
@@ -38,7 +38,7 @@ import Data.Word (Word16, Word64)
3838import qualified Data.Text.Internal as T
3939import qualified Data.Text as T
4040import Data.Text.Fusion.Internal (PairS(..))
41−import Data.Text.Lazy.Internal (Text(..))
41+import Data.Text.Lazy.Internal (Text(..), foldlChunks)
4242import Data.Bits ((.|.), (.&.))
4343import Data.Text.UnsafeShift (shiftL)
4444
@@ -79,7 +79,7 @@ indices needle@(Chunk n ns) haystack@(Chunk k ks)
7979 nlen = wordLength needle
8080 nlast = nlen - 1
8181 nindex = index n ns
82− z = foldChunks fin 0 needle
82+ z = foldlChunks fin 0 needle
8383 where fin _ (T.Text farr foff flen) = A.unsafeIndex farr (foff+flen-1)
8484 mask :*: skip = buildTable needle
8585 scanOne c i (T.Text oarr ooff olen) os = go 0
@@ -104,13 +104,8 @@ index (T.Text arr off len) xs i
104104swizzle :: Word16 -> Word64
105105swizzle k = 1 `shiftL` (fromIntegral k .&. 0x3f)
106106
107−foldChunks :: (a -> T.Text -> a) -> a -> Text -> a
108−foldChunks _ z Empty = z
109−foldChunks f z (Chunk c cs) = let z' = f z c
110− in z' `seq` foldChunks f z' cs
111−
112107wordLength :: Text -> Int64
113−wordLength = foldChunks sumLength 0
108+wordLength = foldlChunks sumLength 0
114109 where sumLength i (T.Text _ _ l) = i + fromIntegral l
115110
116111buildTable :: Text -> PairS Word64 Int64
@@ -129,5 +124,5 @@ buildTable needle@(Chunk k ks) = outer k ks 0 0 0 (nlen-2)
129124 | otherwise = skip
130125 xlast = xlen - 1
131126 nlen = wordLength needle
132− z = foldChunks fin 0 needle
127+ z = foldlChunks fin 0 needle
133128 where fin _ (T.Text farr foff flen) = A.unsafeIndex farr (foff+flen-1)
tests/Properties.hs+5−5
@@ -1,5 +1,5 @@
1−{-# LANGUAGE BangPatterns, FlexibleInstances, ScopedTypeVariables,
2− TypeSynonymInstances #-}
1+{-# LANGUAGE BangPatterns, FlexibleInstances, OverloadedStrings,
2+ ScopedTypeVariables, TypeSynonymInstances #-}
33{-# OPTIONS_GHC -fno-enable-rewrite-rules #-}
44
55import Test.QuickCheck hiding (evaluate)
@@ -487,9 +487,10 @@ sf_zipWith p c s = (L.zipWith c (L.filter p s) . L.filter p) `eqP` (unpackS . S
487487t_zipWith c s = L.zipWith c s `eqP` (unpackS . T.zipWith c (packS s))
488488tl_zipWith c s = L.zipWith c s `eqP` (unpackS . TL.zipWith c (packS s))
489489
490−slow_indices = unsquare (\s -> (map fromIntegral . Slow.indices (T.pack s)) `eq` (Slow.lazyIndices (TL.pack s) . TL.fromChunks . (:[])))
491490t_indices = unsquare (\s -> Slow.indices s `eq` indices s)
492−tl_indices = unsquare (\s -> Slow.lazyIndices s `eq` S.indices s)
491+tl_indices = unsquare (\s -> lazyIndices s `eq` S.indices s)
492+ where lazyIndices s t = map fromIntegral $ Slow.indices (conc s) (conc t)
493+ conc = T.concat . TL.toChunks
493494t_indices_occurs t = unsquare (\ts -> let s = T.intercalate t ts
494495 in Slow.indices t s == indices t s)
495496
@@ -840,7 +841,6 @@ tests = [
840841 testProperty "t_findIndex" t_findIndex,
841842 testProperty "t_count" t_count,
842843 testProperty "tl_count" tl_count,
843− testProperty "slow_indices" slow_indices,
844844 testProperty "t_indices" t_indices,
845845 testProperty "tl_indices" tl_indices,
846846 testProperty "t_indices_occurs" t_indices_occurs
tests/SlowFunctions.hs+5−19
@@ -3,18 +3,15 @@
33module SlowFunctions
44 (
55 indices
6− , lazyIndices
76 , split
87 ) where
98
10−import Data.Int (Int64)
119import qualified Data.Text as T
12−import qualified Data.Text.Lazy as L
1310import Data.Text.Internal (Text(..))
1411import Data.Text.Unsafe (iter_, unsafeHead, unsafeTail)
1512
16−indices :: Text -- ^ Substring to search for (@needle@)
17− -> Text -- ^ Text to search in (@haystack@)
13+indices :: T.Text -- ^ Substring to search for (@needle@)
14+ -> T.Text -- ^ Text to search in (@haystack@)
1815 -> [Int]
1916indices needle@(Text _narr _noff nlen) haystack@(Text harr hoff hlen)
2017 | T.null needle = []
@@ -26,20 +23,9 @@ indices needle@(Text _narr _noff nlen) haystack@(Text harr hoff hlen)
2623 where t = Text harr (hoff+i) (hlen-i)
2724 d = iter_ haystack i
2825
29−lazyIndices :: L.Text -> L.Text -> [Int64]
30−lazyIndices needle haystack
31− | L.null needle = []
32− | otherwise = scan 0 haystack
33− where
34− scan !i hay
35− | L.null hay = []
36− | needle `L.isPrefixOf` hay = i : scan (i+n) (L.drop n hay)
37− | otherwise = scan (i+1) (L.tail hay)
38− n = L.length needle
39−
40−split :: Text -- ^ Text to split on
41− -> Text -- ^ Input text
42− -> [Text]
26+split :: T.Text -- ^ Text to split on
27+ -> T.Text -- ^ Input text
28+ -> [T.Text]
4329split pat src0
4430 | T.null pat = error "split: empty"
4531 | l == 1 = T.splitBy (== (unsafeHead pat)) src0