-- | String operations, part of the "Useful" module.
module Useful.String where

import Useful.General

-- | Strips whitespace from either side of a string.
--
-- > $ strip "   asdsadasds   \r\n" \n
-- > "asdsadasds"	
strip :: String -> String
strip = stripr . stripl

-- | Strips whitespace from the right of a string
--
-- > $ stripr "  asdioamlksd   \n\n" \n
-- > "  asdioamlksd"
stripr :: String -> String
stripr [] = []
stripr x
	|(last x) ? stripset = stripr (init x)
	|otherwise = x

-- | Strips whitespace from the left of a string
--
-- > $ stripl " \n\n  askdjnasdnaskd"
-- > "askdjnasdnaskd"
stripl :: String -> String
stripl [] = []
stripl (x:xs)
	|x ? stripset = stripl xs
	|otherwise = (x:xs)

-- | Alias of 'strip'
chomp :: String -> String
chomp = stripr

-- | List of whitespace characters: 
--
-- > [' ','\r','\n','\t']
stripset :: [Char]
stripset = [' ','\r','\n','\t']