on master help sign in
More lenient casting from string to number

Now whitespace is also allowed after the number being cast, like so: the Int $ cast " 123 " the Integer $ cast " 123 " the Double $ cast " 123 " Fixes #3601

commitfb4a909f9828c552fc4d6510c54f554c2780ede3 authorBjarki Ágúst Guðmundsson <suprdewd@gmail.com> dateThu 5 Jan 2017, 04:06 (10 years ago) parentd719f11
1 file changed+4−4
src/Idris/Primitives.hs+4−4
src/Idris/Primitives.hs+4−4
@@ -455,8 +455,8 @@ getInt _ = []
455455
456456strToInt :: IntTy -> [Const] -> Maybe Const
457457strToInt ity [Str x] = case reads x of
458− [(n,"")] -> Just $ toInt ity (n :: Integer)
459− _ -> Just $ I 0
458+ [(n,s)] -> Just $ if all isSpace s then toInt ity (n :: Integer) else I 0
459+ _ -> Just $ I 0
460460strToInt _ _ = Nothing
461461
462462intToFloat :: [Const] -> Maybe Const
@@ -481,8 +481,8 @@ c_floatToStr :: [Const] -> Maybe Const
481481c_floatToStr [Fl x] = Just $ Str (show x)
482482c_floatToStr _ = Nothing
483483c_strToFloat [Str x] = case reads x of
484− [(n,"")] -> Just $ Fl n
485− _ -> Just $ Fl 0
484+ [(n,s)] -> Just $ Fl (if all isSpace s then n else 0)
485+ _ -> Just $ Fl 0
486486c_strToFloat _ = Nothing
487487
488488p_fPrim :: (Double -> Double) -> [Const] -> Maybe Const