on master help sign in
Rename Lex to Read
b18bfc1Bryan O'Sullivan
Data/Text/Lex.hs → Data/Text/Read.hsrenamed+19−19
@@ -1,7 +1,7 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2
2
3-- |
3-- |
4−-- Module : Data.Text.Lex
4+-- Module : Data.Text.Read
5-- Copyright : (c) 2010 Bryan O'Sullivan
5-- Copyright : (c) 2010 Bryan O'Sullivan
6--
6--
7-- License : BSD-style
7-- License : BSD-style
@@ -10,10 +10,10 @@
10-- Stability : experimental
10-- Stability : experimental
11-- Portability : GHC
11-- Portability : GHC
12--
12--
13−-- Lexing functions used frequently when handling textual data.
13+-- Reading functions used frequently when handling textual data.
14−module Data.Text.Lex
14+module Data.Text.Read
15 (
15 (
16− Lexer
16+ Reader
17 , decimal
17 , decimal
18 , hexadecimal
18 , hexadecimal
19 , signed
19 , signed
@@ -28,15 +28,15 @@ import Data.Text as T
28
28
29-- | Read some text, and if the read succeeds, return its value and
29-- | Read some text, and if the read succeeds, return its value and
30-- the remaining text.
30-- the remaining text.
31−type Lexer a = Text -> Either String (a,Text)
31+type Reader a = Text -> Either String (a,Text)
32
32
33-- | Read a decimal integer.
33-- | Read a decimal integer.
34--
34--
35-- This function does not handle leading sign characters. If you need
35-- This function does not handle leading sign characters. If you need
36-- to handle signed input, use @'signed' 'decimal'@.
36-- to handle signed input, use @'signed' 'decimal'@.
37−decimal :: Integral a => Lexer a
37+decimal :: Integral a => Reader a
38−{-# SPECIALIZE decimal :: Lexer Int #-}
38+{-# SPECIALIZE decimal :: Reader Int #-}
39−{-# SPECIALIZE decimal :: Lexer Integer #-}
39+{-# SPECIALIZE decimal :: Reader Integer #-}
40decimal txt
40decimal txt
41 | T.null h = Left "no digits in input"
41 | T.null h = Left "no digits in input"
42 | otherwise = Right (T.foldl' go 0 h, t)
42 | otherwise = Right (T.foldl' go 0 h, t)
@@ -48,9 +48,9 @@ decimal txt
48--
48--
49-- This function does not handle leading sign characters. If you need
49-- This function does not handle leading sign characters. If you need
50-- to handle signed input, use @'signed' 'hexadecimal'@.
50-- to handle signed input, use @'signed' 'hexadecimal'@.
51−hexadecimal :: Integral a => Lexer a
51+hexadecimal :: Integral a => Reader a
52−{-# SPECIALIZE hex :: Lexer Int #-}
52+{-# SPECIALIZE hex :: Reader Int #-}
53−{-# SPECIALIZE hex :: Lexer Integer #-}
53+{-# SPECIALIZE hex :: Reader Integer #-}
54hexadecimal txt
54hexadecimal txt
55 | T.toLower h == "0x" = hex t
55 | T.toLower h == "0x" = hex t
56 | otherwise = hex txt
56 | otherwise = hex txt
@@ -58,15 +58,15 @@ hexadecimal txt
58
58
59-- | Read a leading sign character (@\'-\'@ or @\'+\'@) and apply it
59-- | Read a leading sign character (@\'-\'@ or @\'+\'@) and apply it
60-- to the result of applying the given reader.
60-- to the result of applying the given reader.
61−signed :: Num a => Lexer a -> Lexer a
61+signed :: Num a => Reader a -> Reader a
62{-# INLINE signed #-}
62{-# INLINE signed #-}
63signed f = runP (signa (P f))
63signed f = runP (signa (P f))
64
64
65-- | Read a rational number.
65-- | Read a rational number.
66--
66--
67-- This function accepts an optional leading sign character.
67-- This function accepts an optional leading sign character.
68−rational :: RealFloat a => Lexer a
68+rational :: RealFloat a => Reader a
69−{-# SPECIALIZE rational :: Lexer Double #-}
69+{-# SPECIALIZE rational :: Reader Double #-}
70rational = floaty $ \real frac fracDenom -> fromRational $
70rational = floaty $ \real frac fracDenom -> fromRational $
71 real % 1 + frac % fracDenom
71 real % 1 + frac % fracDenom
72
72
@@ -82,14 +82,14 @@ rational = floaty $ \real frac fracDenom -> fromRational $
82-- results, but for the remaining 5.8%, this function loses precision
82-- results, but for the remaining 5.8%, this function loses precision
83-- around the 15th decimal place. For 0.001% of numbers, this
83-- around the 15th decimal place. For 0.001% of numbers, this
84-- function will lose precision at the 13th or 14th decimal place.
84-- function will lose precision at the 13th or 14th decimal place.
85−double :: Lexer Double
85+double :: Reader Double
86double = floaty $ \real frac fracDenom ->
86double = floaty $ \real frac fracDenom ->
87 fromIntegral real +
87 fromIntegral real +
88 fromIntegral frac / fromIntegral fracDenom
88 fromIntegral frac / fromIntegral fracDenom
89
89
90−hex :: Integral a => Lexer a
90+hex :: Integral a => Reader a
91−{-# SPECIALIZE hex :: Lexer Int #-}
91+{-# SPECIALIZE hex :: Reader Int #-}
92−{-# SPECIALIZE hex :: Lexer Integer #-}
92+{-# SPECIALIZE hex :: Reader Integer #-}
93hex txt
93hex txt
94 | T.null h = Left "no digits in input"
94 | T.null h = Left "no digits in input"
95 | otherwise = Right (T.foldl' go 0 h, t)
95 | otherwise = Right (T.foldl' go 0 h, t)
@@ -135,7 +135,7 @@ char p = P $ \t -> case T.uncons t of
135
135
136data T = T !Integer !Int
136data T = T !Integer !Int
137
137
138−floaty :: RealFloat a => (Integer -> Integer -> Integer -> a) -> Lexer a
138+floaty :: RealFloat a => (Integer -> Integer -> Integer -> a) -> Reader a
139{-# INLINE floaty #-}
139{-# INLINE floaty #-}
140floaty f = runP $ do
140floaty f = runP $ do
141 real <- signa (P decimal)
141 real <- signa (P decimal)
tests/benchmarks/ReadNumbers.hs+6−6
@@ -2,20 +2,20 @@
2import Debug.Trace
2import Debug.Trace
3import Control.Monad
3import Control.Monad
4import Data.List
4import Data.List
5−import qualified Data.Text.Lex as T
5+import qualified Data.Text.Read as T
6import qualified Data.Text.IO as T
6import qualified Data.Text.IO as T
7import qualified Data.Text as T
7import qualified Data.Text as T
8import System.Environment
8import System.Environment
9
9
10−dec = T.signed T.decimal :: T.Lexer Int
10+dec = T.signed T.decimal :: T.Reader Int
11
11
12−hex = T.signed T.hexadecimal :: T.Lexer Int
12+hex = T.signed T.hexadecimal :: T.Reader Int
13
13
14−double = T.double :: T.Lexer Double
14+double = T.double :: T.Reader Double
15
15
16def = double
16def = double
17
17
18−read1 :: Num a => T.Lexer a -> T.Text -> a
18+read1 :: Num a => T.Reader a -> T.Text -> a
19read1 reader = foldl' go 0 . T.lines
19read1 reader = foldl' go 0 . T.lines
20 where go z t = case reader t of
20 where go z t = case reader t of
21 Left err -> error err
21 Left err -> error err
@@ -33,7 +33,7 @@ paranoid = foldr go [] . T.lines
33 then abs ((a-b)/a) : xs
33 then abs ((a-b)/a) : xs
34 else xs
34 else xs
35
35
36−read2 :: Num a => T.Lexer a -> T.Text -> a
36+read2 :: Num a => T.Reader a -> T.Text -> a
37read2 reader = go 0
37read2 reader = go 0
38 where
38 where
39 go !i t
39 go !i t
text.cabal+1−1
@@ -74,11 +74,11 @@ library
74 Data.Text.Encoding.Error
74 Data.Text.Encoding.Error
75 Data.Text.Foreign
75 Data.Text.Foreign
76 Data.Text.IO
76 Data.Text.IO
77− Data.Text.Lex
78 Data.Text.Lazy
77 Data.Text.Lazy
79 Data.Text.Lazy.Builder
78 Data.Text.Lazy.Builder
80 Data.Text.Lazy.Encoding
79 Data.Text.Lazy.Encoding
81 Data.Text.Lazy.IO
80 Data.Text.Lazy.IO
81+ Data.Text.Read
82 other-modules:
82 other-modules:
83 Data.Text.Array
83 Data.Text.Array
84 Data.Text.Encoding.Fusion
84 Data.Text.Encoding.Fusion