module Text.Proton.XmlInternal where
matches :: String -> Char -> Bool
matches [] _ = False
matches (x:xs) c =
(x == c) || matches xs c
isWhitespace :: Char -> Bool
isWhitespace = matches " \n\t\r"
spanUntil :: (a -> Bool) -> [a] -> ([a], [a])
spanUntil _ [] = ([], [])
spanUntil chk (x:xs) =
if chk x
then ([ x ], xs)
else do
let (hd, tl) = spanUntil chk xs
(x : hd, tl)
splitOn :: Char -> String -> (String, String)
splitOn char s = do
let (splitA, splitB) = span (/=char) s
if not (null splitB)
then (splitA, tail splitB)
else (splitA, splitB)
splitUntilClose :: String -> (String, String)
splitUntilClose "" = ("", "")
splitUntilClose (c:s) = splitUntilClose' s c ""
splitUntilClose' :: String -> Char -> String -> (String, String)
splitUntilClose' "" _ first = (first, "")
splitUntilClose' (c1 : s) untilc first
| s == "" =
if c1 == untilc
then (first, "")
else (first ++ [c1], "")
| c1 == untilc && (first == "" || last first /= '\\') =
(first, s)
| otherwise =
do let c2 = head s
if c2 == untilc && c1 /= '\\'
then (first ++ [c1], tail s)
else splitUntilClose' s untilc (first ++ [c1])
splitText :: String -> [String]
splitText [] = []
splitText (x : xs)
| x == '<' =
do let (first, rest) = spanUntil (== '>') xs
(x : first) : splitText rest
| isWhitespace x =
do let (first, rest) = span isWhitespace xs
(x : first) : splitText rest
| otherwise =
do let (first, rest) = span (/= '<') xs
(x : first) : splitText rest