Commits

Bryan O'Sullivan  committed 533a089

Add a dropAround function.

  • Participants
  • Parent commits 538a222

Comments (0)

Files changed (2)

File Data/Text.hs

     , takeWhile
     , dropWhile
     , dropAfter
+    , dropAround
     , splitAt
     , span
     , break
     S.reverse (S.dropWhile p (S.reverseStream t)) = dropAfter p t
   #-}
 
+-- | /O(n)/ 'dropAround' @p@ @t@ returns the substring remaining after
+-- dropping characters that fail the predicate @p@ from both the
+-- beginning and end of @t@.  This function is subject to array
+-- fusion.
+--
+-- For example, to drop leading and trailing white space:
+--
+-- > dropAround isSpace
+dropAround :: (Char -> Bool) -> Text -> Text
+dropAround p = dropWhile p . dropAfter p
+{-# INLINE [1] dropAround #-}
+
 -- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
 -- prefix of @t@ of length @n@, and whose second is the remainder of
 -- the string. It is equivalent to @('take' n t, 'drop' n t)@.

File tests/Properties.hs

 prop_TL_dropWhile p    = L.dropWhile p `eqP` (unpackS . S.dropWhile p)
 prop_S_dropAfter p     = T.dropAfter p `eq` (S.reverse . S.dropWhile p . S.reverseStream)
 prop_T_dropAfter p     = (T.reverse . T.dropWhile p . T.reverse) `eq` T.dropAfter p
+prop_T_dropAround p    = (T.dropWhile p . T.dropAfter p) `eq` T.dropAround p
 prop_T_splitAt n       = L.splitAt n   `eqP` (unpack2 . T.splitAt n)
 prop_TL_splitAt n      = L.splitAt n   `eqP` (unpack2 . TL.splitAt (fromIntegral n))
 prop_T_span p          = L.span p      `eqP` (unpack2 . T.span p)
   ("prop_TL_dropWhile", mytest prop_TL_dropWhile),
   ("prop_S_dropAfter", mytest prop_S_dropAfter),
   ("prop_T_dropAfter", mytest prop_T_dropAfter),
+  ("prop_T_dropAround", mytest prop_T_dropAround),
   ("prop_T_splitAt", mytest prop_T_splitAt),
   ("prop_T_span", mytest prop_T_span),
   ("prop_TL_span", mytest prop_TL_span),