Skip to content

Instantly share code, notes, and snippets.

@banacorn
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banacorn/04aa36742bc651ac2570 to your computer and use it in GitHub Desktop.
Save banacorn/04aa36742bc651ac2570 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
import Data.Attoparsec.Text
import Data.Text (Text)
import qualified Data.Text as T
import Prelude hiding (take)
input :: Text
input = "𝟘a"
-- homemade takeTill
takeTill' :: (Char -> Bool) -> Parser Text
takeTill' f = do
result <- peekChar
case result of
Just ch -> do
if f ch
then return T.empty
else do
ch' <- take 1
rest <- takeTill' f
return (ch' `T.append` rest)
Nothing -> return T.empty
main :: IO ()
main = do
putStrLn "== input string =="
print input
putStrLn "== takeTill =="
parseTest (takeTill ((==) 'a')) input >>= print
putStrLn "== takeTill' (homemade) =="
parseTest (takeTill' ((==) 'a')) input >>= print
== input string ==
"\120792a"
== takeTill ==
Done "\57304a" "\120792"
()
== takeTill' (homemade) ==
Done "a" "\120792"
()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment