[commit: packages/binary] master: Make get :: Get Char total (62c41d7)

git at git.haskell.org git at git.haskell.org
Mon Jun 1 08:47:29 UTC 2015


Repository : ssh://git@git.haskell.org/binary

On branch  : master
Link       : http://git.haskell.org/packages/binary.git/commitdiff/62c41d79c4df5e26c6d4b39a56b7f54896eaa3ff

>---------------------------------------------------------------

commit 62c41d79c4df5e26c6d4b39a56b7f54896eaa3ff
Author: Thomas Tuegel <ttuegel at gmail.com>
Date:   Fri Feb 20 12:43:59 2015 -0600

    Make get :: Get Char total
    
    The old Binary instance for Char used Data.Char.chr, which calls error
    if given an invalid code point. The instance is modified to call fail
    instead of error on invalid code points, which is total (within the Get
    Monad). Calls to error inside Get will escape decodeOrFail.


>---------------------------------------------------------------

62c41d79c4df5e26c6d4b39a56b7f54896eaa3ff
 src/Data/Binary/Class.hs | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/Data/Binary/Class.hs b/src/Data/Binary/Class.hs
index 8b7bc13..2477056 100644
--- a/src/Data/Binary/Class.hs
+++ b/src/Data/Binary/Class.hs
@@ -47,7 +47,7 @@ import Foreign
 import Data.ByteString.Lazy (ByteString)
 import qualified Data.ByteString.Lazy as L
 
-import Data.Char    (chr,ord)
+import Data.Char    (ord)
 import Data.List    (unfoldr)
 
 -- And needed for the instances:
@@ -382,7 +382,11 @@ instance Binary Char where
                                 z <- liftM (xor 0x80) getByte
                                 return (z .|. shiftL6 (y .|. shiftL6
                                         (x .|. shiftL6 (xor 0xf0 w))))
-        return $! chr r
+        getChr r
+      where
+        getChr w
+          | w <= 0x10ffff = return $! toEnum $ fromEnum w
+          | otherwise = fail "Not a valid Unicode code point!"
 
 ------------------------------------------------------------------------
 -- Instances for the first few tuples



More information about the ghc-commits mailing list