mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-26 18:31:52 +00:00
25 lines
967 B
Haskell
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)
|