Use lower upper bound for pre-allocation in `encodeUtf8`
The rationale is that we encode from UTF16 code-units to UTF8, and
to encode a *single* UTF16 code-unit we need at most 3-byte in UTF8.
Wherease 4-byte UTF8 encodings would require surrogate UTF16 pairs.
Fixes #194
commitd87a8cf428c36fad40650e15b0eeda234c5e95cc
authorHerbert Valerio Riedel <hvr@gnu.org>
dateMon 28 Aug 2017, 13:17 (9 years ago)
parent2d88a0a
Data/Text/Encoding.hs+1−1
@@ -373,7 +373,7 @@ encodeUtf8 :: Text -> ByteString
373373encodeUtf8 (Text arr off len)
374374 | len == 0 = B.empty
375375 | otherwise = unsafeDupablePerformIO $ do
376− fp <- mallocByteString (len*4)
376+ fp <- mallocByteString (len*3) -- see https://github.com/haskell/text/issues/194 for why len*3 is enough
377377 withForeignPtr fp $ \ptr ->
378378 with ptr $ \destPtr -> do
379379 c_encode_utf8 destPtr (A.aBA arr) (fromIntegral off) (fromIntegral len)