From 78bb0ebef15cb87227b0413d3205bd34c9ee5d3a Mon Sep 17 00:00:00 2001 From: "Carsten Csiky (csicar)" Date: Thu, 21 Dec 2017 20:40:46 +0100 Subject: [PATCH 1/6] created an instance of Show for CodePoint --- src/Data/String/CodePoints.purs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Data/String/CodePoints.purs b/src/Data/String/CodePoints.purs index 7fe51f3..01157ef 100644 --- a/src/Data/String/CodePoints.purs +++ b/src/Data/String/CodePoints.purs @@ -50,6 +50,9 @@ newtype CodePoint = CodePoint Int derive instance eqCodePoint :: Eq CodePoint derive instance ordCodePoint :: Ord CodePoint +instance showCodePoint :: Show CodePoint where + show cp = "(CodePoint " <> show (singleton cp) <> ")" + -- I would prefer that this smart constructor not need to exist and instead -- CodePoint just implements Enum, but the Enum module already depends on this -- one. To avoid the circular dependency, we just expose these two functions. From 3cc2dd8a1b695b2970842aef75f5c1e9247fa61e Mon Sep 17 00:00:00 2001 From: "Carsten Csiky (csicar)" Date: Fri, 22 Dec 2017 17:47:06 +0100 Subject: [PATCH 2/6] use int representation for codepoint show --- 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 01157ef..58ae836 100644 --- a/src/Data/String/CodePoints.purs +++ b/src/Data/String/CodePoints.purs @@ -51,7 +51,7 @@ derive instance eqCodePoint :: Eq CodePoint derive instance ordCodePoint :: Ord CodePoint instance showCodePoint :: Show CodePoint where - show cp = "(CodePoint " <> show (singleton cp) <> ")" + show (CodePoint i) = "(CodePoint " <> show i <> ")" -- I would prefer that this smart constructor not need to exist and instead -- CodePoint just implements Enum, but the Enum module already depends on this From a1c559c797ce8d0e6d29b89e01d7ecbe199f7c04 Mon Sep 17 00:00:00 2001 From: "Carsten Csiky (csicar)" Date: Fri, 22 Dec 2017 17:50:22 +0100 Subject: [PATCH 3/6] use hex representation --- src/Data/String/CodePoints.purs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Data/String/CodePoints.purs b/src/Data/String/CodePoints.purs index 58ae836..365f38a 100644 --- a/src/Data/String/CodePoints.purs +++ b/src/Data/String/CodePoints.purs @@ -29,7 +29,9 @@ import Prelude import Data.Array as Array import Data.Char as Char +import Data.Int (hexadecimal, toStringAs) import Data.Maybe (Maybe(Just, Nothing)) +import Data.String (Pattern(..), Replacement(..), charAt, charCodeAt, contains, fromCharArray, joinWith, localeCompare, null, replace, replaceAll, split, stripPrefix, stripSuffix, toChar, toCharArray, toLower, toUpper, trim) as StringReExports import Data.String as String import Data.String.Unsafe as Unsafe -- WARN: If a new function is added to Data.String, a version of that function @@ -51,7 +53,7 @@ derive instance eqCodePoint :: Eq CodePoint derive instance ordCodePoint :: Ord CodePoint instance showCodePoint :: Show CodePoint where - show (CodePoint i) = "(CodePoint " <> show i <> ")" + show (CodePoint i) = "(CodePoint 0x" <> toStringAs hexadecimal i <> ")" -- I would prefer that this smart constructor not need to exist and instead -- CodePoint just implements Enum, but the Enum module already depends on this From 7e3d1a688db438650f8fbbd22eda10ff56cded52 Mon Sep 17 00:00:00 2001 From: "Carsten Csiky (csicar)" Date: Sat, 23 Dec 2017 17:10:37 +0100 Subject: [PATCH 4/6] add purescript-integers as dependency --- bower.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 5178621..385c62f 100644 --- a/bower.json +++ b/bower.json @@ -22,7 +22,8 @@ "purescript-maybe": "^3.0.0", "purescript-partial": "^1.2.0", "purescript-unfoldable": "^3.0.0", - "purescript-arrays": "^4.0.1" + "purescript-arrays": "^4.0.1", + "purescript-integers": "^3.2.0" }, "devDependencies": { "purescript-assert": "^3.0.0", From 69c40b1c5a37ef900698a7b15cc1b6ecda343f73 Mon Sep 17 00:00:00 2001 From: "Carsten Csiky (csicar)" Date: Sat, 23 Dec 2017 17:11:00 +0100 Subject: [PATCH 5/6] remove duplicate import --- src/Data/String/CodePoints.purs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Data/String/CodePoints.purs b/src/Data/String/CodePoints.purs index 365f38a..91ba9e1 100644 --- a/src/Data/String/CodePoints.purs +++ b/src/Data/String/CodePoints.purs @@ -31,7 +31,6 @@ import Data.Array as Array import Data.Char as Char import Data.Int (hexadecimal, toStringAs) import Data.Maybe (Maybe(Just, Nothing)) -import Data.String (Pattern(..), Replacement(..), charAt, charCodeAt, contains, fromCharArray, joinWith, localeCompare, null, replace, replaceAll, split, stripPrefix, stripSuffix, toChar, toCharArray, toLower, toUpper, trim) as StringReExports import Data.String as String import Data.String.Unsafe as Unsafe -- WARN: If a new function is added to Data.String, a version of that function From aaf0d21079c506c035a259fbeb76705e1a89aa0c Mon Sep 17 00:00:00 2001 From: "Carsten Csiky (csicar)" Date: Sat, 23 Dec 2017 17:11:22 +0100 Subject: [PATCH 6/6] added tests for show; and corrected show --- src/Data/String/CodePoints.purs | 2 +- test/Test/Data/String/CodePoints.purs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Data/String/CodePoints.purs b/src/Data/String/CodePoints.purs index 91ba9e1..9e7a121 100644 --- a/src/Data/String/CodePoints.purs +++ b/src/Data/String/CodePoints.purs @@ -52,7 +52,7 @@ derive instance eqCodePoint :: Eq CodePoint derive instance ordCodePoint :: Ord CodePoint instance showCodePoint :: Show CodePoint where - show (CodePoint i) = "(CodePoint 0x" <> toStringAs hexadecimal i <> ")" + show (CodePoint i) = "(CodePoint 0x" <> String.toUpper (toStringAs hexadecimal i) <> ")" -- I would prefer that this smart constructor not need to exist and instead -- CodePoint just implements Enum, but the Enum module already depends on this diff --git a/test/Test/Data/String/CodePoints.purs b/test/Test/Data/String/CodePoints.purs index 7a6aef0..cd60c07 100644 --- a/test/Test/Data/String/CodePoints.purs +++ b/test/Test/Data/String/CodePoints.purs @@ -15,6 +15,15 @@ str = "a\xDC00\xD800\xD800\x16805\x16A06\&z" testStringCodePoints :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit testStringCodePoints = do + log "show" + assert $ map show (codePointAt 0 str) == Just "(CodePoint 0x61)" + assert $ map show (codePointAt 1 str) == Just "(CodePoint 0xDC00)" + assert $ map show (codePointAt 2 str) == Just "(CodePoint 0xD800)" + assert $ map show (codePointAt 3 str) == Just "(CodePoint 0xD800)" + assert $ map show (codePointAt 4 str) == Just "(CodePoint 0x16805)" + assert $ map show (codePointAt 5 str) == Just "(CodePoint 0x16A06)" + assert $ map show (codePointAt 6 str) == Just "(CodePoint 0x7A)" + log "codePointAt" assert $ codePointAt (-1) str == Nothing assert $ codePointAt 0 str == (codePointFromInt 0x61)