Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Data/String/CodePoints.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Data.String.CodePoints
, codePointAt
, codePointFromInt
, codePointToInt
, codePointFromChar
, count
, drop
, dropWhile
Expand All @@ -28,6 +29,7 @@ module Data.String.CodePoints
import Prelude

import Data.Array as Array
import Data.Char (toCharCode)
import Data.Char as Char
import Data.Maybe (Maybe(Just, Nothing))
import Data.String as String
Expand Down Expand Up @@ -60,6 +62,16 @@ codePointFromInt n = Nothing
codePointToInt :: CodePoint -> Int
codePointToInt (CodePoint n) = n

-- | Creates a CodePoint from a given Char.
-- |
-- | ```purescript
-- | >>> codePointFromChar 'B'
-- | CodePoint 0x42 -- represents 'B'
-- | ```
-- |
codePointFromChar :: Char -> CodePoint
codePointFromChar = toCharCode >>> CodePoint

unsurrogate :: Int -> Int -> CodePoint
unsurrogate lead trail = CodePoint ((lead - 0xD800) * 0x400 + (trail - 0xDC00) + 0x10000)

Expand Down
6 changes: 6 additions & 0 deletions test/Test/Data/String/CodePoints.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

import Data.Char (fromCharCode)
import Data.Maybe (Maybe(..), isNothing, maybe)
import Data.String.CodePoints

Expand All @@ -26,6 +27,11 @@ testStringCodePoints = do
assert $ codePointAt 6 str == (codePointFromInt 0x7A)
assert $ codePointAt 7 str == Nothing

log "codePointFromChar"
assert $ Just (codePointFromChar 'A') == (codePointFromInt 65)
assert $ Just (codePointFromChar $ fromCharCode 0) == codePointFromInt 0
assert $ Just (codePointFromChar $ fromCharCode 0xFFFF) == codePointFromInt 0xFFFF

log "count"
assert $ count (\_ -> true) "" == 0
assert $ count (\_ -> false) str == 0
Expand Down