1{-# LANGUAGE CPP, DeriveDataTypeable, UnboxedTuples, MagicHash,
2 BangPatterns, ForeignFunctionInterface, JavaScriptFFI, GeneralizedNewtypeDeriving #-}
3{-# LANGUAGE ForeignFunctionInterface, UnliftedFFITypes,
4 GHCForeignImportPrim, MagicHash, FlexibleInstances #-}
5module Data.Text.Internal
6 ( module Data.Text.Internal
11import Data.String (IsString(..))
19import qualified Data.JSString as JSS
20import qualified Data.JSString.Internal as JSSS
21import qualified Data.JSString.Internal.Type as JSS
22import Data.JSString hiding (empty)
23import qualified Data.Text.Array as A
26import Data.Int (Int32, Int64)
27-- import Data.Text.Internal.Unsafe.Char (ord)
28import Data.Typeable (Typeable)
29import GHC.Exts (ByteArray#, Char(..), ord#, andI#, (/=#), isTrue#, (==#), Int(..), Int#, isTrue#)
30-- import qualified Data.Text.Array as A
31import GHCJS.Marshal.Pure
33import qualified GHC.CString as GHC
36newtype Text = Text JSString
37 deriving (PToJSVal, PFromJSVal, FromJSVal, ToJSVal)
39instance Show Text where
40 showsPrec p (Text jsstring) r = showsPrec p jsstring r
44{-# INLINE [1] empty #-}
47empty_ = Text JSS.empty
48{-# NOINLINE empty_ #-}
50foreign import javascript unsafe
51 "String.fromCharCode.apply(null, $1['u8'])"
52 js_decodeBytes :: ByteArray# -> JSString
54foreign import javascript unsafe
56 js_textToString :: ByteArray# -> Int# -> Int# -> JSString
58-- | Construct a 'Text' without invisibly pinning its byte array in
59-- memory if its length has dwindled to zero.
60text :: A.Array -> Int -> Int -> Text
61-- text arr off len | len == 0 = empty
62-- | otherwise = text_ arr off len
63text (A.Array ba) (I# off#) (I# len#) | isTrue# (len# ==# 0#) = empty
64 | otherwise = Text $ js_textToString ba off# len#
66-- | Checked multiplication. Calls 'error' if the result would
68mul :: Int -> Int -> Int
69#if WORD_SIZE_IN_BITS == 64
70mul a b = fromIntegral $ fromIntegral a `mul64` fromIntegral b
72mul a b = fromIntegral $ fromIntegral a `mul32` fromIntegral b
77-- | Checked multiplication. Calls 'error' if the result would
79mul64 :: Int64 -> Int64 -> Int64
81 | a >= 0 && b >= 0 = mul64_ a b
82 | a >= 0 = -mul64_ a (-b)
83 | b >= 0 = -mul64_ (-a) b
84 | otherwise = mul64_ (-a) (-b)
88mul64_ :: Int64 -> Int64 -> Int64
90 | ahi > 0 && bhi > 0 = error "overflow"
91 | top > 0x7fffffff = error "overflow"
92 | total < 0 = error "overflow"
94 where (# ahi, alo #) = (# a `shiftR` 32, a .&. 0xffffffff #)
95 (# bhi, blo #) = (# b `shiftR` 32, b .&. 0xffffffff #)
96 top = ahi * blo + alo * bhi
97 total = (top `shiftL` 32) + alo * blo
100-- | Checked multiplication. Calls 'error' if the result would
102mul32 :: Int32 -> Int32 -> Int32
103mul32 a b = case fromIntegral a * fromIntegral b of
104 ab | ab < min32 || ab > max32 -> error "overflow"
105 | otherwise -> fromIntegral ab
106 where min32 = -0x80000000 :: Int64
111--------------------------------------
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list