Skip to content

Commit 813989c

Browse files
committed
Text: add fun isSubsequenceOf
Function to detect ordered subsequences in the content.
1 parent 7b0f298 commit 813989c

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
@@ -1881,6 +1882,22 @@ isInfixOf needle haystack
18811882
| otherwise = not . L.null . indices needle $ haystack
18821883
{-# INLINE [1] isInfixOf #-}
18831884

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

0 commit comments

Comments
 (0)