Bryan O'Sullivan avatar Bryan O'Sullivan committed 5adc9f2

Write a property for the new split function, and fix a bug in it.

Comments (0)

Files changed (2)

 -- not copy the substrings, it just constructs new 'Text's that are
 -- slices of the original.
 split :: Text -> Text -> [Text]
-split pat = go
+split pat src0
+    | l == 0    = [src0]
+    | otherwise = go src0
   where
+    l      = length pat
     go src = search 0 src
       where
         search !n !s
             | null s             = [src]      -- not found
             | pat `isPrefixOf` s = take n src : go (drop l s)
             | otherwise          = search (n+1) (unsafeTail s)
-        l = length pat
 {-# INLINE split #-}
 
 -- | /O(n)/ Splits a 'Text' into components delimited by separators,

tests/Properties.hs

 prop_T_tails           = L.tails       `eqP` (map unpackS . T.tails)
 prop_TL_tails          = L.tails       `eqP` (map unpackS . TL.tails)
 
+prop_T_split_i t       = id `eq` (T.intercalate t . T.split t)
 prop_T_splitChar_i c   = id `eq` (T.intercalate (T.singleton c) . T.splitChar c)
 prop_TL_split_i c      = id `eq` (TL.intercalate (TL.singleton c) . TL.split c)
 
   ("prop_T_tails", mytest prop_T_tails),
   ("prop_TL_tails", mytest prop_TL_tails),
 
+  ("prop_T_split_i", mytest prop_T_split_i),
   ("prop_T_splitChar_i", mytest prop_T_splitChar_i),
   ("prop_TL_split_i", mytest prop_TL_split_i),
   ("prop_T_splitWith", mytest prop_T_splitWith),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.