@@ -1,12 +1,11 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, UnboxedTuples #-}
22
33-- |
44-- Module : Data.Text.Read
5−-- Copyright : (c) 2010 Bryan O'Sullivan
5+-- Copyright : (c) 2010, 2011 Bryan O'Sullivan
66--
77-- License : BSD-style
8−-- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,
9−-- duncan@haskell.org
8+-- Maintainer : bos@serpentine.com
109-- Stability : experimental
1110-- Portability : GHC
1211--
@@ -23,8 +22,11 @@ module Data.Text.Read
2322
2423import Control.Monad (liftM)
2524import Data.Char (isDigit, isHexDigit, ord)
25+import Data.Int (Int8, Int16, Int32, Int64)
2626import Data.Ratio ((%))
2727import Data.Text as T
28+import Data.Text.Private (span_)
29+import Data.Word (Word, Word8, Word16, Word32, Word64)
2830
2931-- | Read some text. If the read succeeds, return its value and the
3032-- remaining text, otherwise an error message.
@@ -43,11 +45,20 @@ type Reader a = Text -> Either String (a,Text)
4345-- 'Integer' for your result type.
4446decimal :: Integral a => Reader a
4547{-# SPECIALIZE decimal :: Reader Int #-}
48+{-# SPECIALIZE decimal :: Reader Int8 #-}
49+{-# SPECIALIZE decimal :: Reader Int16 #-}
50+{-# SPECIALIZE decimal :: Reader Int32 #-}
51+{-# SPECIALIZE decimal :: Reader Int64 #-}
4652{-# SPECIALIZE decimal :: Reader Integer #-}
53+{-# SPECIALIZE decimal :: Reader Word #-}
54+{-# SPECIALIZE decimal :: Reader Word8 #-}
55+{-# SPECIALIZE decimal :: Reader Word16 #-}
56+{-# SPECIALIZE decimal :: Reader Word32 #-}
57+{-# SPECIALIZE decimal :: Reader Word64 #-}
4758decimal txt
4859 | T.null h = Left "input does not start with a digit"
4960 | otherwise = Right (T.foldl' go 0 h, t)
50− where (h,t) = T.span isDigit txt
61+ where (# h,t #) = span_ isDigit txt
5162 go n d = (n * 10 + fromIntegral (digitToInt d))
5263
5364-- | Read a hexadecimal integer, consisting of an optional leading
@@ -64,7 +75,16 @@ decimal txt
6475-- 'Integer' for your result type.
6576hexadecimal :: Integral a => Reader a
6677{-# SPECIALIZE hexadecimal :: Reader Int #-}
78+{-# SPECIALIZE hexadecimal :: Reader Int8 #-}
79+{-# SPECIALIZE hexadecimal :: Reader Int16 #-}
80+{-# SPECIALIZE hexadecimal :: Reader Int32 #-}
81+{-# SPECIALIZE hexadecimal :: Reader Int64 #-}
6782{-# SPECIALIZE hexadecimal :: Reader Integer #-}
83+{-# SPECIALIZE hexadecimal :: Reader Word #-}
84+{-# SPECIALIZE hexadecimal :: Reader Word8 #-}
85+{-# SPECIALIZE hexadecimal :: Reader Word16 #-}
86+{-# SPECIALIZE hexadecimal :: Reader Word32 #-}
87+{-# SPECIALIZE hexadecimal :: Reader Word64 #-}
6888hexadecimal txt
6989 | h == "0x" || h == "0X" = hex t
7090 | otherwise = hex txt
@@ -72,11 +92,20 @@ hexadecimal txt
7292
7393hex :: Integral a => Reader a
7494{-# SPECIALIZE hex :: Reader Int #-}
95+{-# SPECIALIZE hex :: Reader Int8 #-}
96+{-# SPECIALIZE hex :: Reader Int16 #-}
97+{-# SPECIALIZE hex :: Reader Int32 #-}
98+{-# SPECIALIZE hex :: Reader Int64 #-}
7599{-# SPECIALIZE hex :: Reader Integer #-}
100+{-# SPECIALIZE hex :: Reader Word #-}
101+{-# SPECIALIZE hex :: Reader Word8 #-}
102+{-# SPECIALIZE hex :: Reader Word16 #-}
103+{-# SPECIALIZE hex :: Reader Word32 #-}
104+{-# SPECIALIZE hex :: Reader Word64 #-}
76105hex txt
77106 | T.null h = Left "input does not start with a hexadecimal digit"
78107 | otherwise = Right (T.foldl' go 0 h, t)
79− where (h,t) = T.span isHexDigit txt
108+ where (# h,t #) = span_ isHexDigit txt
80109 go n d = (n * 16 + fromIntegral (hexDigitToInt d))
81110
82111hexDigitToInt :: Char -> Int
@@ -114,7 +143,7 @@ signed f = runP (signa (P f))
114143--
115144-- >rational "3.foo" == Right (3.0, ".foo")
116145-- >rational "3e" == Right (3.0, "e")
117−rational :: RealFloat a => Reader a
146+rational :: Fractional a => Reader a
118147{-# SPECIALIZE rational :: Reader Double #-}
119148rational = floaty $ \real frac fracDenom -> fromRational $
120149 real % 1 + frac % fracDenom
@@ -138,6 +167,10 @@ double = floaty $ \real frac fracDenom ->
138167
139168signa :: Num a => Parser a -> Parser a
140169{-# SPECIALIZE signa :: Parser Int -> Parser Int #-}
170+{-# SPECIALIZE signa :: Parser Int8 -> Parser Int8 #-}
171+{-# SPECIALIZE signa :: Parser Int16 -> Parser Int16 #-}
172+{-# SPECIALIZE signa :: Parser Int32 -> Parser Int32 #-}
173+{-# SPECIALIZE signa :: Parser Int64 -> Parser Int64 #-}
141174{-# SPECIALIZE signa :: Parser Integer -> Parser Integer #-}
142175signa p = do
143176 sign <- perhaps '+' $ char (\c -> c == '-' || c == '+')
@@ -168,7 +201,7 @@ char p = P $ \t -> case T.uncons t of
168201
169202data T = T !Integer !Int
170203
171−floaty :: RealFloat a => (Integer -> Integer -> Integer -> a) -> Reader a
204+floaty :: Fractional a => (Integer -> Integer -> Integer -> a) -> Reader a
172205{-# INLINE floaty #-}
173206floaty f = runP $ do
174207 sign <- perhaps '+' $ char (\c -> c == '-' || c == '+')