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
88 , codePointAt
99 , codePointFromInt
1010 , codePointToInt
11+ , codePointFromChar
1112 , count
1213 , drop
1314 , dropWhile
@@ -28,6 +29,7 @@ module Data.String.CodePoints
2829import Prelude
2930
3031import Data.Array as Array
32+ import Data.Char (toCharCode )
3133import Data.Char as Char
3234import Data.Int (hexadecimal , toStringAs )
3335import Data.Maybe (Maybe (Just, Nothing))
@@ -64,6 +66,16 @@ codePointFromInt n = Nothing
6466codePointToInt :: CodePoint -> Int
6567codePointToInt (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+
6779unsurrogate :: Int -> Int -> CodePoint
6880unsurrogate lead trail = CodePoint ((lead - 0xD800 ) * 0x400 + (trail - 0xDC00 ) + 0x10000 )
6981
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import Prelude
55import Control.Monad.Eff (Eff )
66import Control.Monad.Eff.Console (CONSOLE , log )
77
8+ import Data.Char (fromCharCode )
89import Data.Maybe (Maybe (..), isNothing , maybe )
910import 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
You can’t perform that action at this time.
0 commit comments