diff options
author | Ian Lynagh <igloo@earth.li> | 2012-07-15 18:53:04 +0100 |
---|---|---|
committer | Ian Lynagh <igloo@earth.li> | 2012-07-15 18:53:04 +0100 |
commit | 68ee44b8f2548a022bcbab25dd586b987a8bb6a5 (patch) | |
tree | 863daa924ba30753c9d3b62cd6ff48eb665f9845 /compiler/utils | |
parent | 28d13243252f8e8490fe0f3b92fa4a7a4ab917fe (diff) | |
download | haskell-68ee44b8f2548a022bcbab25dd586b987a8bb6a5.tar.gz |
Small refactoring for FastZStrings
Diffstat (limited to 'compiler/utils')
-rw-r--r-- | compiler/utils/FastString.lhs | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/compiler/utils/FastString.lhs b/compiler/utils/FastString.lhs index ba078401e7..e6b432dbfa 100644 --- a/compiler/utils/FastString.lhs +++ b/compiler/utils/FastString.lhs @@ -33,6 +33,7 @@ module FastString fastStringToFastBytes, fastZStringToFastBytes, mkFastBytesByteList, + unsafeMkFastBytesString, bytesFB, hashFB, lengthFB, @@ -179,6 +180,24 @@ mkFastBytesByteList bs = pokeArray (castPtr ptr) bs return $ foreignPtrToFastBytes buf l +-- This will drop information if any character > '\xFF' +unsafeMkFastBytesString :: String -> FastBytes +unsafeMkFastBytesString str = + inlinePerformIO $ do + let l = Prelude.length str + buf <- mallocForeignPtrBytes l + withForeignPtr buf $ \ptr -> do + pokeCAString (castPtr ptr) str + return $ foreignPtrToFastBytes buf l + +pokeCAString :: Ptr CChar -> String -> IO () +pokeCAString ptr str = + let + go [] !_ = return () + go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1) + in + go str 0 + -- | Gives the UTF-8 encoded bytes corresponding to a 'FastString' bytesFB :: FastBytes -> [Word8] bytesFB (FastBytes n_bytes buf) = @@ -226,6 +245,9 @@ zString (FastZString (FastBytes n_bytes buf)) = lengthFZS :: FastZString -> Int lengthFZS (FastZString fb) = lengthFB fb +mkFastZStringString :: String -> FastZString +mkFastZStringString str = FastZString (unsafeMkFastBytesString str) + -- ----------------------------------------------------------------------------- {-| @@ -395,8 +417,7 @@ mkFastStringByteList str = -- | Creates a Z-encoded 'FastString' from a 'String' mkZFastString :: String -> FastZString -mkZFastString str = FastZString - $ mkFastBytesByteList $ map (fromIntegral . ord) str +mkZFastString = mkFastZStringString bucket_match :: [FastString] -> Int -> Ptr Word8 -> IO (Maybe FastString) bucket_match [] _ _ = return Nothing |