Skip to content

Commit 66b7bbc

Browse files
csicarhdgarrood
authored andcommitted
Show Instance for CodePoint (#93)
1 parent b3485f6 commit 66b7bbc

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"purescript-maybe": "^3.0.0",
2323
"purescript-partial": "^1.2.0",
2424
"purescript-unfoldable": "^3.0.0",
25-
"purescript-arrays": "^4.0.1"
25+
"purescript-arrays": "^4.0.1",
26+
"purescript-integers": "^3.2.0"
2627
},
2728
"devDependencies": {
2829
"purescript-assert": "^3.0.0",

src/Data/String/CodePoints.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Prelude
2929

3030
import Data.Array as Array
3131
import Data.Char as Char
32+
import Data.Int (hexadecimal, toStringAs)
3233
import Data.Maybe (Maybe(Just, Nothing))
3334
import Data.String as String
3435
import Data.String.Unsafe as Unsafe
@@ -50,6 +51,9 @@ newtype CodePoint = CodePoint Int
5051
derive instance eqCodePoint :: Eq CodePoint
5152
derive instance ordCodePoint :: Ord CodePoint
5253

54+
instance showCodePoint :: Show CodePoint where
55+
show (CodePoint i) = "(CodePoint 0x" <> String.toUpper (toStringAs hexadecimal i) <> ")"
56+
5357
-- I would prefer that this smart constructor not need to exist and instead
5458
-- CodePoint just implements Enum, but the Enum module already depends on this
5559
-- one. To avoid the circular dependency, we just expose these two functions.

test/Test/Data/String/CodePoints.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ str = "a\xDC00\xD800\xD800\x16805\x16A06\&z"
1515

1616
testStringCodePoints :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT | eff) Unit
1717
testStringCodePoints = do
18+
log "show"
19+
assert $ map show (codePointAt 0 str) == Just "(CodePoint 0x61)"
20+
assert $ map show (codePointAt 1 str) == Just "(CodePoint 0xDC00)"
21+
assert $ map show (codePointAt 2 str) == Just "(CodePoint 0xD800)"
22+
assert $ map show (codePointAt 3 str) == Just "(CodePoint 0xD800)"
23+
assert $ map show (codePointAt 4 str) == Just "(CodePoint 0x16805)"
24+
assert $ map show (codePointAt 5 str) == Just "(CodePoint 0x16A06)"
25+
assert $ map show (codePointAt 6 str) == Just "(CodePoint 0x7A)"
26+
1827
log "codePointAt"
1928
assert $ codePointAt (-1) str == Nothing
2029
assert $ codePointAt 0 str == (codePointFromInt 0x61)

0 commit comments

Comments
 (0)