-----------------------------------------------------------------------------
-- |
-- Module      :  Data.CharSet.Posix.Ascii
-- Copyright   :  (c) Edward Kmett 2010
-- License     :  BSD3
-- Maintainer  :  ekmett@gmail.com
-- Stability   :  experimental
-- Portability :  portable
--
-------------------------------------------------------------------------------

module Data.CharSet.Posix.Ascii
    ( posixAscii
    , lookupPosixAsciiCharSet
    -- * Traditional POSIX ASCII \"classes\"
    , alnum, alpha, ascii, blank, cntrl, digit, graph, print, word, punct, space, upper, lower, xdigit
    ) where

import Prelude hiding (print)
import Data.Char
import Data.CharSet
import Data.HashMap.Lazy (HashMap)
import qualified Data.HashMap.Lazy as HashMap

alnum, alpha, ascii, blank, cntrl, digit, graph, print, word, punct, space, upper, lower, xdigit :: CharSet
alnum :: CharSet
alnum = CharSet
alpha CharSet -> CharSet -> CharSet
`union` CharSet
digit
alpha :: CharSet
alpha = CharSet
lower CharSet -> CharSet -> CharSet
`union` CharSet
upper
ascii :: CharSet
ascii = Char -> Char -> CharSet
range Char
'\x00' Char
'\x7f'
blank :: CharSet
blank = String -> CharSet
fromList String
" \t"
cntrl :: CharSet
cntrl = Char -> CharSet -> CharSet
insert Char
'\x7f' (CharSet -> CharSet) -> CharSet -> CharSet
forall a b. (a -> b) -> a -> b
$ Char -> Char -> CharSet
range Char
'\x00' Char
'\x1f'
digit :: CharSet
digit = Char -> Char -> CharSet
range Char
'0' Char
'9'
lower :: CharSet
lower = Char -> Char -> CharSet
range Char
'a' Char
'z'
upper :: CharSet
upper = Char -> Char -> CharSet
range Char
'A' Char
'Z'
graph :: CharSet
graph = Char -> Char -> CharSet
range Char
'\x21' Char
'\x7e'
print :: CharSet
print = Char -> CharSet -> CharSet
insert Char
'\x20' CharSet
graph
word :: CharSet
word  = Char -> CharSet -> CharSet
insert Char
'_' CharSet
alnum
punct :: CharSet
punct = String -> CharSet
fromList String
"-!\"#$%&'()*+,./:;<=>?@[\\]^_`{|}~"
space :: CharSet
space = String -> CharSet
fromList String
" \t\r\n\v\f"
xdigit :: CharSet
xdigit = CharSet
digit CharSet -> CharSet -> CharSet
`union` Char -> Char -> CharSet
range Char
'a' Char
'f' CharSet -> CharSet -> CharSet
`union` Char -> Char -> CharSet
range Char
'A' Char
'F'

-- :digit:, etc.
posixAscii :: HashMap String CharSet
posixAscii :: HashMap String CharSet
posixAscii = [(String, CharSet)] -> HashMap String CharSet
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
    [ (String
"alnum", CharSet
alnum)
    , (String
"alpha", CharSet
alpha)
    , (String
"ascii", CharSet
ascii)
    , (String
"blank", CharSet
blank)
    , (String
"cntrl", CharSet
cntrl)
    , (String
"digit", CharSet
digit)
    , (String
"graph", CharSet
graph)
    , (String
"print", CharSet
print)
    , (String
"word",  CharSet
word)
    , (String
"punct", CharSet
punct)
    , (String
"space", CharSet
space)
    , (String
"upper", CharSet
upper)
    , (String
"lower", CharSet
lower)
    , (String
"xdigit", CharSet
xdigit)
    ]

lookupPosixAsciiCharSet :: String -> Maybe CharSet
lookupPosixAsciiCharSet :: String -> Maybe CharSet
lookupPosixAsciiCharSet String
s = String -> HashMap String CharSet -> Maybe CharSet
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup ((Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
Prelude.map Char -> Char
toLower String
s) HashMap String CharSet
posixAscii