Skip to content

Commit 8d49474

Browse files
committed
Serialise Texts without going through ByteString
1 parent aee6924 commit 8d49474

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/Data/Text.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ import qualified Data.Text.Array as A
235235
import qualified Data.List as L hiding (head, tail)
236236
import qualified Data.List.NonEmpty as NonEmptyList
237237
import Data.Binary (Binary(get, put))
238+
import Data.Binary.Put (putBuilder)
238239
import Data.Monoid (Monoid(..))
239240
import Data.Semigroup (Semigroup(..))
240241
import Data.String (IsString(..))
@@ -245,7 +246,7 @@ import Data.Text.Internal.Measure (measure_off)
245246
import Data.Text.Internal.Encoding.Utf8 (utf8Length, utf8LengthByLeader, chr3, ord2, ord3, ord4)
246247
import qualified Data.Text.Internal.Fusion as S
247248
import qualified Data.Text.Internal.Fusion.Common as S
248-
import Data.Text.Encoding (decodeUtf8', encodeUtf8)
249+
import Data.Text.Encoding (decodeUtf8', encodeUtf8Builder)
249250
import Data.Text.Internal.Fusion (stream, reverseStream, unstream)
250251
import Data.Text.Internal.Private (span_)
251252
import Data.Text.Internal (Text(..), StrictText, empty, firstf, mul, safe, text, append, pack)
@@ -394,7 +395,9 @@ instance NFData Text where rnf !_ = ()
394395

395396
-- | @since 1.2.1.0
396397
instance Binary Text where
397-
put t = put (encodeUtf8 t)
398+
put t = do
399+
put (lengthWord8 t)
400+
putBuilder (encodeUtf8Builder t)
398401
get = do
399402
bs <- get
400403
case decodeUtf8' bs of
@@ -556,7 +559,7 @@ null (Text _arr _off len) =
556559
len <= 0
557560
{-# INLINE [1] null #-}
558561

559-
{-# RULES
562+
{-# RULES
560563
"TEXT null/empty -> True" null empty = True
561564
#-}
562565

@@ -1275,7 +1278,7 @@ take :: Int -> Text -> Text
12751278
take n t@(Text arr off len)
12761279
| n <= 0 = empty
12771280
| n >= len || m >= len || m < 0 = t
1278-
| otherwise = Text arr off m
1281+
| otherwise = Text arr off m
12791282
where
12801283
m = measureOff n t
12811284
{-# INLINE [1] take #-}
@@ -1325,7 +1328,7 @@ drop :: Int -> Text -> Text
13251328
drop n t@(Text arr off len)
13261329
| n <= 0 = t
13271330
| n >= len || m >= len || m < 0 = empty
1328-
| otherwise = Text arr (off+m) (len-m)
1331+
| otherwise = Text arr (off+m) (len-m)
13291332
where m = measureOff n t
13301333
{-# INLINE [1] drop #-}
13311334

@@ -1434,7 +1437,7 @@ splitAt :: Int -> Text -> (Text, Text)
14341437
splitAt n t@(Text arr off len)
14351438
| n <= 0 = (empty, t)
14361439
| n >= len || m >= len || m < 0 = (t, empty)
1437-
| otherwise = (Text arr off m, Text arr (off+m) (len-m))
1440+
| otherwise = (Text arr off m, Text arr (off+m) (len-m))
14381441
where
14391442
m = measureOff n t
14401443

0 commit comments

Comments
 (0)