From 1efb1b1d2dc28dd39aec9e0249efc1232d22699d Mon Sep 17 00:00:00 2001 From: "Carsten Csiky (csicar)" Date: Thu, 21 Dec 2017 20:42:21 +0100 Subject: [PATCH 1/4] added function codePointFromChar --- src/Data/String/CodePoints.purs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Data/String/CodePoints.purs b/src/Data/String/CodePoints.purs index 7fe51f3..2b4d7e7 100644 --- a/src/Data/String/CodePoints.purs +++ b/src/Data/String/CodePoints.purs @@ -8,6 +8,7 @@ module Data.String.CodePoints , codePointAt , codePointFromInt , codePointToInt + , codePointFromChar , count , drop , dropWhile @@ -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 @@ -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 "B" +-- | ``` +-- | +codePointFromChar :: Char -> CodePoint +codePointFromChar = toCharCode >>> CodePoint + unsurrogate :: Int -> Int -> CodePoint unsurrogate lead trail = CodePoint ((lead - 0xD800) * 0x400 + (trail - 0xDC00) + 0x10000) From 0e0b8c64f97eb95adf0828730e763d6d8978aeab Mon Sep 17 00:00:00 2001 From: "Carsten Csiky (csicar)" Date: Sat, 23 Dec 2017 19:57:11 +0100 Subject: [PATCH 2/4] added tests for codePointFromChar --- test/Test/Data/String/CodePoints.purs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/Test/Data/String/CodePoints.purs b/test/Test/Data/String/CodePoints.purs index 7a6aef0..d74ad51 100644 --- a/test/Test/Data/String/CodePoints.purs +++ b/test/Test/Data/String/CodePoints.purs @@ -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 @@ -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 From 9232504d8872d05ab87f096ba8b2e0279af1b2a7 Mon Sep 17 00:00:00 2001 From: "Carsten Csiky (csicar)" Date: Sun, 24 Dec 2017 22:54:20 +0100 Subject: [PATCH 3/4] use correct show instance for CodePoint --- src/Data/String/CodePoints.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/String/CodePoints.purs b/src/Data/String/CodePoints.purs index 2b4d7e7..0d58cb7 100644 --- a/src/Data/String/CodePoints.purs +++ b/src/Data/String/CodePoints.purs @@ -66,7 +66,7 @@ codePointToInt (CodePoint n) = n -- | -- | ```purescript -- | codePointFromChar 'B' --- | == CodePoint "B" +-- | == CodePoint 0x42 -- represents 'B' -- | ``` -- | codePointFromChar :: Char -> CodePoint From 2c9ee488f38612fa3023787739876903f02a027a Mon Sep 17 00:00:00 2001 From: "Carsten Csiky (csicar)" Date: Thu, 28 Dec 2017 11:06:49 +0100 Subject: [PATCH 4/4] changed example to repl version --- src/Data/String/CodePoints.purs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Data/String/CodePoints.purs b/src/Data/String/CodePoints.purs index 0d58cb7..18567c7 100644 --- a/src/Data/String/CodePoints.purs +++ b/src/Data/String/CodePoints.purs @@ -65,8 +65,8 @@ codePointToInt (CodePoint n) = n -- | Creates a CodePoint from a given Char. -- | -- | ```purescript --- | codePointFromChar 'B' --- | == CodePoint 0x42 -- represents 'B' +-- | >>> codePointFromChar 'B' +-- | CodePoint 0x42 -- represents 'B' -- | ``` -- | codePointFromChar :: Char -> CodePoint