@@ -20,6 +20,8 @@ import qualified Data.Text.Internal.Lazy as TL (Text(..))
20
20
import qualified Data.Text.Internal.Lazy.Fusion as SL
21
21
import qualified Data.Text.Lazy as TL
22
22
import qualified Tests.SlowFunctions as Slow
23
+ import Control.Monad (replicateM )
24
+ import Data.List (nub , sort )
23
25
24
26
s_take n = L. take n `eqP` (unpackS . S. take n)
25
27
s_take_s (Small n) = L. take n `eqP` (unpackS . S. unstream . S. take n)
@@ -231,6 +233,47 @@ tl_isSuffixOf s = L.isSuffixOf s`eqP` TL.isSuffixOf (packS s)
231
233
t_isInfixOf s = L. isInfixOf s `eqP` T. isInfixOf (packS s)
232
234
tl_isInfixOf s = L. isInfixOf s `eqP` TL. isInfixOf (packS s)
233
235
236
+ -- | Generator for substrings that keeps the element order.
237
+ -- Aka: "1234567890" -> "245680"
238
+ genOrdSubseq :: T. Text -> Gen T. Text
239
+ genOrdSubseq txt =
240
+ T. pack . transform <$> genTransformMap
241
+ where
242
+
243
+ pickN :: Gen Int
244
+ pickN =
245
+ choose (0 , T. length txt)
246
+
247
+ pickNs :: Gen [Int ]
248
+ pickNs =
249
+ fmap (sort . nub) $ (`replicateM` pickN) =<< pickN
250
+
251
+ growInst :: [Bool ] -> Int -> [Bool ]
252
+ growInst ls n =
253
+ ls
254
+ <> take (length ls - pred n) [True .. ]
255
+ <> [False ]
256
+
257
+ mkTransformInst :: [Bool ] -> [Int ] -> [Bool ]
258
+ mkTransformInst bls [] =
259
+ bls
260
+ <> take (T. length txt - length bls) [True .. ]
261
+ mkTransformInst bls (i: is) =
262
+ mkTransformInst
263
+ (growInst bls i)
264
+ is
265
+
266
+ mkTransformMap :: [a ] -> [Int ] -> [(a , Bool )]
267
+ mkTransformMap ls ixs =
268
+ zip ls (mkTransformInst mempty ixs)
269
+
270
+ genTransformMap :: (Gen [(Char , Bool )])
271
+ genTransformMap = fmap (mkTransformMap $ T. unpack txt) pickNs
272
+
273
+ transform :: [(Char , Bool )] -> [Char ]
274
+ transform =
275
+ foldr (\ (c, b) as -> as <> if b then [c] else mempty ) mempty
276
+
234
277
t_stripPrefix s = (fmap packS . L. stripPrefix s) `eqP` T. stripPrefix (packS s)
235
278
tl_stripPrefix s = (fmap packS . L. stripPrefix s) `eqP` TL. stripPrefix (packS s)
236
279
0 commit comments