Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,7 @@
|
|||
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)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
{-#Language LambdaCase #-}
|
||||
import Conduit
|
||||
|
||||
splitEscC :: (Monad m, Eq t) => t -> t -> Conduit t m [t]
|
||||
splitEscC sep esc = mapOutput reverse $ go True []
|
||||
where
|
||||
go notEsc b = await >>= \case
|
||||
Nothing -> yield b
|
||||
Just ch | notEsc && ch == esc -> go False b
|
||||
| notEsc && ch == sep -> yield b >> go True []
|
||||
| otherwise -> go True (ch:b)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
main = runConduit $
|
||||
yieldMany "one^|uno||three^^^^|four^^^|^cuatro|"
|
||||
.| splitEscC '|' '^'
|
||||
.| mapM_C print
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
import Data.Bool (bool)
|
||||
|
||||
------------------ TOKENIZE WITH ESCAPING ----------------
|
||||
|
||||
tokenize :: Char -> Char -> String -> [String]
|
||||
tokenize delim esc str =
|
||||
reverse $
|
||||
reverse <$> (token : list)
|
||||
where
|
||||
(token, list, _) =
|
||||
foldr
|
||||
( \x (aToken, aList, aEsc) ->
|
||||
let literal = not aEsc
|
||||
isEsc = literal && (x == esc)
|
||||
in bool
|
||||
( bool (x : aToken) aToken isEsc,
|
||||
aList,
|
||||
isEsc
|
||||
)
|
||||
([], aToken : aList, isEsc)
|
||||
(literal && x == delim)
|
||||
)
|
||||
([], [], False)
|
||||
(reverse str)
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_ print $
|
||||
tokenize
|
||||
'|'
|
||||
'^'
|
||||
"one^|uno||three^^^^|four^^^|^cuatro|"
|
||||
Loading…
Add table
Add a link
Reference in a new issue