module Text.Regex.Applicative.Common (
digit
, hexDigit
, signed
, decimal
, hexadecimal
) where
import Data.Char
import Data.List (foldl')
import Text.Regex.Applicative
digit :: Num a => RE Char a
digit = fromIntegral . digitToInt <$> psym isDigit
hexDigit :: Num a => RE Char a
hexDigit = fromIntegral . digitToInt <$> psym isHexDigit
signed :: Num a => RE Char a -> RE Char a
signed p = sign <*> p
where
sign = id <$ sym '+'
<|> negate <$ sym '-'
<|> pure id
decimal :: Num a => RE Char a
decimal = foldl' (\d i -> d*10 + i) 0 <$> some digit
hexadecimal :: Num a => RE Char a
hexadecimal = foldl' (\d i -> d*16 + i) 0 <$> some hexDigit