RosettaCodeData/Task/Tokenize-a-string-with-escaping/Haskell/tokenize-a-string-with-escaping-1.hs

8 lines
418 B
Haskell
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
splitEsc :: (Foldable t1, Eq t) => t -> t -> t1 t -> [[t]]
splitEsc sep esc = reverse . map reverse . snd . foldl process (0, [[]])
where process (st, r:rs) ch
| st == 0 && ch == esc = (1, r:rs)
| st == 0 && ch == sep = (0, []:r:rs)
| st == 1 && sep == esc && ch /= sep = (0, [ch]:r:rs)
| otherwise = (0, (ch:r):rs)