X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Futils%2FFastString.lhs;h=ca7c2c71af80f0a4d8ca05094762de99262fbca6;hb=1a7d1b77334529ca96ed4cbc03fcb5f55dc2de4a;hp=ea307799c424f6f164fbf1074f49a6c819e14750;hpb=0065d5ab628975892cea1ec7303f968c3338cbe1;p=ghc-hetmet.git diff --git a/compiler/utils/FastString.lhs b/compiler/utils/FastString.lhs index ea30779..ca7c2c7 100644 --- a/compiler/utils/FastString.lhs +++ b/compiler/utils/FastString.lhs @@ -2,6 +2,13 @@ % (c) The University of Glasgow, 1997-2006 % \begin{code} +{-# OPTIONS -w #-} +-- The above warning supression flag is a temporary kludge. +-- While working on this module you are encouraged to remove it and fix +-- any warnings in the module. See +-- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings +-- for details + {- FastString: A compact, hash-consed, representation of character strings. Comparison is O(1), and you can get a Unique from them. @@ -24,6 +31,7 @@ module FastString -- ** Construction mkFastString, mkFastStringBytes, + mkFastStringByteList, mkFastStringForeignPtr, mkFastString#, mkZFastString, @@ -77,11 +85,12 @@ import Data.IORef ( IORef, newIORef, readIORef, writeIORef ) import System.IO ( hPutBuf ) import Data.Maybe ( isJust ) -import GHC.Arr ( STArray(..), newSTArray ) +import GHC.ST import GHC.IOBase ( IO(..) ) import GHC.Ptr ( Ptr(..) ) -#define hASH_TBL_SIZE 4091 +#define hASH_TBL_SIZE 4091 +#define hASH_TBL_SIZE_UNBOXED 4091# {-| @@ -128,17 +137,17 @@ instance Show FastString where cmpFS :: FastString -> FastString -> Ordering cmpFS (FastString u1 l1 _ buf1 _) (FastString u2 l2 _ buf2 _) = if u1 == u2 then EQ else - let l = if l1 <= l2 then l1 else l2 in - inlinePerformIO $ - withForeignPtr buf1 $ \p1 -> - withForeignPtr buf2 $ \p2 -> do - res <- memcmp p1 p2 l - case () of - _ | res < 0 -> return LT - | res == 0 -> if l1 == l2 then return EQ - else if l1 < l2 then return LT - else return GT - | otherwise -> return GT + case unsafeMemcmp buf1 buf2 (min l1 l2) `compare` 0 of + LT -> LT + EQ -> compare l1 l2 + GT -> GT + +unsafeMemcmp :: ForeignPtr a -> ForeignPtr b -> Int -> Int +unsafeMemcmp buf1 buf2 l = + inlinePerformIO $ + withForeignPtr buf1 $ \p1 -> + withForeignPtr buf2 $ \p2 -> + memcmp p1 p2 l #ifndef __HADDOCK__ foreign import ccall unsafe "ghc_memcmp" @@ -160,11 +169,14 @@ data FastStringTable = {-# UNPACK #-} !Int (MutableArray# RealWorld [FastString]) +{-# NOINLINE string_table #-} string_table :: IORef FastStringTable string_table = unsafePerformIO $ do - (STArray _ _ arr#) <- stToIO (newSTArray (0::Int,hASH_TBL_SIZE) []) - newIORef (FastStringTable 0 arr#) + tab <- IO $ \s1# -> case newArray# hASH_TBL_SIZE_UNBOXED [] s1# of + (# s2#, arr# #) -> + (# s2#, FastStringTable 0 arr# #) + newIORef tab lookupTbl :: FastStringTable -> Int -> IO [FastString] lookupTbl (FastStringTable _ arr#) (I# i#) = @@ -275,6 +287,15 @@ mkFastString str = utf8EncodeString ptr str mkFastStringForeignPtr ptr buf l +-- | Creates a 'FastString' from a UTF-8 encoded @[Word8]@ +mkFastStringByteList :: [Word8] -> FastString +mkFastStringByteList str = + inlinePerformIO $ do + let l = Prelude.length str + buf <- mallocForeignPtrBytes l + withForeignPtr buf $ \ptr -> do + pokeArray (castPtr ptr) str + mkFastStringForeignPtr ptr buf l -- | Creates a Z-encoded 'FastString' from a 'String' mkZFastString :: String -> FastString @@ -479,21 +500,7 @@ pokeCAString ptr str = in go str 0 -#if __GLASGOW_HASKELL__ < 600 - -mallocForeignPtrBytes :: Int -> IO (ForeignPtr a) -mallocForeignPtrBytes n = do - r <- mallocBytes n - newForeignPtr r (finalizerFree r) - -foreign import ccall unsafe "stdlib.h free" - finalizerFree :: Ptr a -> IO () - -peekCAStringLen = peekCStringLen - -#elif __GLASGOW_HASKELL__ <= 602 - +#if __GLASGOW_HASKELL__ <= 602 peekCAStringLen = peekCStringLen - #endif \end{code}