-- I'm just copying this until I figure out what I'm doing with Antelude. module Glyph.Extra.String where ---------------------------------------------------------------------------------------------------- import qualified Data.Char as Char import qualified Data.List as List ---------------------------------------------------------------------------------------------------- -- | Join a 'List' of 'String's together with no additional characters join :: List String -> String join = List.intercalate "" -- | Given a "replaceable" substring to replace, and a "replacement" substring, replace all instances of the "replaceable" within a 'String' with the "replacement". replace :: String -> String -> String -> String replace replaceThis replaceWith = replaceMany [(replaceThis, replaceWith)] -- | Replace a bunch of things at once replaceMany :: List (String, String) -> String -> String replaceMany rules = walk where usableRules :: List (String, String) usableRules = List.filter (\(replaceThis, _) -> List.null replaceThis |> not) rules matchAt :: String -> Maybe (String, String) matchAt string = List.find (\(replaceThis, _) -> replaceThis `List.isPrefixOf` string) usableRules walk :: String -> String walk [] = [] walk string@(character : remaining) = case matchAt string of Just (replaceThis, replaceWith) -> replaceWith <> walk (List.drop (List.length replaceThis) string) Nothing -> character : walk remaining isNumeric :: String -> Bool isNumeric = List.all Char.isDigit isPrintable :: String -> Bool isPrintable str = let nonChars :: List String nonChars = [ "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "
" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" ] in List.elem str nonChars |> not toNumber :: List Char -> Natural toNumber chars = let digitValue :: Char -> Natural digitValue char | Char.isDigit char = Char.ord char - Char.ord '0' |> toEnum | otherwise = 0 in List.foldl' (\accum char -> accum * 10 + digitValue char) 0 chars