diff options
Diffstat (limited to 'libraries/ghc-boot/GHC/Utils/Encoding.hs')
-rw-r--r-- | libraries/ghc-boot/GHC/Utils/Encoding.hs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/libraries/ghc-boot/GHC/Utils/Encoding.hs b/libraries/ghc-boot/GHC/Utils/Encoding.hs index 519b607425..ba07784b0d 100644 --- a/libraries/ghc-boot/GHC/Utils/Encoding.hs +++ b/libraries/ghc-boot/GHC/Utils/Encoding.hs @@ -27,6 +27,7 @@ module GHC.Utils.Encoding ( utf8DecodeStringLazy, utf8EncodeChar, utf8EncodeString, + utf8EncodeStringPtr, utf8EncodeShortByteString, utf8EncodedLength, countUTF8Chars, @@ -276,8 +277,17 @@ utf8EncodeChar write# c = #endif s -> (# s, () #) -utf8EncodeString :: Ptr Word8 -> String -> IO () -utf8EncodeString (Ptr a#) str = go a# str +utf8EncodeString :: String -> ByteString +utf8EncodeString s = + unsafePerformIO $ do + let len = utf8EncodedLength s + buf <- mallocForeignPtrBytes len + withForeignPtr buf $ \ptr -> do + utf8EncodeStringPtr ptr s + pure (BS.fromForeignPtr buf 0 len) + +utf8EncodeStringPtr :: Ptr Word8 -> String -> IO () +utf8EncodeStringPtr (Ptr a#) str = go a# str where go !_ [] = return () go a# (c:cs) = do #if !MIN_VERSION_base(4,16,0) |