-
Notifications
You must be signed in to change notification settings - Fork 162
Isolate reverse in preparation for a pure-Haskell implementation #536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.