Skip to content

Commit ed8e045

Browse files
committed
Added digitToInt function.
1 parent e7310ef commit ed8e045

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/Data/Char/Unicode.purs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -514,19 +514,23 @@ toTitle = fromCharCode <<< uTowtitle <<< toCharCode
514514
-- | >>> digitToInt '♥'
515515
-- | *** Exception: Char.digitToInt: not a digit '\9829'
516516
-- | ```
517-
-- |
518-
-- | TODO: This function.
519-
-- |
520-
-- digitToInt :: Char -> Int
521-
-- digitToInt c
522-
-- | (fromIntegral dec::Word) <= 9 = dec
523-
-- | (fromIntegral hexl::Word) <= 5 = hexl + 10
524-
-- | (fromIntegral hexu::Word) <= 5 = hexu + 10
525-
-- | otherwise = errorWithoutStackTrace ("Char.digitToInt: not a digit " ++ show c) -- sigh
526-
-- where
527-
-- dec = ord c - ord '0'
528-
-- hexl = ord c - ord 'a'
529-
-- hexu = ord c - ord 'A'
517+
digitToInt :: Char -> Maybe Int
518+
digitToInt c = go c
519+
where
520+
go :: Char -> Maybe Int
521+
go c | dec <= 9 && dec >= 0 = Just dec
522+
| hexLower <= 5 && hexLower >= 0 = Just $ hexLower + 10
523+
| hexUpper <= 5 && hexUpper >= 0 = Just $ hexUpper + 10
524+
| otherwise = Nothing
525+
526+
dec :: Int
527+
dec = toCharCode c - toCharCode '0'
528+
529+
hexLower :: Int
530+
hexLower = toCharCode c - toCharCode 'a'
531+
532+
hexUpper :: Int
533+
hexUpper = toCharCode c - toCharCode 'A'
530534

531535
-- | Selects alphabetic Unicode characters (lower-case, upper-case and
532536
-- | title-case letters, plus letters of caseless scripts and

test/Test/Data/Char/Unicode.purs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dataCharUnicodeTests = describe "module Data.Char.Unicode" do
3737
toUpperTests
3838
toLowerTests
3939
toTitleTests
40+
digitToIntTests
4041
isLetterTests
4142
isMarkTests
4243
isNumberTests
@@ -321,6 +322,24 @@ toLowerTests = pure unit
321322
toTitleTests :: forall eff . Spec eff Unit
322323
toTitleTests = pure unit
323324

325+
digitToIntTests :: forall eff . Spec eff Unit
326+
digitToIntTests = describe "digitToInt" do
327+
it "'0'..'9' get mapped correctly" $
328+
map digitToInt ['0','1','2','3','4','5','6','7','8','9'] `shouldEqual`
329+
[Just 0, Just 1, Just 2, Just 3, Just 4, Just 5, Just 6, Just 7, Just 8, Just 9]
330+
it "'a'..'f' get mapped correctly" $
331+
map digitToInt ['a','b','c','d','e','f'] `shouldEqual`
332+
[Just 10, Just 11, Just 12, Just 13, Just 14, Just 15]
333+
it "'A'..'F' get mapped correctly" $
334+
map digitToInt ['A','B','C','D','E','F'] `shouldEqual`
335+
[Just 10, Just 11, Just 12, Just 13, Just 14, Just 15]
336+
it "'G' is not a digit" $
337+
digitToInt 'G' `shouldEqual` Nothing
338+
it "'♥' is not a digit" $
339+
digitToInt '♥' `shouldEqual` Nothing
340+
it "'国' is not a digit" $
341+
digitToInt '国' `shouldEqual` Nothing
342+
324343
isLetterTests:: forall eff . Spec (console :: CONSOLE, random :: RANDOM, err :: EXCEPTION | eff) Unit
325344
isLetterTests = describe "isLetter" do
326345
prop "isLetter == isAlpha" \char -> isLetter char == isAlpha char

0 commit comments

Comments
 (0)