[commit: base] encoding: Use correct encoding in Foreign.C.String.charIsRepresentable by improving GHC.Foreign.charIsRepresentable (af1bbde)
Max Bolingbroke
batterseapower at hotmail.com
Mon Apr 11 18:34:04 CEST 2011
Repository : ssh://darcs.haskell.org//srv/darcs/packages/base
On branch : encoding
http://hackage.haskell.org/trac/ghc/changeset/af1bbdee927a56e4b9749a297f827935d5e87cd7
>---------------------------------------------------------------
commit af1bbdee927a56e4b9749a297f827935d5e87cd7
Author: Max Bolingbroke <batterseapower at hotmail.com>
Date: Mon Apr 11 11:54:34 2011 +0100
Use correct encoding in Foreign.C.String.charIsRepresentable by improving GHC.Foreign.charIsRepresentable
>---------------------------------------------------------------
Foreign/C/String.hs | 2 +-
GHC/Foreign.hs | 5 +----
2 files changed, 2 insertions(+), 5 deletions(-)
diff --git a/Foreign/C/String.hs b/Foreign/C/String.hs
index cbf8a1e..fdefdc6 100644
--- a/Foreign/C/String.hs
+++ b/Foreign/C/String.hs
@@ -229,7 +229,7 @@ charIsRepresentable c = return (ord c < 256)
-- -- | Determines whether a character can be accurately encoded in a 'CString'.
-- -- Unrepresentable characters are converted to '?' or their nearest visual equivalent.
charIsRepresentable :: Char -> IO Bool
-charIsRepresentable = GHC.charIsRepresentable localeEncoding -- NOT foreignEncoding because that ignores failure
+charIsRepresentable = GHC.charIsRepresentable foreignEncoding
#endif
-- single byte characters
diff --git a/GHC/Foreign.hs b/GHC/Foreign.hs
index eeac27e..188e190 100644
--- a/GHC/Foreign.hs
+++ b/GHC/Foreign.hs
@@ -138,14 +138,11 @@ withCStringLen enc = withEncodedCString enc False
-- | Determines whether a character can be accurately encoded in a 'CString'.
--
--- For this to work properly, the supplied 'TextEncoding' must throw an
--- IOException upon encountering an invalid sequence.
>---------------------------------------------------------------
-- Pretty much anyone who uses this function is in a state of sin because
-- whether or not a character is encodable will, in general, depend on the
-- context in which it occurs.
charIsRepresentable :: TextEncoding -> Char -> IO Bool
-charIsRepresentable enc c = withEncodedCString enc False [c] (\_ -> return True) `catchException` (\e -> let _ = e :: IOException in return False)
+charIsRepresentable enc c = withCString enc [c] (fmap (== [c]) . peekCString enc) `catchException` (\e -> let _ = e :: IOException in return False)
-- auxiliary definitions
-- ----------------------
More information about the Cvs-libraries
mailing list