Changeset 10467
- Timestamp:
- 06/03/06 08:13:11 (3 years ago)
- Location:
- third-party/fps/Data
- Files:
-
- 6 modified
-
ByteString.hs (modified) (51 diffs)
-
ByteString/Base.hs (modified) (13 diffs)
-
ByteString/Char8.hs (modified) (5 diffs)
-
ByteString/Fusion.hs (modified) (24 diffs)
-
ByteString/Lazy.hs (modified) (22 diffs)
-
ByteString/Lazy/Char8.hs (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
third-party/fps/Data/ByteString.hs
r10434 r10467 88 88 scanl, -- :: (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteString 89 89 scanl1, -- :: (Word8 -> Word8 -> Word8) -> ByteString -> ByteString 90 scanr, -- :: (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteString 91 scanr1, -- :: (Word8 -> Word8 -> Word8) -> ByteString -> ByteString 90 92 91 93 -- ** Accumulating maps 92 94 mapAccumL, -- :: (acc -> Word8 -> (acc, Word8)) -> acc -> ByteString -> (acc, ByteString) 93 --mapAccumR, -- :: (acc -> Word8 -> (acc, Word8)) -> acc -> ByteString -> (acc, ByteString)95 mapAccumR, -- :: (acc -> Word8 -> (acc, Word8)) -> acc -> ByteString -> (acc, ByteString) 94 96 mapIndexed, -- :: (Int -> Word8 -> Word8) -> ByteString -> ByteString 95 97 … … 186 188 -- | These functions perform memcpy(3) operations 187 189 copy, -- :: ByteString -> ByteString 188 copyCString, -- :: CString -> ByteString189 copyCStringLen, -- :: CStringLen -> ByteString190 copyCString, -- :: CString -> IO ByteString 191 copyCStringLen, -- :: CStringLen -> IO ByteString 190 192 191 193 -- * I\/O with 'ByteString's … … 203 205 readFile, -- :: FilePath -> IO ByteString 204 206 writeFile, -- :: FilePath -> ByteString -> IO () 207 appendFile, -- :: FilePath -> ByteString -> IO () 205 208 -- mmapFile, -- :: FilePath -> IO ByteString 206 209 … … 220 223 unpackList, -- eek, otherwise it gets thrown away by the simplifier 221 224 #endif 225 lengthU, maximumU, minimumU 222 226 ) where 223 227 … … 228 232 ,dropWhile,span,break,elem,filter,maximum 229 233 ,minimum,all,concatMap,foldl1,foldr1 230 ,scanl,scanl1,readFile,writeFile,replicate 234 ,scanl,scanl1,scanr,scanr1 235 ,readFile,writeFile,appendFile,replicate 231 236 ,getContents,getLine,putStr,putStrLn 232 237 ,zip,zipWith,unzip,notElem) … … 243 248 244 249 -- Control.Exception.bracket not available in yhc or nhc 245 import Control.Exception (bracket )250 import Control.Exception (bracket, assert) 246 251 import Control.Monad (when) 247 252 … … 371 376 -- | /O(1)/ The empty 'ByteString' 372 377 empty :: ByteString 373 empty = inlinePerformIO $ mallocByteString 1 >>= \fp -> return $ PS fp 0 0378 empty = unsafeCreate 0 $ const $ return () 374 379 {-# NOINLINE empty #-} 375 380 376 381 -- | /O(1)/ Convert a 'Word8' into a 'ByteString' 377 382 singleton :: Word8 -> ByteString 378 singleton c = unsafePerformIO $ mallocByteString 2 >>= \fp -> do 379 withForeignPtr fp $ \p -> poke p c 380 return $ PS fp 0 1 383 singleton c = unsafeCreate 1 $ \p -> poke p c 381 384 {-# INLINE singleton #-} 382 385 … … 408 411 #if !defined(__GLASGOW_HASKELL__) 409 412 410 pack str = create (P.length str) $ \p -> go p str413 pack str = unsafeCreate (P.length str) $ \p -> go p str 411 414 where 412 415 go _ [] = return () … … 415 418 #else /* hack away */ 416 419 417 pack str = create (P.length str) $ \(Ptr p) -> stToIO (go p 0# str)420 pack str = unsafeCreate (P.length str) $ \(Ptr p) -> stToIO (go p 0# str) 418 421 where 419 422 go _ _ [] = return () … … 468 471 469 472 -- TODO just use normal foldr here. 473 -- 474 -- or 475 -- unpack xs | null xs = [] 476 -- | otherwise = unsafeHead xs : unpack (unsafeTail xs) 477 -- 478 -- ? 470 479 471 480 #endif … … 476 485 -- conversion function 477 486 packWith :: (a -> Word8) -> [a] -> ByteString 478 packWith k str = create (P.length str) $ \p -> go p str487 packWith k str = unsafeCreate (P.length str) $ \p -> go p str 479 488 where 480 489 STRICT2(go) … … 501 510 -- | /O(1)/ Test whether a ByteString is empty. 502 511 null :: ByteString -> Bool 503 null (PS _ _ l) = l == 0512 null (PS _ _ l) = assert (l >= 0) $ l <= 0 504 513 {-# INLINE null #-} 505 514 515 -- --------------------------------------------------------------------- 506 516 -- | /O(1)/ 'length' returns the length of a ByteString as an 'Int'. 507 517 length :: ByteString -> Int 508 length (PS _ _ l) = l 518 length (PS _ _ l) = assert (l >= 0) $ l 519 520 -- 521 -- length/loop fusion. When taking the length of any fuseable loop, 522 -- rewrite it as a foldl', and thus avoid allocating the result buffer 523 -- worth around 10% in speed testing. 524 -- 509 525 510 526 #if defined(__GLASGOW_HASKELL__) … … 512 528 #endif 513 529 514 {-# 515 516 -- Translate length into a loop. 517 -- Performace ok, but allocates too much, so disable for now. 518 519 "length/loop" forall f acc s . 520 length (loopArr (loopU f acc s)) = foldl' (const . (+1)) (0::Int) (loopArr (loopU f acc s)) 530 lengthU :: ByteString -> Int 531 lengthU = foldl' (const . (+1)) (0::Int) 532 {-# INLINE lengthU #-} 533 534 {-# RULES 535 536 -- v2 fusion 537 "length/loop" forall loop s . 538 length (loopArr (loopWrapper loop s)) = 539 lengthU (loopArr (loopWrapper loop s)) 521 540 522 541 #-} 542 543 ------------------------------------------------------------------------ 523 544 524 545 -- | /O(n)/ 'cons' is analogous to (:) for lists, but of different 525 546 -- complexity, as it requires a memcpy. 526 547 cons :: Word8 -> ByteString -> ByteString 527 cons c (PS x s l) = create (l+1) $ \p -> withForeignPtr x $ \f -> do548 cons c (PS x s l) = unsafeCreate (l+1) $ \p -> withForeignPtr x $ \f -> do 528 549 poke p c 529 550 memcpy (p `plusPtr` 1) (f `plusPtr` s) (fromIntegral l) 530 551 {-# INLINE cons #-} 531 552 532 -- todo fuse533 534 553 -- | /O(n)/ Append a byte to the end of a 'ByteString' 535 554 snoc :: ByteString -> Word8 -> ByteString 536 snoc (PS x s l) c = create (l+1) $ \p -> withForeignPtr x $ \f -> do555 snoc (PS x s l) c = unsafeCreate (l+1) $ \p -> withForeignPtr x $ \f -> do 537 556 memcpy p (f `plusPtr` s) (fromIntegral l) 538 557 poke (p `plusPtr` l) c … … 542 561 543 562 -- | /O(1)/ Extract the first element of a ByteString, which must be non-empty. 563 -- An exception will be thrown in the case of an empty ByteString. 544 564 head :: ByteString -> Word8 545 565 head ps@(PS x s _) … … 549 569 550 570 -- | /O(1)/ Extract the elements after the head of a ByteString, which must be non-empty. 571 -- An exception will be thrown in the case of an empty ByteString. 551 572 tail :: ByteString -> ByteString 552 573 tail (PS p s l) … … 556 577 557 578 -- | /O(1)/ Extract the last element of a ByteString, which must be finite and non-empty. 579 -- An exception will be thrown in the case of an empty ByteString. 558 580 last :: ByteString -> Word8 559 581 last ps@(PS x s l) … … 563 585 564 586 -- | /O(1)/ Return all the elements of a 'ByteString' except the last one. 587 -- An exception will be thrown in the case of an empty ByteString. 565 588 init :: ByteString -> ByteString 566 init (PS p s l)567 | l <= 0= errorEmptyList "init"589 init ps@(PS p s l) 590 | null ps = errorEmptyList "init" 568 591 | otherwise = PS p s (l-1) 569 592 {-# INLINE init #-} … … 582 605 -- element of @xs@. This function is subject to array fusion. 583 606 map :: (Word8 -> Word8) -> ByteString -> ByteString 607 #if defined(LOOPU_FUSION) 584 608 map f = loopArr . loopU (mapEFL f) NoAcc 609 #elif defined(LOOPUP_FUSION) 610 map f = loopArr . loopUp (mapEFL f) NoAcc 611 #elif defined(LOOPNOACC_FUSION) 612 map f = loopArr . loopNoAcc (mapEFL f) 613 #else 614 map f = loopArr . loopMap f 615 #endif 585 616 {-# INLINE map #-} 586 617 … … 588 619 -- slightly faster for one-shot cases. 589 620 map' :: (Word8 -> Word8) -> ByteString -> ByteString 590 map' f (PS fp s len) = inlinePerformIO $ withForeignPtr fp $ \a -> do 591 np <- mallocByteString (len+1) 592 withForeignPtr np $ \p -> do 593 map_ 0 (a `plusPtr` s) p 594 return (PS np 0 len) 621 map' f (PS fp s len) = inlinePerformIO $ withForeignPtr fp $ \a -> 622 create len $ map_ 0 (a `plusPtr` s) 595 623 where 596 597 624 map_ :: Int -> Ptr Word8 -> Ptr Word8 -> IO () 598 625 STRICT3(map_) … … 607 634 -- | /O(n)/ 'reverse' @xs@ efficiently returns the elements of @xs@ in reverse order. 608 635 reverse :: ByteString -> ByteString 609 reverse (PS x s l) = create l $ \p -> withForeignPtr x $ \f ->636 reverse (PS x s l) = unsafeCreate l $ \p -> withForeignPtr x $ \f -> 610 637 c_reverse p (f `plusPtr` s) (fromIntegral l) 611 638 612 {- 613 reverse = pack . P.reverse . unpack 614 -} 639 -- todo, fuseable version 615 640 616 641 -- | /O(n)/ The 'intersperse' function takes a 'Word8' and a … … 621 646 intersperse c ps@(PS x s l) 622 647 | length ps < 2 = ps 623 | otherwise = create (2*l-1) $ \p -> withForeignPtr x $ \f ->648 | otherwise = unsafeCreate (2*l-1) $ \p -> withForeignPtr x $ \f -> 624 649 c_intersperse p (f `plusPtr` s) (fromIntegral l) c 625 650 … … 641 666 -- This function is subject to array fusion. 642 667 foldl :: (a -> Word8 -> a) -> a -> ByteString -> a 668 #if !defined(LOOPU_FUSION) 669 foldl f z = loopAcc . loopUp (foldEFL f) z 670 #else 643 671 foldl f z = loopAcc . loopU (foldEFL f) z 672 #endif 644 673 {-# INLINE foldl #-} 645 674 … … 659 688 660 689 -- | 'foldl\'' is like 'foldl', but strict in the accumulator. 690 -- Though actually foldl is also strict in the accumulator. 661 691 foldl' :: (a -> Word8 -> a) -> a -> ByteString -> a 662 foldl' f z = loopAcc . loopU (foldEFL' f) z 692 foldl' = foldl 693 -- foldl' f z = loopAcc . loopU (foldEFL' f) z 663 694 {-# INLINE foldl' #-} 664 695 … … 667 698 -- reduces the ByteString using the binary operator, from right to left. 668 699 foldr :: (Word8 -> a -> a) -> a -> ByteString -> a 669 foldr k z (PS x s l) = inlinePerformIO $ withForeignPtr x $ \ptr -> 670 go (ptr `plusPtr` s) (ptr `plusPtr` (s+l)) 671 where 672 STRICT2(go) 673 go p q | p == q = return z 674 | otherwise = do c <- peek p 675 ws <- go (p `plusPtr` 1) q 676 return $ c `k` ws 700 foldr k z = loopAcc . loopDown (foldEFL (flip k)) z 701 {-# INLINE foldr #-} 677 702 678 703 -- | 'foldl1' is a variant of 'foldl' that has no starting value 679 704 -- argument, and thus must be applied to non-empty 'ByteStrings'. 680 -- This function is subject to array fusion. 705 -- This function is subject to array fusion. 706 -- An exception will be thrown in the case of an empty ByteString. 681 707 foldl1 :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8 682 708 foldl1 f ps 683 709 | null ps = errorEmptyList "foldl1" 684 710 | otherwise = foldl f (unsafeHead ps) (unsafeTail ps) 711 {-# INLINE foldl1 #-} 685 712 686 713 -- | 'foldl1\'' is like 'foldl1', but strict in the accumulator. 714 -- An exception will be thrown in the case of an empty ByteString. 687 715 foldl1' :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8 688 716 foldl1' f ps 689 717 | null ps = errorEmptyList "foldl1'" 690 718 | otherwise = foldl' f (unsafeHead ps) (unsafeTail ps) 719 {-# INLINE foldl1' #-} 691 720 692 721 -- | 'foldr1' is a variant of 'foldr' that has no starting value argument, 693 722 -- and thus must be applied to non-empty 'ByteString's 723 -- An exception will be thrown in the case of an empty ByteString. 694 724 foldr1 :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8 695 725 foldr1 f ps 696 726 | null ps = errorEmptyList "foldr1" 697 727 | otherwise = foldr f (last ps) (init ps) 728 {-# INLINE foldr1 #-} 698 729 699 730 -- --------------------------------------------------------------------- … … 704 735 concat [] = empty 705 736 concat [ps] = ps 706 concat xs = create len $ \ptr -> go xs ptr737 concat xs = unsafeCreate len $ \ptr -> go xs ptr 707 738 where len = P.sum . P.map length $ xs 708 739 STRICT2(go) … … 745 776 then go (p `plusPtr` 1) q 746 777 else return False 747 -- todo fuse 778 779 ------------------------------------------------------------------------ 748 780 749 781 -- | /O(n)/ 'maximum' returns the maximum value from a 'ByteString' 782 -- This function will fuse. 783 -- An exception will be thrown in the case of an empty ByteString. 750 784 maximum :: ByteString -> Word8 751 785 maximum xs@(PS x s l) 752 786 | null xs = errorEmptyList "maximum" 753 787 | otherwise = inlinePerformIO $ withForeignPtr x $ \p -> 754 return $ c_maximum (p `plusPtr` s) (fromIntegral l) 755 {-# INLINE maximum #-} 788 c_maximum (p `plusPtr` s) (fromIntegral l) 756 789 757 790 -- | /O(n)/ 'minimum' returns the minimum value from a 'ByteString' 791 -- This function will fuse. 792 -- An exception will be thrown in the case of an empty ByteString. 758 793 minimum :: ByteString -> Word8 759 794 minimum xs@(PS x s l) 760 795 | null xs = errorEmptyList "minimum" 761 796 | otherwise = inlinePerformIO $ withForeignPtr x $ \p -> 762 return $ c_minimum (p `plusPtr` s) (fromIntegral l) 763 {-# INLINE minimum #-} 764 765 -- fusion is too slow here (10x) 766 767 {- 768 maximum xs@(PS x s l) 769 | null xs = errorEmptyList "maximum" 770 | otherwise = inlinePerformIO $ withForeignPtr x $ \p -> do 771 w <- peek p 772 maximum_ (p `plusPtr` s) 0 l w 773 774 maximum_ :: Ptr Word8 -> Int -> Int -> Word8 -> IO Word8 775 STRICT4(maximum_) 776 maximum_ ptr n m c 777 | n >= m = return c 778 | otherwise = do w <- peekByteOff ptr n 779 maximum_ ptr (n+1) m (if w > c then w else c) 780 781 minimum xs@(PS x s l) 782 | null xs = errorEmptyList "minimum" 783 | otherwise = inlinePerformIO $ withForeignPtr x $ \p -> do 784 w <- peek p 785 minimum_ (p `plusPtr` s) 0 l w 786 787 minimum_ :: Ptr Word8 -> Int -> Int -> Word8 -> IO Word8 788 STRICT4(minimum_) 789 minimum_ ptr n m c 790 | n >= m = return c 791 | otherwise = do w <- peekByteOff ptr n 792 minimum_ ptr (n+1) m (if w < c then w else c) 793 -} 797 c_minimum (p `plusPtr` s) (fromIntegral l) 798 799 -- 800 -- minimum/maximum/loop fusion. As for length (and other folds), when we 801 -- see we're applied after a fuseable op, switch from using the C 802 -- version, to the fuseable version. The result should then avoid 803 -- allocating a buffer. 804 -- 805 806 #if defined(__GLASGOW_HASKELL__) 807 {-# INLINE [1] minimum #-} 808 {-# INLINE [1] maximum #-} 809 #endif 810 811 maximumU :: ByteString -> Word8 812 maximumU = foldl1' max 813 {-# INLINE maximumU #-} 814 815 minimumU :: ByteString -> Word8 816 minimumU = foldl1' min 817 {-# INLINE minimumU #-} 818 819 {-# RULES 820 821 "minimum/loop" forall loop s . 822 minimum (loopArr (loopWrapper loop s)) = 823 minimumU (loopArr (loopWrapper loop s)) 824 825 "maximum/loop" forall loop s . 826 maximum (loopArr (loopWrapper loop s)) = 827 maximumU (loopArr (loopWrapper loop s)) 828 829 #-} 830 831 ------------------------------------------------------------------------ 794 832 795 833 mapAccumL :: (acc -> Word8 -> (acc, Word8)) -> acc -> ByteString -> (acc, ByteString) 834 #if !defined(LOOPU_FUSION) 835 mapAccumL f z = unSP . loopUp (mapAccumEFL f) z 836 #else 796 837 mapAccumL f z = unSP . loopU (mapAccumEFL f) z 797 798 --mapAccumR :: (acc -> Word8 -> (acc, Word8)) -> acc -> ByteString -> (acc, ByteString) 838 #endif 839 {-# INLINE mapAccumL #-} 840 841 mapAccumR :: (acc -> Word8 -> (acc, Word8)) -> acc -> ByteString -> (acc, ByteString) 842 mapAccumR f z = unSP . loopDown (mapAccumEFL f) z 843 {-# INLINE mapAccumR #-} 799 844 800 845 -- | /O(n)/ map Word8 functions, provided with the index at each position 801 846 mapIndexed :: (Int -> Word8 -> Word8) -> ByteString -> ByteString 802 mapIndexed f = loopArr . loopU (mapIndexEFL f) 0 847 mapIndexed f = loopArr . loopUp (mapIndexEFL f) 0 848 {-# INLINE mapIndexed #-} 803 849 804 850 -- --------------------------------------------------------------------- … … 814 860 -- > last (scanl f z xs) == foldl f z xs. 815 861 scanl :: (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteString 816 scanl f z ps = loopArr . loopU (scanEFL f) z $ (ps `snoc` 0) -- extra space 862 #if !defined(LOOPU_FUSION) 863 scanl f z ps = loopArr . loopUp (scanEFL f) z $ (ps `snoc` 0) 864 #else 865 scanl f z ps = loopArr . loopU (scanEFL f) z $ (ps `snoc` 0) 866 #endif 867 868 -- n.b. haskell's List scan returns a list one bigger than the 869 -- input, so we need to snoc here to get some extra space, however, 870 -- it breaks map/up fusion (i.e. scanl . map no longer fuses) 817 871 {-# INLINE scanl #-} 818 872 … … 827 881 {-# INLINE scanl1 #-} 828 882 883 -- | scanr is the right-to-left dual of scanl. 884 scanr :: (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteString 885 scanr f z ps = loopArr . loopDown (scanEFL (flip f)) z $ (0 `cons` ps) -- extra space 886 {-# INLINE scanr #-} 887 888 -- | 'scanr1' is a variant of 'scanr' that has no starting value argument. 889 scanr1 :: (Word8 -> Word8 -> Word8) -> ByteString -> ByteString 890 scanr1 f ps 891 | null ps = empty 892 | otherwise = scanr f (last ps) (init ps) -- todo, unsafe versions 893 {-# INLINE scanr1 #-} 894 829 895 -- --------------------------------------------------------------------- 830 896 -- Unfolds and replicates … … 837 903 -- This implemenation uses @memset(3)@ 838 904 replicate :: Int -> Word8 -> ByteString 839 replicate w c | w <= 0 = empty 840 | otherwise = create w $ \ptr -> memset ptr c (fromIntegral w) >> return () 841 842 {- 843 -- About 5x slower 844 replicate w c = inlinePerformIO $ generate w $ \ptr -> go ptr w 845 where 846 STRICT2(go) 847 go _ 0 = return w 848 go ptr n = poke ptr c >> go (ptr `plusPtr` 1) (n-1) 849 -} 905 replicate w c 906 | w <= 0 = empty 907 | otherwise = unsafeCreate w $ \ptr -> 908 memset ptr c (fromIntegral w) >> return () 850 909 851 910 -- | /O(n)/, where /n/ is the length of the result. The 'unfoldr' … … 880 939 unfoldrN i f x0 881 940 | i < 0 = (empty, Just x0) 882 | otherwise = inlinePerformIO $ do 883 fp <- mallocByteString i 884 withForeignPtr fp (\p -> go fp p x0 0) 885 where STRICT4(go) 886 go fp p x n = 941 | otherwise = unsafePerformIO $ createAndTrim' i $ \p -> go p x0 0 942 where STRICT3(go) 943 go p x n = 887 944 case f x of 888 Nothing -> let s = copy (PS fp 0 n) 889 in s `seq` return (s, Nothing) 945 Nothing -> return (0, n, Nothing) 890 946 Just (w,x') 891 | n == i -> return ( PS fp 0 i, Just x)947 | n == i -> return (0, n, Just x) 892 948 | otherwise -> do poke p w 893 go fp(p `plusPtr` 1) x' (n+1)949 go (p `plusPtr` 1) x' (n+1) 894 950 895 951 -- --------------------------------------------------------------------- … … 1063 1119 1064 1120 STRICT1(loop) 1065 loop n = do 1066 let q = memchr (ptr `plusPtr` n) w (fromIntegral (l-n)) 1067 if q == nullPtr 1121 loop n = 1122 let q = inlinePerformIO $ memchr (ptr `plusPtr` n) 1123 w (fromIntegral (l-n)) 1124 in if q == nullPtr 1068 1125 then [PS x (s+n) (l-n)] 1069 1126 else let i = q `minusPtr` ptr in PS x (s+n) (i-n) : loop (i+1) … … 1143 1200 -- 1144 1201 joinWithByte :: Word8 -> ByteString -> ByteString -> ByteString 1145 joinWithByte c f@(PS ffp s l) g@(PS fgp t m) = create len $ \ptr ->1202 joinWithByte c f@(PS ffp s l) g@(PS fgp t m) = unsafeCreate len $ \ptr -> 1146 1203 withForeignPtr ffp $ \fp -> 1147 1204 withForeignPtr fgp $ \gp -> do … … 1172 1229 elemIndex c (PS x s l) = inlinePerformIO $ withForeignPtr x $ \p -> do 1173 1230 let p' = p `plusPtr` s 1174 q =memchr p' c (fromIntegral l)1231 q <- memchr p' c (fromIntegral l) 1175 1232 return $ if q == nullPtr then Nothing else Just $! q `minusPtr` p' 1176 1233 {-# INLINE elemIndex #-} … … 1204 1261 1205 1262 STRICT1(loop) 1206 loop n = let q = memchr (ptr `plusPtr` n) w (fromIntegral (l - n)) 1263 loop n = let q = inlinePerformIO $ memchr (ptr `plusPtr` n) 1264 w (fromIntegral (l - n)) 1207 1265 in if q == nullPtr 1208 1266 then [] … … 1229 1287 count :: Word8 -> ByteString -> Int 1230 1288 count w (PS x s m) = inlinePerformIO $ withForeignPtr x $ \p -> 1231 return $fromIntegral $ c_count (p `plusPtr` s) (fromIntegral m) w1289 fmap fromIntegral $ c_count (p `plusPtr` s) (fromIntegral m) w 1232 1290 {-# INLINE count #-} 1233 1291 … … 1242 1300 STRICT3(go) 1243 1301 go p l i = do 1244 let q =memchr p w l1302 q <- memchr p w l 1245 1303 if q == nullPtr 1246 1304 then return i … … 1303 1361 -- predicate. This function is subject to array fusion. 1304 1362 filter :: (Word8 -> Bool) -> ByteString -> ByteString 1363 #if defined(LOOPU_FUSION) 1305 1364 filter p = loopArr . loopU (filterEFL p) NoAcc 1365 #elif defined(LOOPUP_FUSION) 1366 filter p = loopArr . loopUp (filterEFL p) NoAcc 1367 #elif defined(LOOPNOACC_FUSION) 1368 filter p = loopArr . loopNoAcc (filterEFL p) 1369 #else 1370 filter f = loopArr . loopFilter f 1371 #endif 1306 1372 {-# INLINE filter #-} 1307 1373 … … 1311 1377 filter' k ps@(PS x s l) 1312 1378 | null ps = ps 1313 | otherwise = inlinePerformIO $ generatel $ \p -> withForeignPtr x $ \f -> do1379 | otherwise = unsafePerformIO $ createAndTrim l $ \p -> withForeignPtr x $ \f -> do 1314 1380 t <- go (f `plusPtr` s) p (f `plusPtr` (s + l)) 1315 1381 return (t `minusPtr` p) -- actual length … … 1490 1556 -- | /O(n)/ Sort a ByteString efficiently, using counting sort. 1491 1557 sort :: ByteString -> ByteString 1492 sort (PS input s l) = create l $ \p -> allocaArray 256 $ \arr -> do1558 sort (PS input s l) = unsafeCreate l $ \p -> allocaArray 256 $ \arr -> do 1493 1559 1494 1560 memset (castPtr arr) 0 (256 * fromIntegral (sizeOf (undefined :: CSize))) … … 1504 1570 {- 1505 1571 sort :: ByteString -> ByteString 1506 sort (PS x s l) = create l $ \p -> withForeignPtr x $ \f -> do1572 sort (PS x s l) = unsafeCreate l $ \p -> withForeignPtr x $ \f -> do 1507 1573 memcpy p (f `plusPtr` s) l 1508 1574 c_qsort p l -- inplace … … 1528 1594 -- /strlen(3)/, and thus the complexity is a /O(n)/. 1529 1595 packCString :: CString -> ByteString 1530 packCString cstr = inlinePerformIO $ do1596 packCString cstr = unsafePerformIO $ do 1531 1597 fp <- newForeignPtr_ (castPtr cstr) 1532 return $ PS fp 0 (fromIntegral $ c_strlen cstr) 1598 l <- c_strlen cstr 1599 return $ PS fp 0 (fromIntegral l) 1533 1600 1534 1601 -- | /O(1)/ Build a @ByteString@ from a @CStringLen@. This value will … … 1537 1604 -- required. 1538 1605 packCStringLen :: CStringLen -> ByteString 1539 packCStringLen (ptr,len) = inlinePerformIO $ do1606 packCStringLen (ptr,len) = unsafePerformIO $ do 1540 1607 fp <- newForeignPtr_ (castPtr ptr) 1541 1608 return $ PS fp 0 (fromIntegral len) … … 1544 1611 -- have a @free(3)@ finalizer associated to it. 1545 1612 packMallocCString :: CString -> ByteString 1546 packMallocCString cstr = inlinePerformIO $ do1613 packMallocCString cstr = unsafePerformIO $ do 1547 1614 fp <- newForeignFreePtr (castPtr cstr) 1548 return $ PS fp 0 (fromIntegral $ c_strlen cstr) 1615 len <- c_strlen cstr 1616 return $ PS fp 0 (fromIntegral len) 1549 1617 1550 1618 -- | /O(n) construction/ Use a @ByteString@ with a function requiring a null-terminated @CString@. …
