Skip to content

Commit e8a5440

Browse files
authored
Merge pull request #905 from haskell/unsafeDecodeASCII
unsafer unsafeDecodeASCII
2 parents 84bd935 + ed4f596 commit e8a5440

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/Data/Aeson/Internal/Text.hs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1-
{-# LANGUAGE CPP #-}
1+
{-# LANGUAGE BangPatterns #-}
2+
{-# LANGUAGE CPP #-}
23
module Data.Aeson.Internal.Text (
34
unsafeDecodeASCII,
45
) where
56

6-
import Data.ByteString (ByteString)
7-
import Data.Text (Text)
7+
import Data.ByteString (ByteString)
8+
import Data.Text (Text, empty)
89

9-
import qualified Data.Text.Encoding as TE
10+
11+
#if MIN_VERSION_text(2,0,0)
12+
import Data.Text.Array (Array (..))
13+
import Data.Text.Internal (Text (..))
14+
15+
import qualified Data.ByteString.Short.Internal as SBS
16+
17+
import Data.Aeson.Internal.ByteString
18+
19+
#else
20+
import qualified Data.Text.Encoding as TE
21+
22+
#endif
1023

1124
-- | The input is assumed to contain only 7bit ASCII characters (i.e. @< 0x80@).
1225
-- We use TE.decodeLatin1 here because TE.decodeASCII is currently (text-1.2.4.0)
1326
-- deprecated and equal to TE.decodeUtf8, which is slower than TE.decodeLatin1.
1427
unsafeDecodeASCII :: ByteString -> Text
1528

1629
#if MIN_VERSION_text(2,0,0)
17-
unsafeDecodeASCII = TE.decodeASCII
30+
unsafeDecodeASCII bs = withBS bs $ \_fp len -> if len == 0 then empty else
31+
let !(SBS.SBS arr) = SBS.toShort bs in Text (ByteArray arr) 0 len
32+
1833
#else
1934
unsafeDecodeASCII = TE.decodeLatin1
2035
#endif

0 commit comments

Comments
 (0)