From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Wed, 4 Feb 2015 03:51:22 +0000 (+0530)
Subject: initial parser for integers
X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/uri/flags/vdrive?a=commitdiff_plain;h=bb2674e1caabdb3fbf9ecf5a08923bd4761bb96f;p=functorrent.git

initial parser for integers
---

diff --git a/src/Bencode.hs b/src/Bencode.hs
index d6b5740..87da970 100644
--- a/src/Bencode.hs
+++ b/src/Bencode.hs
@@ -5,7 +5,6 @@ import qualified Data.ByteString.Char8 as BC
 import qualified Data.Map as M
 import qualified Text.Parsec.ByteString as ParsecBS
 import Text.ParserCombinators.Parsec
-import Text.ParserCombinators.Parsec.Char
 import Control.Applicative ((<*))
 import Data.Functor
 
@@ -35,12 +34,14 @@ bencStr = do _ <- spaces
 
 bencInt :: ParsecBS.Parser Integer
 bencInt = do _ <- spaces
-             ds <- between (char 'i') (char 'e') numbers1
+             ds <- between (char 'i') (char 'e') numbers
              return (read ds)
-               where numbers = do sign <- option ' ' (try (char '-'))
-                                  ds' <- many1 digit
-                                  return (sign : ds')
-                     numbers1 = many1 anyChar
+               where numbers = do d' <- (char '-' <|> digit)
+                                  if d' == '0'
+                                    then unexpected "numbers cannot be left-padded with zeros"
+                                    else
+                                      do ds' <- many1 digit
+                                         return (d' : ds')
 
 bencParser :: ParsecBS.Parser BVal
 bencParser = Bstr <$> bencStr <|>