Commits

Bryan O'Sullivan  committed 45883bc

Implement strip, stripLeft, and stripRight helpers.

  • Participants
  • Parent commits 0aaaa15

Comments (0)

Files changed (1)

File Data/Text.hs

     , dropWhile
     , dropAfter
     , dropAround
+    , strip
+    , stripLeft
+    , stripRight
     , splitAt
     , span
     , break
 -- | /O(n)/ 'dropAfter' @p@ @t@ returns the prefix remaining after
 -- dropping characters that fail the predicate @p@ from the end of
 -- @t@.  This function is subject to array fusion.
---
--- For example, to drop trailing white space:
---
--- > dropAfter isSpace
 dropAfter :: (Char -> Bool) -> Text -> Text
 dropAfter p t@(Text arr off len) = loop (len-1) len
   where loop !i !l | l <= 0    = empty
 -- 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)/ Remove leading white space from a string.  Equivalent to:
+--
+-- > dropWhile isSpace
+stripLeft :: Text -> Text
+stripLeft = dropWhile isSpace
+{-# INLINE [1] stripLeft #-}
+
+-- | /O(n)/ Remove trailing white space from a string.  Equivalent to:
+--
+-- > dropAfter isSpace
+stripRight :: Text -> Text
+stripRight = dropAfter isSpace
+{-# INLINE [1] stripRight #-}
+
+-- | /O(n)/ Remove leading and trailing white space from a string.
+-- Equivalent to:
+--
+-- > dropAround isSpace
+strip :: Text -> Text
+strip = dropAround isSpace
+{-# INLINE [1] strip #-}
+
 -- | /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)@.