Skip to content

Commit

Permalink
Minor refactorings & use of unsafeChr
Browse files Browse the repository at this point in the history
  • Loading branch information
hvr committed Jan 27, 2018
1 parent f0708ba commit 8db0211
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
5 changes: 3 additions & 2 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
packages: .

tests: True
flags: +asserts

package text-short
flags: +asserts
29 changes: 19 additions & 10 deletions src/Data/Text/Short/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ module Data.Text.Short.Internal
import Control.DeepSeq (NFData)
import Control.Monad.ST (stToIO)
import Data.Binary
import Data.Bits (shiftR, (.&.), (.|.))
import Data.Bits
import qualified Data.ByteString as BS
import qualified Data.ByteString.Builder as BB
import Data.ByteString.Short (ShortByteString)
import qualified Data.ByteString.Short as BSS
import qualified Data.ByteString.Short.Internal as BSSI
import Data.Char (chr, ord)
import Data.Char (ord)
import Data.Hashable (Hashable)
import qualified Data.List as List
import Data.Maybe (fromMaybe, isNothing)
Expand All @@ -109,6 +109,7 @@ import qualified Data.String as S
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Foreign.C
import GHC.Base (assert, unsafeChr)
import qualified GHC.CString as GHC
import GHC.Exts (Addr#, ByteArray#, Int (I#),
Int#, MutableByteArray#,
Expand Down Expand Up @@ -572,9 +573,8 @@ foreign import ccall unsafe "hs_text_short_index_cp" c_text_short_index :: ByteA
-- @since 0.1.2
indexMaybe :: ShortText -> Int -> Maybe Char
indexMaybe st i
| i < 0 = Nothing
| unCP cp < 0x110000 = Just (cp2ch cp)
| otherwise = Nothing
| i < 0 = Nothing
| otherwise = cp2chSafe cp
where
cp = CP $ fromIntegral $
unsafeDupablePerformIO (c_text_short_index (toByteArray# st) (toCSize st) (fromIntegral i))
Expand All @@ -592,9 +592,8 @@ indexMaybe st i
-- @since 0.1.2
indexEndMaybe :: ShortText -> Int -> Maybe Char
indexEndMaybe st i
| i < 0 = Nothing
| unCP cp < 0x110000 = Just (cp2ch cp)
| otherwise = Nothing
| i < 0 = Nothing
| otherwise = cp2chSafe cp
where
cp = CP $ fromIntegral $
unsafeDupablePerformIO (c_text_short_index_rev (toByteArray# st) (toCSize st) (fromIntegral i))
Expand Down Expand Up @@ -1114,13 +1113,23 @@ copyByteArray2 (MBA# src#) (B (I# src_off#)) (MBA# dst#) (B (I# dst_off#)) (B( I
-- | Unicode Code-point
--
-- Keeping it as a 'Word' is more convenient for bit-ops and FFI
newtype CP = CP { unCP :: Word }
newtype CP = CP Word

ch2cp :: Char -> CP
ch2cp = CP . fromIntegral . ord

{-# INLINE cp2ch #-}
cp2ch :: CP -> Char
cp2ch = chr . fromIntegral . unCP
cp2ch (CP w) = (w < 0x110000) `assert` unsafeChr (fromIntegral w)

-- used/needed by index-lookup functions to encode out of bounds
cp2chSafe :: CP -> Maybe Char
cp2chSafe cp
| cpNull cp = Nothing
| otherwise = Just $! cp2ch cp
where
cpNull :: CP -> Bool
cpNull (CP w) = w >= 0x110000

{-# INLINE cpLen #-}
cpLen :: CP -> B
Expand Down

0 comments on commit 8db0211

Please sign in to comment.