{- |

The __American Standard Code for Information Interchange__ (ASCII) comprises a set of 128 characters, each represented by 7 bits. 33 of these characters are /'Control' codes/; a few of these are still in use, but most are obsolete relics of the early days of computing. The other 95 are /'Printable' characters/ such as letters and numbers, mostly corresponding to the keys on an American English keyboard.

Nowadays instead of ASCII we typically work with text using an encoding such as UTF-8 that can represent the entire Unicode character set, which includes over a hundred thousand characters and is not limited to the symbols of any particular writing system or culture. However, ASCII is still relevant to network protocols; for example, we can see it in the specification of [HTTP message headers](https://tools.ietf.org/html/rfc7230#section-1.2).

There is a convenient relationship between ASCII and Unicode: the ASCII characters are the first 128 characters of the much larger Unicode character set. The [C0 Controls and Basic Latin](https://www.unicode.org/charts/PDF/U0000.pdf) section of the Unicode standard contains a list of all the ASCII characters. You may also find this list replicated in the "ASCII.Char" module; each ASCII character corresponds to a constructor of the 'ASCII.Char' type.

We do not elaborate on the semantics of the control characters here, because this information is both obsolete and restricted by copyright law. It is described by a document entitled /"Coded Character Sets - 7-Bit American National Standard Code for Information Interchange (7-Bit ASCII)"/, published by American National Standards Institute (ANSI) and available for purchase [on their website](https://webstore.ansi.org/Standards/INCITS/INCITS1986R2012).

-}

module ASCII
  (
    {- * @Char@ -} {- $char -} Char,

    {- * Character classifications -}
    {- ** Print/control groups -} {- $groups -} Group (..), charGroup,  inGroup,
    {- ** Upper/lower case     -} {- $case   -} Case (..),  letterCase, isCase, toCaseChar, toCaseString,

    {- * Monomorphic conversions -} {- $monomorphicConversions -}
    {- ** @Int@        -} {- $intConversions           -} charToInt,               intToCharMaybe,               intToCharUnsafe,
    {- ** @Word8@      -} {- $word8Conversions         -} charToWord8,             word8ToCharMaybe,             word8ToCharUnsafe,
    {- ** @Char@       -} {- $unicodeCharConversions   -} charToUnicode,           unicodeToCharMaybe,           unicodeToCharUnsafe,
    {- ** @String@     -} {- $unicodeStringConversions -} charListToUnicodeString, unicodeStringToCharListMaybe, unicodeStringToCharListUnsafe,
    {- ** @Text@       -} {- $textConversions          -} charListToText,          textToCharListMaybe,          textToCharListUnsafe,
    {- ** @ByteString@ -} {- $byteStringConversions    -} charListToByteString,    byteStringToCharListMaybe,    byteStringToCharListUnsafe,

    {- * Refinement type -} {- $refinement -} {- ** @ASCII@ -} ASCII,

    {- * Polymorphic conversions -} {- ** Validate -} validateChar, validateString, {- ** Lift -} {- $lift -} lift,

    {- * Classes -} {- ** @CharSuperset@ -} CharSuperset, {- ** @StringSuperset@ -} StringSuperset, {- ** @Lift@ -} Lift, {- ** @CharIso@ -} CharIso, {- ** @StringIso@ -} StringIso,

    {- * Quasi-quoters -} {- ** @char@ -} char, {- ** @string@ -} string
  )
  where

import ASCII.Case          ( Case (..) )
import ASCII.Char          ( Char )
import ASCII.Group         ( Group (..) )
import ASCII.Isomorphism   ( CharIso, StringIso )
import ASCII.Lift          ( Lift )
import ASCII.QuasiQuoters  ( char, string )
import ASCII.Refinement    ( ASCII, validateChar, validateString )
import ASCII.Superset      ( CharSuperset, StringSuperset )

import Control.Monad       ( (>=>) )
import Data.Bool           ( Bool (..) )
import Data.Function       ( (.) )
import Data.Int            ( Int )
import Data.Maybe          ( Maybe, maybe )
import Data.Word           ( Word8 )

import qualified  ASCII.Case
import qualified  ASCII.Group
import qualified  ASCII.Isomorphism
import qualified  ASCII.Lift
import qualified  ASCII.Superset

import qualified  Data.ByteString  as  BS
import qualified  Data.Char        as  Unicode
import qualified  Data.String      as  Unicode
import qualified  Data.Text        as  Text

{- $setup

>>> import ASCII.Char (Char (..))
>>> import Data.List (map)
>>> import Data.Text (Text)
>>> import Data.Word (Word8)

-}

{- $char

See also: "ASCII.Char"

-}

{- $groups

ASCII characters are broadly categorized into two groups: /control codes/ and /printable characters/.

See also: "ASCII.Group"

-}


{- | Determine which group a particular character belongs to.

>>> map charGroup [CapitalLetterA,EndOfTransmission]
[Printable,Control]

-}

charGroup :: CharIso char => char -> Group
charGroup :: char -> Group
charGroup = Char -> Group
ASCII.Group.charGroup (Char -> Group) -> (char -> Char) -> char -> Group
forall b c a. (b -> c) -> (a -> b) -> a -> c
. char -> Char
forall char. CharIso char => char -> Char
ASCII.Isomorphism.toChar

{- | Test whether a character belongs to a particular ASCII group.

>>> inGroup Printable EndOfTransmission
False

>>> inGroup Control EndOfTransmission
True

>>> map (inGroup Printable) ( [-1, 5, 65, 97, 127, 130] :: [Int] )
[False,False,True,True,False,False]

-}

inGroup :: CharSuperset char => Group -> char -> Bool
inGroup :: Group -> char -> Bool
inGroup Group
g = Bool -> (Char -> Bool) -> Maybe Char -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Group -> Char -> Bool
ASCII.Group.inGroup Group
g) (Maybe Char -> Bool) -> (char -> Maybe Char) -> char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. char -> Maybe Char
forall char. CharSuperset char => char -> Maybe Char
ASCII.Superset.toCharMaybe

{- $case

/Case/ is a property of letters. /A-Z/ are /upper case/ letters, and /a-z/ are /lower case/ letters. No other ASCII characters have case.

See also: "ASCII.Case"

-}

{- | Determines whether a character is an ASCII letter, and if so, whether it is upper or lower case.

>>> map letterCase [SmallLetterA, CapitalLetterA, ExclamationMark]
[Just LowerCase,Just UpperCase,Nothing]

>>> map letterCase ( [string|Hey!|] :: [ASCII Word8] )
[Just UpperCase,Just LowerCase,Just LowerCase,Nothing]

-}

letterCase :: CharSuperset char => char -> Maybe Case
letterCase :: char -> Maybe Case
letterCase = char -> Maybe Char
forall char. CharSuperset char => char -> Maybe Char
ASCII.Superset.toCharMaybe (char -> Maybe Char) -> (Char -> Maybe Case) -> char -> Maybe Case
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Char -> Maybe Case
ASCII.Case.letterCase

{- | Determines whether a character is an ASCII letter of a particular case.

>>> map (isCase UpperCase) [SmallLetterA, CapitalLetterA, ExclamationMark]
[False,True,False]

>>> map (isCase UpperCase) ( [string|Hey!|] :: [ASCII Word8] )
[True,False,False,False]

>>> map (isCase UpperCase) ( [-1, 65, 97, 150] :: [Int] )
[False,True,False,False]

-}

isCase :: CharSuperset char => Case -> char -> Bool
isCase :: Case -> char -> Bool
isCase Case
c = Bool -> (Char -> Bool) -> Maybe Char -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Case -> Char -> Bool
ASCII.Case.isCase Case
c) (Maybe Char -> Bool) -> (char -> Maybe Char) -> char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. char -> Maybe Char
forall char. CharSuperset char => char -> Maybe Char
ASCII.Superset.toCharMaybe

{- | Maps a letter character to its upper/lower case equivalent.

>>> toCaseChar UpperCase SmallLetterA
CapitalLetterA

>>> ([char|a|] :: ASCII Word8, toCaseChar UpperCase [char|a|] :: ASCII Word8)
(asciiUnsafe 97,asciiUnsafe 65)

-}

toCaseChar :: CharIso char => Case -> char -> char
toCaseChar :: Case -> char -> char
toCaseChar Case
c = (Char -> Char) -> char -> char
forall char. CharIso char => (Char -> Char) -> char -> char
ASCII.Isomorphism.asChar (Case -> Char -> Char
ASCII.Case.toCase Case
c)

{- | Maps each of the characters in a string to its upper/lower case equivalent.

>>> toCaseString UpperCase [CapitalLetterH,SmallLetterE,SmallLetterY,ExclamationMark]
[CapitalLetterH,CapitalLetterE,CapitalLetterY,ExclamationMark]

>>> toCaseString UpperCase [string|Hey!|] :: ASCII Text
asciiUnsafe "HEY!"

-}

toCaseString :: StringIso string => Case -> string -> string
toCaseString :: Case -> string -> string
toCaseString Case
c = (Char -> Char) -> string -> string
forall string.
StringIso string =>
(Char -> Char) -> string -> string
ASCII.Isomorphism.mapChars (Case -> Char -> Char
ASCII.Case.toCase Case
c)

{- $monomorphicConversions

These are a few simple monomorphic functions to convert between ASCII and types representing some other character set.

This is not intended to be an exhaustive list of all possible conversions. For more options, see "ASCII.Superset".

-}

{- $intConversions

These functions convert between the ASCII 'Char' type and 'Int'.

-}

{- |

>>> map charToInt [Null, CapitalLetterA, SmallLetterA, Delete]
[0,65,97,127]

-}

charToInt :: Char -> Int
charToInt :: Char -> Int
charToInt = Char -> Int
forall char. CharSuperset char => Char -> char
ASCII.Superset.fromChar

intToCharMaybe :: Int -> Maybe Char
intToCharMaybe :: Int -> Maybe Char
intToCharMaybe = Int -> Maybe Char
forall char. CharSuperset char => char -> Maybe Char
ASCII.Superset.toCharMaybe

intToCharUnsafe :: Int -> Char
intToCharUnsafe :: Int -> Char
intToCharUnsafe = Int -> Char
forall char. CharSuperset char => char -> Char
ASCII.Superset.toCharUnsafe

{- $word8Conversions

These functions convert between the ASCII 'Char' type and 'Word8'.

-}

{- |

>>> map charToWord8 [Null, CapitalLetterA, SmallLetterA, Delete]
[0,65,97,127]

-}

charToWord8 :: Char -> Word8
charToWord8 :: Char -> Word8
charToWord8 = Char -> Word8
forall char. CharSuperset char => Char -> char
ASCII.Superset.fromChar

word8ToCharMaybe :: Word8 -> Maybe Char
word8ToCharMaybe :: Word8 -> Maybe Char
word8ToCharMaybe = Word8 -> Maybe Char
forall char. CharSuperset char => char -> Maybe Char
ASCII.Superset.toCharMaybe

word8ToCharUnsafe :: Word8 -> Char
word8ToCharUnsafe :: Word8 -> Char
word8ToCharUnsafe = Word8 -> Char
forall char. CharSuperset char => char -> Char
ASCII.Superset.toCharUnsafe

{- $unicodeCharConversions

These functions convert between the ASCII 'Char' type and the Unicode 'Unicode.Char' type.

-}

charToUnicode :: Char -> Unicode.Char
charToUnicode :: Char -> Char
charToUnicode = Char -> Char
forall char. CharSuperset char => Char -> char
ASCII.Superset.fromChar

unicodeToCharMaybe :: Unicode.Char -> Maybe Char
unicodeToCharMaybe :: Char -> Maybe Char
unicodeToCharMaybe = Char -> Maybe Char
forall char. CharSuperset char => char -> Maybe Char
ASCII.Superset.toCharMaybe

unicodeToCharUnsafe :: Unicode.Char -> Char
unicodeToCharUnsafe :: Char -> Char
unicodeToCharUnsafe = Char -> Char
forall char. CharSuperset char => char -> Char
ASCII.Superset.toCharUnsafe

{- $unicodeStringConversions

These functions convert between @['Char']@ (a list of ASCII characters) and 'Unicode.String' (a list of Unicode characters).

-}

charListToUnicodeString :: [Char] -> Unicode.String
charListToUnicodeString :: [Char] -> String
charListToUnicodeString = [Char] -> String
forall string. StringSuperset string => [Char] -> string
ASCII.Superset.fromCharList

unicodeStringToCharListMaybe :: Unicode.String -> Maybe [Char]
unicodeStringToCharListMaybe :: String -> Maybe [Char]
unicodeStringToCharListMaybe = String -> Maybe [Char]
forall string. StringSuperset string => string -> Maybe [Char]
ASCII.Superset.toCharListMaybe

unicodeStringToCharListUnsafe :: Unicode.String -> [Char]
unicodeStringToCharListUnsafe :: String -> [Char]
unicodeStringToCharListUnsafe = String -> [Char]
forall string. StringSuperset string => string -> [Char]
ASCII.Superset.toCharListUnsafe

{- $textConversions

These functions convert between @['Char']@ and 'Text.Text'.

-}

{- |

>>> charListToText [CapitalLetterH,SmallLetterI,ExclamationMark]
"Hi!"

-}

charListToText :: [Char] -> Text.Text
charListToText :: [Char] -> Text
charListToText = [Char] -> Text
forall string. StringSuperset string => [Char] -> string
ASCII.Superset.fromCharList

textToCharListMaybe :: Text.Text -> Maybe [Char]
textToCharListMaybe :: Text -> Maybe [Char]
textToCharListMaybe = Text -> Maybe [Char]
forall string. StringSuperset string => string -> Maybe [Char]
ASCII.Superset.toCharListMaybe

textToCharListUnsafe :: Text.Text -> [Char]
textToCharListUnsafe :: Text -> [Char]
textToCharListUnsafe = Text -> [Char]
forall string. StringSuperset string => string -> [Char]
ASCII.Superset.toCharListUnsafe

{- $byteStringConversions

These functions convert between @['Char']@ and 'BS.ByteString'.

-}

charListToByteString :: [Char] -> BS.ByteString
charListToByteString :: [Char] -> ByteString
charListToByteString = [Char] -> ByteString
forall string. StringSuperset string => [Char] -> string
ASCII.Superset.fromCharList

byteStringToCharListMaybe :: BS.ByteString -> Maybe [Char]
byteStringToCharListMaybe :: ByteString -> Maybe [Char]
byteStringToCharListMaybe = ByteString -> Maybe [Char]
forall string. StringSuperset string => string -> Maybe [Char]
ASCII.Superset.toCharListMaybe

byteStringToCharListUnsafe :: BS.ByteString -> [Char]
byteStringToCharListUnsafe :: ByteString -> [Char]
byteStringToCharListUnsafe = ByteString -> [Char]
forall string. StringSuperset string => string -> [Char]
ASCII.Superset.toCharListUnsafe

{- $refinement

See also: "ASCII.Refinement"

-}

{- $lift

See also: "ASCII.Lift"

-}

{- | Converts from ASCII to any larger type.

For example, @(lift \@ASCII.Char \@Word8)@ is the same function as 'charToWord8'.

>>> lift CapitalLetterA :: Word8
65

And @(lift \@[ASCII.Char] \@Text)@ is equivalent to 'charListToText'.

>>> lift [CapitalLetterH,SmallLetterI,ExclamationMark] :: Text
"Hi!"

Due to the highly polymorphic nature of the 'lift' function, often it must used with an explicit type signature or type application to avoid any type ambiguity.

-}

lift :: Lift ascii superset => ascii -> superset
lift :: ascii -> superset
lift = ascii -> superset
forall ascii superset. Lift ascii superset => ascii -> superset
ASCII.Lift.lift