unicode-data-0.6.0: Access Unicode Character Database (UCD)

Copyright | (c) 2020 Composewell Technologies and Contributors
License | Apache-2.0
Maintainer | streamly@composewell.com
Stability | experimental
Safe Haskell | None
Language | Haskell2010

Unicode.Char.Numeric

Contents

- Predicates
- Numeric values
- Single digit characters
- Re-export from `base`

Description

Numeric character property related functions.

Since: 0.3.0

Synopsis

- isNumeric :: Char -> Bool
- numericValue :: Char -> Maybe Rational
- integerValue :: Integral a => Char -> Maybe a
- intToDigiT :: Int -> Char
- isDigit :: Char -> Bool
- isOctDigit :: Char -> Bool
- isHexDigit :: Char -> Bool
- digitToInt :: Char -> Int
- intToDigit :: Int -> Char

# Predicates

isNumeric :: Char -> Bool #

Selects Unicode character with a numeric value.

Note: a character may have a numeric value but return `False` with the predicate `isNumber`, because `isNumber` only tests `GeneralCategory`: some CJK characters are `OtherLetter` and do have a numeric value.

    isNumeric c == isJust (numericValue c)

Since: 0.3.1

# Numeric values

numericValue :: Char -> Maybe Rational #

Numeric value of a character, if relevant.

Note: a character may have a numeric value but return `False` with the predicate `isNumber`, because `isNumber` only tests `GeneralCategory`: some CJK characters are `OtherLetter` and do have a numeric value.

Since: 0.3.1

integerValue :: Integral a => Char -> Maybe a #

Integer value of a character, if relevant.

This is a special case of `numericValue`.

Warning: There is a risk of integer overflow depending of the chosen concrete return type. As of Unicode 15.1 the results range from 0 to 1e16.

    >>> minimum [v | v@Just{} <- integerValue <$> [minBound..]] :: Maybe Integer
    Just 0
    >>> maximum (integerValue <$> [minBound..]) :: Maybe Integer
    Just 10000000000000000
    >>> integerValue '\x4EAC' :: Maybe Int64 -- OK
    Just 10000000000000000
    >>> integerValue '\x4EAC' :: Maybe Int32 -- Will overflow!
    Just 1874919424

Therefore it is advised to use: ``integerValue` @`Int64``.

Note: A character may have a numeric value but return `False` with the predicate `isNumber`, because `isNumber` only tests `GeneralCategory`: some CJK characters are `OtherLetter` and do have a numeric value.

Since: 0.3.1

# Single digit characters

intToDigiT :: Int -> Char #

Same a `intToDigit`, but with upper case.

    >>> intToDigiT <$> [0..15]
    "0123456789ABCDEF"

Since: 0.6.0

# Re-export from `base`

isDigit :: Char -> Bool #

Selects ASCII digits, i.e. `'0'`..`'9'`.

isOctDigit :: Char -> Bool #

Selects ASCII octal digits, i.e. `'0'`..`'7'`.

isHexDigit :: Char -> Bool #

Selects ASCII hexadecimal digits, i.e. `'0'`..`'9'`, `'a'`..`'f'`, `'A'`..`'F'`.

digitToInt :: Char -> Int #

Convert a single digit `Char` to the corresponding `Int`. This function fails unless its argument satisfies `isHexDigit`, but recognises both upper- and lower-case hexadecimal digits (that is, `'0'`..`'9'`, `'a'`..`'f'`, `'A'`..`'F'`).

#### Examples

Expand

Characters `'0'` through `'9'` are converted properly to `0..9`:

    >>> map digitToInt ['0'..'9']
    [0,1,2,3,4,5,6,7,8,9]

Both upper- and lower-case `'A'` through `'F'` are converted as well, to `10..15`.

    >>> map digitToInt ['a'..'f']
    [10,11,12,13,14,15]
    >>> map digitToInt ['A'..'F']
    [10,11,12,13,14,15]

Anything else throws an exception:

    >>> digitToInt 'G'
    *** Exception: Char.digitToInt: not a digit 'G'
    >>> digitToInt '♥'
    *** Exception: Char.digitToInt: not a digit '\9829'

intToDigit :: Int -> Char #

Convert an `Int` in the range `0`..`15` to the corresponding single digit `Char`. This function fails on other inputs, and generates lower-case hexadecimal digits.