Efficient attoparsec parser combinating general parsers and anyChar

47 views Asked by At

This is similar to my previous question Attoparsec efficient parser for multiple char, but I oversimplified the example parser I provided. I really apologize if it is considered spamming.

If I define

charToText :: Char -> Text
charToText c = pack [c]

parseEqStarMonad :: Parser Text
-- I will not define it here, but it could be any Parser Text

envParser :: Parser Text
envParser = mconcat <$> many (parseEqStarMonad <|> (charToText <$> anyChar))

it seems to me that lifting charToText is inefficient, because for each character matched charToText creates a singleton list to pack it as a Text.

Is there or more efficient way to perform this parsing ?

1

There are 1 answers

0
Li-yao Xia On

You can use singleton c instead of pack [c]. But beyond that, I don't see any obvious improvement.

This is fine if mconcat is used sparsely. If you need to append a lot of Text together, you should use a Builder instead.