File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ module Data.String.CodePoints
8
8
, codePointAt
9
9
, codePointFromInt
10
10
, codePointToInt
11
+ , codePointFromChar
11
12
, count
12
13
, drop
13
14
, dropWhile
@@ -28,6 +29,7 @@ module Data.String.CodePoints
28
29
import Prelude
29
30
30
31
import Data.Array as Array
32
+ import Data.Char (toCharCode )
31
33
import Data.Char as Char
32
34
import Data.Int (hexadecimal , toStringAs )
33
35
import Data.Maybe (Maybe (Just, Nothing))
@@ -64,6 +66,16 @@ codePointFromInt n = Nothing
64
66
codePointToInt :: CodePoint -> Int
65
67
codePointToInt (CodePoint n) = n
66
68
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
+
67
79
unsurrogate :: Int -> Int -> CodePoint
68
80
unsurrogate lead trail = CodePoint ((lead - 0xD800 ) * 0x400 + (trail - 0xDC00 ) + 0x10000 )
69
81
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import Prelude
5
5
import Control.Monad.Eff (Eff )
6
6
import Control.Monad.Eff.Console (CONSOLE , log )
7
7
8
+ import Data.Char (fromCharCode )
8
9
import Data.Maybe (Maybe (..), isNothing , maybe )
9
10
import Data.String.CodePoints
10
11
@@ -35,6 +36,11 @@ testStringCodePoints = do
35
36
assert $ codePointAt 6 str == (codePointFromInt 0x7A )
36
37
assert $ codePointAt 7 str == Nothing
37
38
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
+
38
44
log " count"
39
45
assert $ count (\_ -> true ) " " == 0
40
46
assert $ count (\_ -> false ) str == 0
You can’t perform that action at this time.
0 commit comments