{- split utility functions
 -
 - Copyright 2017 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

{-# OPTIONS_GHC -fno-warn-tabs #-}

module Utility.Split where

import Data.List (intercalate)
import Data.List.Split (splitOn)

-- | same as Data.List.Utils.split
--
-- intercalate x . splitOn x === id
split :: Eq a => [a] -> [a] -> [[a]]
split :: forall a. Eq a => [a] -> [a] -> [[a]]
split = forall a. Eq a => [a] -> [a] -> [[a]]
splitOn

-- | Split on a single character. This is over twice as fast as using
-- split on a list of length 1, while producing identical results. -}
splitc :: Eq c => c -> [c] -> [[c]]
splitc :: forall c. Eq c => c -> [c] -> [[c]]
splitc c
c [c]
s = case forall a. (a -> Bool) -> [a] -> ([a], [a])
break (forall a. Eq a => a -> a -> Bool
== c
c) [c]
s of
	([c]
i, c
_c:[c]
rest) -> [c]
i forall a. a -> [a] -> [a]
: forall c. Eq c => c -> [c] -> [[c]]
splitc c
c [c]
rest
	([c]
i, []) -> [c]
i forall a. a -> [a] -> [a]
: []

-- | same as Data.List.Utils.replace
replace :: Eq a => [a] -> [a] -> [a] -> [a] 
replace :: forall a. Eq a => [a] -> [a] -> [a] -> [a]
replace [a]
old [a]
new = forall a. [a] -> [[a]] -> [a]
intercalate [a]
new forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => [a] -> [a] -> [[a]]
split [a]
old

-- | Only traverses the list once while dropping the last n characters.
dropFromEnd :: Int -> [a] -> [a]
dropFromEnd :: forall a. Int -> [a] -> [a]
dropFromEnd Int
n [a]
l = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a b. a -> b -> a
const [a]
l (forall a. Int -> [a] -> [a]
drop Int
n [a]
l)