on master help sign in
Rename Lex to Read

--HG-- extra : convert_revision : c0ad41f04418dcc15f4b71cfbd4ecd1e77b0d146

commitb18bfc1403cefad7948a2dee0538347b0df3475d authorBryan O'Sullivan <bos@serpentine.com> dateSun 10 Oct 2010, 02:42 (16 years ago) parent446eb32
3 files changed+26−26
Data/Text/Lex.hs → Data/Text/Read.hs+19−19 tests/benchmarks/ReadNumbers.hs+6−6 text.cabal+1−1
Data/Text/Lex.hs → Data/Text/Read.hsrenamed+19−19
@@ -1,7 +1,7 @@
11{-# LANGUAGE OverloadedStrings #-}
22
33-- |
4−-- Module : Data.Text.Lex
4+-- Module : Data.Text.Read
55-- Copyright : (c) 2010 Bryan O'Sullivan
66--
77-- License : BSD-style
@@ -10,10 +10,10 @@
1010-- Stability : experimental
1111-- Portability : GHC
1212--
13−-- Lexing functions used frequently when handling textual data.
14−module Data.Text.Lex
13+-- Reading functions used frequently when handling textual data.
14+module Data.Text.Read
1515 (
16− Lexer
16+ Reader
1717 , decimal
1818 , hexadecimal
1919 , signed
@@ -28,15 +28,15 @@ import Data.Text as T
2828
2929-- | Read some text, and if the read succeeds, return its value and
3030-- the remaining text.
31−type Lexer a = Text -> Either String (a,Text)
31+type Reader a = Text -> Either String (a,Text)
3232
3333-- | Read a decimal integer.
3434--
3535-- This function does not handle leading sign characters. If you need
3636-- to handle signed input, use @'signed' 'decimal'@.
37−decimal :: Integral a => Lexer a
38−{-# SPECIALIZE decimal :: Lexer Int #-}
39−{-# SPECIALIZE decimal :: Lexer Integer #-}
37+decimal :: Integral a => Reader a
38+{-# SPECIALIZE decimal :: Reader Int #-}
39+{-# SPECIALIZE decimal :: Reader Integer #-}
4040decimal txt
4141 | T.null h = Left "no digits in input"
4242 | otherwise = Right (T.foldl' go 0 h, t)
@@ -48,9 +48,9 @@ decimal txt
4848--
4949-- This function does not handle leading sign characters. If you need
5050-- to handle signed input, use @'signed' 'hexadecimal'@.
51−hexadecimal :: Integral a => Lexer a
52−{-# SPECIALIZE hex :: Lexer Int #-}
53−{-# SPECIALIZE hex :: Lexer Integer #-}
51+hexadecimal :: Integral a => Reader a
52+{-# SPECIALIZE hex :: Reader Int #-}
53+{-# SPECIALIZE hex :: Reader Integer #-}
5454hexadecimal txt
5555 | T.toLower h == "0x" = hex t
5656 | otherwise = hex txt
@@ -58,15 +58,15 @@ hexadecimal txt
5858
5959-- | Read a leading sign character (@\'-\'@ or @\'+\'@) and apply it
6060-- to the result of applying the given reader.
61−signed :: Num a => Lexer a -> Lexer a
61+signed :: Num a => Reader a -> Reader a
6262{-# INLINE signed #-}
6363signed f = runP (signa (P f))
6464
6565-- | Read a rational number.
6666--
6767-- This function accepts an optional leading sign character.
68−rational :: RealFloat a => Lexer a
69−{-# SPECIALIZE rational :: Lexer Double #-}
68+rational :: RealFloat a => Reader a
69+{-# SPECIALIZE rational :: Reader Double #-}
7070rational = floaty $ \real frac fracDenom -> fromRational $
7171 real % 1 + frac % fracDenom
7272
@@ -82,14 +82,14 @@ rational = floaty $ \real frac fracDenom -> fromRational $
8282-- results, but for the remaining 5.8%, this function loses precision
8383-- around the 15th decimal place. For 0.001% of numbers, this
8484-- function will lose precision at the 13th or 14th decimal place.
85−double :: Lexer Double
85+double :: Reader Double
8686double = floaty $ \real frac fracDenom ->
8787 fromIntegral real +
8888 fromIntegral frac / fromIntegral fracDenom
8989
90−hex :: Integral a => Lexer a
91−{-# SPECIALIZE hex :: Lexer Int #-}
92−{-# SPECIALIZE hex :: Lexer Integer #-}
90+hex :: Integral a => Reader a
91+{-# SPECIALIZE hex :: Reader Int #-}
92+{-# SPECIALIZE hex :: Reader Integer #-}
9393hex txt
9494 | T.null h = Left "no digits in input"
9595 | otherwise = Right (T.foldl' go 0 h, t)
@@ -135,7 +135,7 @@ char p = P $ \t -> case T.uncons t of
135135
136136data T = T !Integer !Int
137137
138−floaty :: RealFloat a => (Integer -> Integer -> Integer -> a) -> Lexer a
138+floaty :: RealFloat a => (Integer -> Integer -> Integer -> a) -> Reader a
139139{-# INLINE floaty #-}
140140floaty f = runP $ do
141141 real <- signa (P decimal)
tests/benchmarks/ReadNumbers.hs+6−6
@@ -2,20 +2,20 @@
22import Debug.Trace
33import Control.Monad
44import Data.List
5−import qualified Data.Text.Lex as T
5+import qualified Data.Text.Read as T
66import qualified Data.Text.IO as T
77import qualified Data.Text as T
88import System.Environment
99
10−dec = T.signed T.decimal :: T.Lexer Int
10+dec = T.signed T.decimal :: T.Reader Int
1111
12−hex = T.signed T.hexadecimal :: T.Lexer Int
12+hex = T.signed T.hexadecimal :: T.Reader Int
1313
14−double = T.double :: T.Lexer Double
14+double = T.double :: T.Reader Double
1515
1616def = double
1717
18−read1 :: Num a => T.Lexer a -> T.Text -> a
18+read1 :: Num a => T.Reader a -> T.Text -> a
1919read1 reader = foldl' go 0 . T.lines
2020 where go z t = case reader t of
2121 Left err -> error err
@@ -33,7 +33,7 @@ paranoid = foldr go [] . T.lines
3333 then abs ((a-b)/a) : xs
3434 else xs
3535
36−read2 :: Num a => T.Lexer a -> T.Text -> a
36+read2 :: Num a => T.Reader a -> T.Text -> a
3737read2 reader = go 0
3838 where
3939 go !i t
text.cabal+1−1
@@ -74,11 +74,11 @@ library
7474 Data.Text.Encoding.Error
7575 Data.Text.Foreign
7676 Data.Text.IO
77− Data.Text.Lex
7877 Data.Text.Lazy
7978 Data.Text.Lazy.Builder
8079 Data.Text.Lazy.Encoding
8180 Data.Text.Lazy.IO
81+ Data.Text.Read
8282 other-modules:
8383 Data.Text.Array
8484 Data.Text.Encoding.Fusion