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", diff --git a/src/Data/String/CodePoints.purs b/src/Data/String/CodePoints.purs index 7fe51f3..9e7a121 100644 --- a/src/Data/String/CodePoints.purs +++ b/src/Data/String/CodePoints.purs @@ -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 @@ -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. 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)