on master help sign in
Fix string to positive number conversion (#3129)

Currently "-123" is being parsed as Just 0 for Nats, which is wrong. It should return Nothing for non numbers and negative numbers.

commitea0966e72ae1d0c82e81b719afb03a286fecd69b authorSaurabh Rawat <eklavya@users.noreply.github.com> dateSun 24 Apr 2016, 20:13 (11 years ago) parent7c5819b
1 file changed+5−3
libs/base/Data/String.idr+5−3
libs/base/Data/String.idr+5−3
@@ -23,10 +23,12 @@ parsePositive s = parsePosTrimmed (trim s)
2323 where
2424 parsePosTrimmed s with (strM s)
2525 parsePosTrimmed "" | StrNil = Nothing
26+ parsePosTrimmed (strCons '+' xs) | (StrCons '+' xs) =
27+ map fromInteger (parseNumWithoutSign (unpack xs) 0)
2628 parsePosTrimmed (strCons x xs) | (StrCons x xs) =
27− if (x == '+')
28− then map fromInteger (parseNumWithoutSign (unpack xs) 0)
29− else map fromInteger (parseNumWithoutSign (unpack xs) (cast (ord x - ord '0')))
29+ if (x >= '0' && x <= '9')
30+ then map fromInteger (parseNumWithoutSign (unpack xs) (cast (ord x - ord '0')))
31+ else Nothing
3032
3133
3234||| Convert a number string to a Num.