Skip to content

Commit d09e477

Browse files
committed
Text: add fun isSubsequenceOf
Function to detect ordered subsequences in the content.
1 parent ff31f14 commit d09e477

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/Data/Text.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ module Data.Text
167167
, isPrefixOf
168168
, isSuffixOf
169169
, isInfixOf
170+
, isSubsequenceOf
170171

171172
-- ** View patterns
172173
, stripPrefix
@@ -1877,6 +1878,22 @@ isInfixOf needle haystack
18771878
| otherwise = not . L.null . indices needle $ haystack
18781879
{-# INLINE [1] isInfixOf #-}
18791880

1881+
-- | The 'isSubsequenceOf' function takes two 'Text's and returns
1882+
-- 'True' iff the first is a subsequence of a second.
1883+
-- (characters of the first argument appear in same sequential order in
1884+
-- the second, to say if first argument that can be derived by deleting some
1885+
-- or no elements from the second).
1886+
isSubsequenceOf :: Text -> Text -> Bool
1887+
isSubsequenceOf sf tf
1888+
| length sf > length tf = False
1889+
| otherwise = subseqOf sf tf
1890+
where
1891+
subseqOf s t
1892+
| null s = True
1893+
| null t = False
1894+
| unsafeHead s == unsafeHead t = subseqOf (unsafeTail s) (unsafeTail t)
1895+
| otherwise = subseqOf s $ unsafeTail t
1896+
18801897
-------------------------------------------------------------------------------
18811898
-- * View patterns
18821899

0 commit comments

Comments
 (0)