|
| 1 | +module ASCII.Refinement |
| 2 | + ( |
| 3 | + {- * ASCII type constructor -} ASCII, lift, asciiUnsafe, |
| 4 | + {- * Character functions -} validateChar, fromChar, toChar, substituteChar, asChar, |
| 5 | + {- * String functions -} validateString, fromCharList, toCharList, substituteString, mapChars |
| 6 | + ) |
| 7 | + where |
| 8 | + |
| 9 | +import qualified ASCII.Char as ASCII |
| 10 | +import qualified ASCII.Isomorphism as I |
| 11 | +import qualified ASCII.Superset as S |
| 12 | + |
| 13 | +import ASCII.Superset (CharSuperset, StringSuperset) |
| 14 | +import Data.Bool (Bool (..)) |
| 15 | +import Data.Data (Data) |
| 16 | +import Data.Eq (Eq) |
| 17 | +import Data.Function (id, ($), (.)) |
| 18 | +import Data.Hashable (Hashable) |
| 19 | +import Data.List (map) |
| 20 | +import Data.Maybe (Maybe (..)) |
| 21 | +import Data.Monoid (Monoid) |
| 22 | +import Data.Ord (Ord, (>)) |
| 23 | +import Data.Semigroup (Semigroup) |
| 24 | +import GHC.Generics (Generic) |
| 25 | +import Prelude (succ) |
| 26 | +import Text.Show (Show, showList, showParen, showString, showsPrec) |
| 27 | + |
| 28 | +{- | This type constructor indicates that a value from some ASCII superset is valid ASCII. The type parameter is the ASCII superset, which should be a type with an instance of either 'CharSuperset' or 'StringSuperset'. |
| 29 | +
|
| 30 | +For example, whereas a 'Data.Text.Text' value may contain a combination of ASCII and non-ASCII characters, a value of type @'ASCII' 'Data.Text.Text'@ may contain only ASCII characters. |
| 31 | +
|
| 32 | +-} |
| 33 | + |
| 34 | +newtype ASCII superset = ASCII_Unsafe { lift :: superset } |
| 35 | + |
| 36 | +deriving stock instance Eq superset => Eq (ASCII superset) |
| 37 | + |
| 38 | +deriving stock instance Ord superset => Ord (ASCII superset) |
| 39 | + |
| 40 | +deriving newtype instance Hashable superset => Hashable (ASCII superset) |
| 41 | + |
| 42 | +deriving newtype instance Semigroup superset => Semigroup (ASCII superset) |
| 43 | + |
| 44 | +deriving newtype instance Monoid superset => Monoid (ASCII superset) |
| 45 | + |
| 46 | +deriving stock instance Data superset => Data (ASCII superset) |
| 47 | + |
| 48 | +deriving stock instance Generic (ASCII superset) |
| 49 | + |
| 50 | +instance Show superset => Show (ASCII superset) |
| 51 | + where |
| 52 | + showsPrec d x = showParen (d > app_prec) $ |
| 53 | + showString "asciiUnsafe " . showsPrec (succ app_prec) (lift x) |
| 54 | + where app_prec = 10 |
| 55 | + |
| 56 | + showList x = showString "asciiUnsafe " . showList (map lift x) |
| 57 | + |
| 58 | +instance CharSuperset char => CharSuperset (ASCII char) |
| 59 | + where |
| 60 | + isAsciiChar _ = True |
| 61 | + fromChar = asciiUnsafe . S.fromChar |
| 62 | + toCharUnsafe = S.toCharUnsafe . lift |
| 63 | + |
| 64 | +instance CharSuperset char => I.CharIso (ASCII char) |
| 65 | + where |
| 66 | + toChar = S.toCharUnsafe |
| 67 | + |
| 68 | +instance StringSuperset string => StringSuperset (ASCII string) |
| 69 | + where |
| 70 | + isAsciiString _ = True |
| 71 | + fromCharList = asciiUnsafe . S.fromCharList |
| 72 | + toCharListUnsafe = S.toCharListUnsafe . lift |
| 73 | + toCharListSub = S.toCharListUnsafe . lift |
| 74 | + substituteString = id |
| 75 | + |
| 76 | +instance StringSuperset string => I.StringIso (ASCII string) |
| 77 | + where |
| 78 | + toCharList = S.toCharListUnsafe |
| 79 | + mapChars = S.mapCharsUnsafe |
| 80 | + |
| 81 | +asciiUnsafe :: superset -> ASCII superset |
| 82 | +asciiUnsafe = ASCII_Unsafe |
| 83 | + |
| 84 | +{- | |
| 85 | +
|
| 86 | +>>> map validateChar [-1, 65, 97, 128] :: [Maybe (ASCII Int)] |
| 87 | +[Nothing,Just (asciiUnsafe 65),Just (asciiUnsafe 97),Nothing] |
| 88 | +
|
| 89 | +-} |
| 90 | + |
| 91 | +validateChar :: CharSuperset superset => superset -> Maybe (ASCII superset) |
| 92 | +validateChar x = if S.isAsciiChar x then Just (asciiUnsafe x) else Nothing |
| 93 | + |
| 94 | +substituteChar :: CharSuperset superset => superset -> ASCII superset |
| 95 | +substituteChar x = if S.isAsciiChar x then asciiUnsafe x else fromChar ASCII.Substitute |
| 96 | + |
| 97 | +fromChar :: CharSuperset superset => ASCII.Char -> ASCII superset |
| 98 | +fromChar = asciiUnsafe . S.fromChar |
| 99 | + |
| 100 | +toChar :: CharSuperset superset => ASCII superset -> ASCII.Char |
| 101 | +toChar = S.toCharUnsafe . lift |
| 102 | + |
| 103 | +{- | |
| 104 | +
|
| 105 | +>>> fromCharList [CapitalLetterH,SmallLetterI,ExclamationMark] :: ASCII Text |
| 106 | +asciiUnsafe "Hi!" |
| 107 | +
|
| 108 | +-} |
| 109 | + |
| 110 | +fromCharList :: StringSuperset superset => [ASCII.Char] -> ASCII superset |
| 111 | +fromCharList = asciiUnsafe . S.fromCharList |
| 112 | + |
| 113 | +{- | |
| 114 | +
|
| 115 | +>>> toCharList (substituteString "Piñata" :: ASCII Text) |
| 116 | +[CapitalLetterP,SmallLetterI,Substitute,SmallLetterA,SmallLetterT,SmallLetterA] |
| 117 | +
|
| 118 | +-} |
| 119 | + |
| 120 | +toCharList :: StringSuperset superset => ASCII superset -> [ASCII.Char] |
| 121 | +toCharList = S.toCharListUnsafe . lift |
| 122 | + |
| 123 | +{- | Forces a string from a larger character set into ASCII by using the 'ASCII.Substitute' character in place of any non-ASCII characters. |
| 124 | +
|
| 125 | +>>> substituteString "Cristóbal" :: ASCII Text |
| 126 | +asciiUnsafe "Crist\SUBbal" |
| 127 | +
|
| 128 | +-} |
| 129 | + |
| 130 | +substituteString :: StringSuperset superset => superset -> ASCII superset |
| 131 | +substituteString = asciiUnsafe . S.substituteString |
| 132 | + |
| 133 | +{- | |
| 134 | +
|
| 135 | +>>> map validateString ["Hello", "Cristóbal"] :: [Maybe (ASCII Text)] |
| 136 | +[Just (asciiUnsafe "Hello"),Nothing] |
| 137 | +
|
| 138 | +>>> map validateString ["Hello", "Cristóbal"] :: [Maybe (ASCII String)] |
| 139 | +[Just (asciiUnsafe "Hello"),Nothing] |
| 140 | +
|
| 141 | +-} |
| 142 | + |
| 143 | +validateString :: StringSuperset superset => superset -> Maybe (ASCII superset) |
| 144 | +validateString x = if S.isAsciiString x then Just (asciiUnsafe x) else Nothing |
| 145 | + |
| 146 | +asChar :: CharSuperset superset => (ASCII.Char -> ASCII.Char) -> ASCII superset -> ASCII superset |
| 147 | +asChar f = asciiUnsafe . S.asCharUnsafe f . lift |
| 148 | + |
| 149 | +mapChars :: StringSuperset superset => (ASCII.Char -> ASCII.Char) -> ASCII superset -> ASCII superset |
| 150 | +mapChars f = asciiUnsafe . S.mapCharsUnsafe f . lift |
0 commit comments