-- Copyright 2023 Lennart Augustsson-- See LICENSE file for full license.module Data.Char( Char, String, chr, ord, isLower, isUpper, isAsciiLower, isAsciiUpper, isAlpha, isLetter, isAlphaNum, isDigit, isOctDigit, isHexDigit, isNumber, isSymbol, isPunctuation, isSpace, isControl, U.isMark, isSeparator, isAscii, isLatin1, isPrint, U.GeneralCategory(..), U.generalCategory, digitToInt, intToDigit, toLower, toUpper, toTitle, showLitChar, -- lexLitChar, readLitChar, -- XXX For now, don't import Text.Read, it's a bloated beast. ) whereimport qualified Prelude() -- do not import Preludeimport Primitivesimport Control.Errorimport Data.Boolimport Data.Boundedimport Data.Char_Typeimport {-# SOURCE #-} qualified Data.Char.Unicode as Uimport Data.Eqimport Data.Functionimport Data.Int.Intimport Data.List_Typeimport Data.Numimport Data.Ord--import {-# SOURCE #-} Text.Read.Internal (lexLitChar, readLitChar)-- XXX For now, don't import Text.Read, it's a bloated beast.import Text.Showinstance Eq Char where (==) = primCharEQ (/=) = primCharNEinstance Ord Char where compare = primCharCompare (<) = primCharLT (<=) = primCharLE (>) = primCharGT (>=) = primCharGEinstance Bounded Char where minBound = primChr 0 maxBound = primChr 0x10ffffchr :: Int -> Charchr i | primIntToWord i `primWordLE` 0x10ffff = primChr i | otherwise = error "Data.Char.chr: invalid codepoint"ord :: Char -> Intord = primOrdisLower :: Char -> BoolisLower c = if c <= '\177' then isAsciiLower c else U.isLower cisAsciiLower :: Char -> BoolisAsciiLower c = 'a' <= c && c <= 'z'isUpper :: Char -> BoolisUpper c = if isAscii c then isAsciiUpper c else U.isUpper cisAsciiUpper :: Char -> BoolisAsciiUpper c = 'A' <= c && c <= 'Z'isAlpha :: Char -> BoolisAlpha c = if isAscii c then isLower c || isUpper c else U.isAlpha cisLetter :: Char -> BoolisLetter = isAlphaisDigit :: Char -> BoolisDigit c = '0' <= c && c <= '9'isOctDigit :: Char -> BoolisOctDigit c = '0' <= c && c <= '7'isHexDigit :: Char -> BoolisHexDigit c = isDigit c || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')isAlphaNum :: Char -> BoolisAlphaNum c = if isAscii c then isAlpha c || isDigit c else U.isAlphaNum cisNumber :: Char -> BoolisNumber c = if isAscii c then isDigit c else U.isNumber cisSymbol :: Char -> BoolisSymbol c = if isAscii c then c `xelem` "$+<=>^`|~" else U.isSymbol cisPunctuation :: Char -> BoolisPunctuation c = if isAscii c then c `xelem` "!\"#%&'()*,-./:;?@[\\]_{}" else U.isPunctuation c-- Don't want to import Data.Listxelem :: Char -> [Char] -> Boolxelem _ [] = Falsexelem d (c:cs) = d == c || xelem d csisPrint :: Char -> BoolisPrint c = if isAscii c then ' ' <= c && c <= '~' else U.isPrint cisSpace :: Char -> BoolisSpace c = if isAscii c then c `xelem` " \t\n\v\f\r" else U.isSpace cisControl :: Char -> BoolisControl c = c <= '\31' || c == '\127'isSeparator :: Char -> BoolisSeparator c = if isAscii c then c == ' ' else U.isSeparator cisAscii :: Char -> BoolisAscii c = c <= '\127'isLatin1 :: Char -> BoolisLatin1 c = c <= '\255'digitToInt :: Char -> IntdigitToInt c | isDigit c = ord c - ord '0' | 'a' <= c && c <= 'f' = ord c - (ord 'a' - 10) | 'A' <= c && c <= 'F' = ord c - (ord 'A' - 10) | otherwise = error "digitToInt"intToDigit :: Int -> CharintToDigit i | w `primWordLT` 10 = primChr (ord '0' + i) | w `primWordLT` 16 = primChr (ord 'a' - 10 + i) | otherwise = error "intToDigit" where w = primIntToWord itoLower :: Char -> ChartoLower c | isAsciiUpper c = primChr (ord c - ord 'A' + ord 'a') | isAscii c = c | True = U.toLower ctoUpper :: Char -> ChartoUpper c | isAsciiLower c = primChr (ord c - ord 'a' + ord 'A') | isAscii c = c | True = U.toUpper ctoTitle :: Char -> ChartoTitle c | isAsciiLower c = primChr (ord c - ord 'a' + ord 'A') | isAscii c = c | True = U.toTitle cinstance Show Char where showsPrec _ '\'' = showString "'\\''" showsPrec _ c = showChar '\'' . showLitChar c . showChar '\'' showList s = showChar '"' . f s where f [] = showChar '"' f (c:cs) = if c == '"' then showString "\\\"" . f cs else showLitChar c . f csshowLitChar :: Char -> ShowSshowLitChar c s | isAscii c && isPrint c && c /= '\\' = c : sshowLitChar c rest = let needDigitProtect = case rest of [] -> False c : _ -> isDigit c needHProtect = case rest of 'H' : _ -> True _ -> False spec :: [(Char, String)] spec = [('\NUL', "NUL"), ('\SOH', "SOH"), ('\STX', "STX"), ('\ETX', "ETX"), ('\EOT', "EOT"), ('\ENQ', "ENQ"), ('\ACK', "ACK"), ('\a', "a"), ('\b', "b"), ('\t', "t"), ('\n', "n"), ('\v', "v"), ('\f', "f"), ('\r', "r"), ('\\', "\\"), ('\SO', "SO"), ('\SI', "SI"), ('\DLE', "DLE"), ('\DC1', "DC1"), ('\DC2', "DC2"), ('\DC3', "DC3"), ('\DC4', "DC4"), ('\NAK', "NAK"), ('\SYN', "SYN"), ('\ETB', "ETB"), ('\CAN', "CAN"), ('\EM', "EM"), ('\SUB', "SUB"), ('\ESC', "ESC"), ('\FS', "FS"), ('\GS', "GS"), ('\RS', "RS"), ('\US', "US"), ('\DEL', "DEL") ] look [] = ("\\"::String) ++ show (ord c) ++ if needDigitProtect then "\\&"::String else "" look ((d,s):xs) = if d == c then '\\':s ++ (if c == '\SO' && needHProtect then "\\&"::String else "") else look xs in look spec ++ rest