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
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/Data/String/CodePoints.purs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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 as String
import Data.String.Unsafe as Unsafe
Expand All @@ -50,6 +51,9 @@ newtype CodePoint = CodePoint Int
derive instance eqCodePoint :: Eq CodePoint
derive instance ordCodePoint :: Ord CodePoint

instance showCodePoint :: Show CodePoint where
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
-- one. To avoid the circular dependency, we just expose these two functions.
Expand Down
9 changes: 9 additions & 0 deletions test/Test/Data/String/CodePoints.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down