Skip to content

Commit 13d3474

Browse files
committed
core: Add intToDigiT
1 parent 5b5ee64 commit 13d3474

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

unicode-data/Changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## 0.6.0 (July 2024)
44

5-
- Update to [Unicode 15.1.0](https://www.unicode.org/versions/Unicode15.1.0/).
5+
- Updated to [Unicode 15.1.0](https://www.unicode.org/versions/Unicode15.1.0/).
6+
- Added `intToDigiT` to `Unicode.Char.Numeric`.
67

78
## 0.5.0 (July 2024)
89

unicode-data/lib/Unicode/Char/Numeric.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ module Unicode.Char.Numeric
1717
, numericValue
1818
, integerValue
1919

20+
-- * Single digit characters
21+
, intToDigiT
22+
2023
-- * Re-export from @base@
2124
, isDigit
2225
, isOctDigit
@@ -29,6 +32,7 @@ import Data.Char (digitToInt, intToDigit, isDigit, isHexDigit, isOctDigit)
2932
import Data.Int (Int64)
3033
import Data.Maybe (isJust)
3134
import Data.Ratio (denominator, numerator)
35+
import GHC.Exts (Char (..), Int (..), chr#, isTrue#, (+#), (<=#), (>=#))
3236

3337
import qualified Unicode.Char.Numeric.Compat as Compat
3438
import qualified Unicode.Internal.Char.DerivedNumericValues as V
@@ -118,3 +122,16 @@ integerValue c = do
118122
if denominator r == 1
119123
then Just (fromInteger (numerator r))
120124
else Nothing
125+
126+
-- | Same a 'intToDigit', but with upper case.
127+
--
128+
-- >>> intToDigiT <$> [0..15]
129+
-- "0123456789ABCDEF"
130+
--
131+
-- @since 0.6.0
132+
intToDigiT :: Int -> Char
133+
intToDigiT (I# i)
134+
| isTrue# (i >=# 0#) && isTrue# (i <=# 9#) = C# (chr# (0x30# +# i))
135+
| isTrue# (i >=# 10#) && isTrue# (i <=# 15#) = C# (chr# (0x37# +# i))
136+
| otherwise = errorWithoutStackTrace
137+
("Unicode.Char.Numeric.intToDigiT: not a digit " ++ show (I# i))

0 commit comments

Comments
 (0)