packages feed

papa-0.7.0: src/Papa/Base/Data/Char.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

-- | Characters, with a total 'chr'.
module Papa.Base.Data.Char (
  module P,
  chr,
) where

import Data.Bool (bool, (&&))
import Data.Char as P (
  Char,
  GeneralCategory (ClosePunctuation, ConnectorPunctuation, Control, CurrencySymbol, DashPunctuation, DecimalNumber, EnclosingMark, FinalQuote, Format, InitialQuote, LetterNumber, LineSeparator, LowercaseLetter, MathSymbol, ModifierLetter, ModifierSymbol, NonSpacingMark, NotAssigned, OpenPunctuation, OtherLetter, OtherNumber, OtherPunctuation, OtherSymbol, ParagraphSeparator, PrivateUse, Space, SpacingCombiningMark, Surrogate, TitlecaseLetter, UppercaseLetter),
  isAlpha,
  isAlphaNum,
  isAscii,
  isAsciiLower,
  isAsciiUpper,
  isControl,
  isDigit,
  isHexDigit,
  isLatin1,
  isLetter,
  isLower,
  isMark,
  isNumber,
  isOctDigit,
  isPrint,
  isPunctuation,
  isSeparator,
  isSpace,
  isSymbol,
  isUpper,
  ord,
  toLower,
  toTitle,
  toUpper,
 )
import qualified Data.Char as C
import Data.Int (Int)
import Data.Maybe (Maybe (Just, Nothing))
import Data.Ord (Ord ((<=)))

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen

-- | Total 'Data.Char.chr': 'Data.Maybe.Nothing' outside the range of 'Char'
-- (@0@ to @0x10FFFF@), where "Data.Char"'s @chr@ throws.
--
-- ==== __Examples__
--
-- >>> chr 65
-- Just 'A'
--
-- >>> chr (-1)
-- Nothing
--
-- >>> chr 0x110000
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do c <- forAll Gen.unicodeAll; chr (ord c) === Just c
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
chr :: Int -> Maybe Char
chr n =
  bool Nothing (Just (C.chr n)) (0 <= n && n <= 0x10FFFF)
{-# INLINE chr #-}