    {- 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 :: [a] -> [a] -> [[a]]
    split = [a] -> [a] -> [[a]]
    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 :: c -> [c] -> [[c]]
    splitc c :: c
    c s :: [c]
    s = case (c -> Bool) -> [c] -> ([c], [c])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    break (c -> c -> Bool
    forall a. Eq a => a -> a -> Bool
    == c
    c) [c]
    s of
    	(i :: [c]
    i, _c :: c
    _c:rest :: [c]
    rest) -> [c]
    i [c] -> [[c]] -> [[c]]
    forall a. a -> [a] -> [a]
    : c -> [c] -> [[c]]
    forall c. Eq c => c -> [c] -> [[c]]
    splitc c
    c [c]
    rest
    	(i :: [c]
    i, []) -> [c]
    i [c] -> [[c]] -> [[c]]
    forall a. a -> [a] -> [a]
    : []

    -- | same as Data.List.Utils.replace
    replace :: Eq a => [a] -> [a] -> [a] -> [a]
    replace :: [a] -> [a] -> [a] -> [a]
    replace old :: [a]
    old new :: [a]
    new = [a] -> [[a]] -> [a]
    forall a. [a] -> [[a]] -> [a]
    intercalate [a]
    new ([[a]] -> [a]) -> ([a] -> [[a]]) -> [a] -> [a]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [a] -> [a] -> [[a]]
    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 :: Int -> [a] -> [a]
    dropFromEnd n :: Int
    n l :: [a]
    l = (a -> a -> a) -> [a] -> [a] -> [a]
    forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
    zipWith a -> a -> a
    forall a b. a -> b -> a
    const [a]
    l (Int -> [a] -> [a]
    forall a. Int -> [a] -> [a]
    drop Int
    n [a]
    l)