-- | String functions.
module Music.Theory.String where

import Data.Char {- base -}
import Data.List {- base -}

-- | Case-insensitive '=='.
--
-- > map (str_eq_ci "ci") (words "CI ci Ci cI")
str_eq_ci :: String -> String -> Bool
str_eq_ci :: [Char] -> [Char] -> Bool
str_eq_ci [Char]
x [Char]
y = forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toUpper [Char]
x forall a. Eq a => a -> a -> Bool
== forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toUpper [Char]
y

-- | Remove @\r@.
filter_cr :: String -> String
filter_cr :: [Char] -> [Char]
filter_cr = forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => a -> a -> Bool
(==) Char
'\r')

-- | Delete trailing 'Char' where 'isSpace' holds.
--
-- > delete_trailing_whitespace "   str   " == "   str"
-- > delete_trailing_whitespace "\t\n        \t\n" == ""
delete_trailing_whitespace :: String -> String
delete_trailing_whitespace :: [Char] -> [Char]
delete_trailing_whitespace = forall a. [a] -> [a]
reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
reverse

{- | Variant of 'unwords' that does not write spaces for NIL elements.

> unwords_nil [] == ""
> unwords_nil ["a"] == "a"
> unwords_nil ["a",""] == "a"
> unwords_nil ["a","b"] == "a b"
> unwords_nil ["a","","b"] == "a b"
> unwords_nil ["a","","","b"] == "a b"
> unwords_nil ["a","b",""] == "a b"
> unwords_nil ["a","b","",""] == "a b"
> unwords_nil ["","a","b"] == "a b"
> unwords_nil ["","","a","b"] == "a b"
-}
unwords_nil :: [String] -> String
unwords_nil :: [[Char]] -> [Char]
unwords_nil = [[Char]] -> [Char]
unwords forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null)

-- | Variant of 'unlines' that does not write empty lines for NIL elements.
unlines_nil :: [String] -> String
unlines_nil :: [[Char]] -> [Char]
unlines_nil = [[Char]] -> [Char]
unlines forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null)

{- | unlines without a trailing newline.

> unlines (words "a b c") == "a\nb\nc\n"
> unlinesNoTrailingNewline (words "a b c") == "a\nb\nc"
-}
unlinesNoTrailingNewline :: [String] -> String
unlinesNoTrailingNewline :: [[Char]] -> [Char]
unlinesNoTrailingNewline = forall a. [a] -> [[a]] -> [a]
intercalate [Char]
"\n"

{- | Capitalise first character of word.

> capitalise "freqShift" == "FreqShift"
-}
capitalise :: String -> String
capitalise :: [Char] -> [Char]
capitalise [Char]
x = Char -> Char
toUpper (forall a. [a] -> a
head [Char]
x) forall a. a -> [a] -> [a]
: forall a. [a] -> [a]
tail [Char]
x

{- | Downcase first character of word.

> unCapitalise "FreqShift" == "freqShift"
-}
unCapitalise :: String -> String
unCapitalise :: [Char] -> [Char]
unCapitalise [Char]
x = Char -> Char
toLower (forall a. [a] -> a
head [Char]
x) forall a. a -> [a] -> [a]
: forall a. [a] -> [a]
tail [Char]
x

-- | Apply function at each line of string.
--
-- > on_lines reverse "ab\ncde\nfg" == "ba\nedc\ngf\n"
on_lines :: (String -> String) -> String -> String
on_lines :: ([Char] -> [Char]) -> [Char] -> [Char]
on_lines [Char] -> [Char]
f = [[Char]] -> [Char]
unlines forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map [Char] -> [Char]
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines