Files
start-os/agent/src/Util/Text.hs
Aiden McClelland 95d3845906 0.2.5 initial commit
Makefile incomplete
2020-11-23 13:44:28 -07:00

25 lines
967 B
Haskell

module Util.Text where
import Data.Text ( strip )
import Startlude
import Text.Regex ( matchRegexAll
, mkRegex
, subRegex
)
-- | Behaves like Ruby gsub implementation
gsub :: Text -> Text -> Text -> Text
gsub regex replaceWith str = toS $ subRegex (mkRegex $ toS regex) (toS str) (toS replaceWith)
containsMatch :: Text -> Text -> Bool
containsMatch regex str = not . null $ getMatches regex str
getMatches :: Text -> Text -> [Text]
getMatches regex str
| str == "" = []
| otherwise = case matchRegexAll (mkRegex $ toS regex) (toS str) of
Nothing -> []
Just (_, "" , after, _) -> getMatches regex (toS . strip . toS $ after)
Just (_, match, after, _) -> toS match : getMatches regex (toS after)