diff options
author | Andreas Klebinger <klebinger.andreas@gmx.at> | 2020-06-10 23:43:01 +0200 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2020-07-15 16:41:03 -0400 |
commit | 895a3beb26de69f5611ea496dddb2b121c1dd5c1 (patch) | |
tree | 9e449eb6b86d2676e0a0875f33dcb4b4eadef22a /libraries | |
parent | dcb423937a052496af73e34a315e3d15882b9f19 (diff) | |
download | haskell-895a3beb26de69f5611ea496dddb2b121c1dd5c1.tar.gz |
winio: Deduplicate logic in encodeMultiByte[Raw]IO.
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/base/GHC/IO/Windows/Encoding.hs | 39 |
1 files changed, 14 insertions, 25 deletions
diff --git a/libraries/base/GHC/IO/Windows/Encoding.hs b/libraries/base/GHC/IO/Windows/Encoding.hs index 78e4b87f12..2a0c3fb730 100644 --- a/libraries/base/GHC/IO/Windows/Encoding.hs +++ b/libraries/base/GHC/IO/Windows/Encoding.hs @@ -48,10 +48,10 @@ import GHC.Real encodeMultiByte :: CodePage -> String -> String encodeMultiByte cp = unsafeLocalState . encodeMultiByteIO cp -encodeMultiByteIO :: CodePage -> String -> IO String -encodeMultiByteIO _ "" = return "" - -- WideCharToMultiByte doesn't handle empty strings -encodeMultiByteIO cp wstr = +{-# INLINE encodeMultiByteIO' #-} +-- | String must not be zero length. +encodeMultiByteIO' :: CodePage -> String -> ((LPCSTR, CInt) -> IO a) -> IO a +encodeMultiByteIO' cp wstr transformer = withCWStringLen wstr $ \(cwstr,len) -> do mbchars' <- failIfZero "WideCharToMultiByte" $ wideCharToMultiByte cp @@ -69,30 +69,19 @@ encodeMultiByteIO cp wstr = (fromIntegral len) mbstr mbchars' nullPtr nullPtr - peekCAStringLen (mbstr,fromIntegral mbchars) -- converts [Char] to UTF-16 + transformer (mbstr,fromIntegral mbchars) + +-- converts [Char] to UTF-16 +encodeMultiByteIO :: CodePage -> String -> IO String +encodeMultiByteIO _ "" = return "" +encodeMultiByteIO cp s = encodeMultiByteIO' cp s toString + where toString (s,l) = peekCAStringLen (s,fromIntegral l) +-- converts [Char] to UTF-16 encodeMultiByteRawIO :: CodePage -> String -> IO (LPCSTR, CInt) encodeMultiByteRawIO _ "" = return (nullPtr, 0) - -- WideCharToMultiByte doesn't handle empty strings -encodeMultiByteRawIO cp wstr = - withCWStringLen wstr $ \(cwstr,len) -> do - mbchars' <- failIfZero "WideCharToMultiByte" $ wideCharToMultiByte - cp - 0 - cwstr - (fromIntegral len) - nullPtr 0 - nullPtr nullPtr - -- mbchar' is the length of buffer required - allocaArray (fromIntegral mbchars') $ \mbstr -> do - mbchars <- failIfZero "WideCharToMultiByte" $ wideCharToMultiByte - cp - 0 - cwstr - (fromIntegral len) - mbstr mbchars' - nullPtr nullPtr - return (mbstr,fromIntegral mbchars) -- converts [Char] to UTF-16 +encodeMultiByteRawIO cp s = encodeMultiByteIO' cp s toSizedCString + where toSizedCString (s,l) = return (s, fromIntegral l) foreign import WINDOWS_CCONV "WideCharToMultiByte" wideCharToMultiByte |