-
Notifications
You must be signed in to change notification settings - Fork 6
/
Utils.hs
30 lines (26 loc) · 973 Bytes
/
Utils.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{-# OPTIONS_GHC -Wall #-}
-----------------------------------------------------------------------------
-- |
-- Copyright : (C) 2014 Edward Kmett
-- License : BSD-style (see the file LICENSE.gl)
-- Maintainer : Edward Kmett <ekmett@gmail.com>
-- Stability : experimental
-- Portability : portable
--
-- Simple string munging utilities
----------------------------------------------------------------------------
module Utils (splitOn, strip, replace) where
import Data.Char (isSpace)
import Data.List (intercalate, isPrefixOf)
strip :: String -> String
strip = dropWhile isSpace . reverse . dropWhile isSpace . reverse
replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace a b = intercalate b . splitOn a
splitOn :: Eq a => [a] -> [a] -> [[a]]
splitOn _ [] = []
splitOn xs ys0 = go [] ys0 where
go acc ys
| isPrefixOf xs ys = reverse acc : go [] (drop (length xs) ys)
| otherwise = case ys of
z:zs -> go (z:acc) zs
[] -> [reverse acc]