-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
131 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
{-# LANGUAGE CPP #-} | ||
{-# LANGUAGE MagicHash #-} | ||
{-# LANGUAGE UnliftedFFITypes #-} | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
#if defined(PURE_HASKELL) | ||
{-# LANGUAGE BangPatterns #-} | ||
#endif | ||
|
||
{-# OPTIONS_HADDOCK not-home #-} | ||
|
||
{-# OPTIONS_GHC -ddump-to-file -ddump-simpl -dsuppress-all -dno-suppress-type-signatures #-} | ||
|
||
-- | Implements 'reverse', using efficient C routines by default. | ||
module Data.Text.Internal.Reverse (reverse) where | ||
|
||
#if !defined(PURE_HASKELL) | ||
import GHC.Exts as Exts | ||
import Control.Monad.ST.Unsafe (unsafeIOToST) | ||
import Foreign.C.Types (CSize(..)) | ||
#else | ||
import Control.Monad.ST (ST) | ||
import Data.Text.Internal.Encoding.Utf8 (utf8LengthByLeader) | ||
#endif | ||
#if defined(ASSERTS) | ||
import GHC.Stack (HasCallStack) | ||
#endif | ||
import Prelude hiding (reverse) | ||
import Data.Text.Internal (Text(..)) | ||
import Control.Monad.ST (runST) | ||
import qualified Data.Text.Array as A | ||
|
||
-- | /O(n)/ Reverse the characters of a string. | ||
-- | ||
-- Example: | ||
-- | ||
-- $setup | ||
-- >>> T.reverse "desrever" | ||
-- "reversed" | ||
reverse :: | ||
#if defined(ASSERTS) | ||
HasCallStack => | ||
#endif | ||
Text -> Text | ||
#if defined(PURE_HASKELL) | ||
reverse (Text src off len) = runST $ do | ||
dest <- A.new len | ||
_ <- reversePoints src off dest len | ||
result <- A.unsafeFreeze dest | ||
pure $ Text result 0 len | ||
|
||
-- Step 0: | ||
-- | ||
-- Input: R E D R U M | ||
-- ^ | ||
-- x | ||
-- Output: _ _ _ _ _ _ | ||
-- ^ | ||
-- y | ||
-- | ||
-- Step 1: | ||
-- | ||
-- Input: R E D R U M | ||
-- ^ | ||
-- x | ||
-- | ||
-- Output: _ _ _ _ _ R | ||
-- ^ | ||
-- y | ||
reversePoints | ||
:: A.Array -- ^ Input array | ||
-> Int -- ^ Input index | ||
-> A.MArray s -- ^ Output array | ||
-> Int -- ^ Output index | ||
-> ST s () | ||
reversePoints src xx dest yy = go xx yy where | ||
go !_ y | y <= 0 = pure () | ||
go x y = | ||
let pLen = utf8LengthByLeader (A.unsafeIndex src x) | ||
-- The next y is also the start of the current point in the output | ||
yNext = y - pLen | ||
in do | ||
A.copyI pLen dest yNext src x | ||
go (x + pLen) yNext | ||
#else | ||
reverse (Text (A.ByteArray ba) off len) = runST $ do | ||
marr@(A.MutableByteArray mba) <- A.new len | ||
unsafeIOToST $ c_reverse mba ba (fromIntegral off) (fromIntegral len) | ||
brr <- A.unsafeFreeze marr | ||
return $ Text brr 0 len | ||
#endif | ||
{-# INLINE reverse #-} | ||
|
||
#if !defined(PURE_HASKELL) | ||
-- | The input buffer (src :: ByteArray#, off :: CSize, len :: CSize) | ||
-- must specify a valid UTF-8 sequence, this condition is not checked. | ||
foreign import ccall unsafe "_hs_text_reverse" c_reverse | ||
:: Exts.MutableByteArray# s -> ByteArray# -> CSize -> CSize -> IO () | ||
#endif | ||
|
||
-- $setup | ||
-- >>> :set -XOverloadedStrings | ||
-- >>> import qualified Data.Text.Internal.Reverse as T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters