Skip to content

Commit 7a2a33c

Browse files
csicarhdgarrood
authored andcommitted
added function codePointFromChar (#92)
1 parent 66b7bbc commit 7a2a33c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Data/String/CodePoints.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Data.String.CodePoints
88
, codePointAt
99
, codePointFromInt
1010
, codePointToInt
11+
, codePointFromChar
1112
, count
1213
, drop
1314
, dropWhile
@@ -28,6 +29,7 @@ module Data.String.CodePoints
2829
import Prelude
2930

3031
import Data.Array as Array
32+
import Data.Char (toCharCode)
3133
import Data.Char as Char
3234
import Data.Int (hexadecimal, toStringAs)
3335
import Data.Maybe (Maybe(Just, Nothing))
@@ -64,6 +66,16 @@ codePointFromInt n = Nothing
6466
codePointToInt :: CodePoint -> Int
6567
codePointToInt (CodePoint n) = n
6668

69+
-- | Creates a CodePoint from a given Char.
70+
-- |
71+
-- | ```purescript
72+
-- | >>> codePointFromChar 'B'
73+
-- | CodePoint 0x42 -- represents 'B'
74+
-- | ```
75+
-- |
76+
codePointFromChar :: Char -> CodePoint
77+
codePointFromChar = toCharCode >>> CodePoint
78+
6779
unsurrogate :: Int -> Int -> CodePoint
6880
unsurrogate lead trail = CodePoint ((lead - 0xD800) * 0x400 + (trail - 0xDC00) + 0x10000)
6981

test/Test/Data/String/CodePoints.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Prelude
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
77

8+
import Data.Char (fromCharCode)
89
import Data.Maybe (Maybe(..), isNothing, maybe)
910
import Data.String.CodePoints
1011

@@ -35,6 +36,11 @@ testStringCodePoints = do
3536
assert $ codePointAt 6 str == (codePointFromInt 0x7A)
3637
assert $ codePointAt 7 str == Nothing
3738

39+
log "codePointFromChar"
40+
assert $ Just (codePointFromChar 'A') == (codePointFromInt 65)
41+
assert $ Just (codePointFromChar $ fromCharCode 0) == codePointFromInt 0
42+
assert $ Just (codePointFromChar $ fromCharCode 0xFFFF) == codePointFromInt 0xFFFF
43+
3844
log "count"
3945
assert $ count (\_ -> true) "" == 0
4046
assert $ count (\_ -> false) str == 0

0 commit comments

Comments
 (0)