strict bytestring encoders and decoders

Only had lazy ones before.

Already sped up a few parts of the code.
This commit is contained in:
Joey Hess 2019-01-01 14:54:06 -04:00
parent 9cc6d5549b
commit b3c69eaaf8
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
14 changed files with 41 additions and 25 deletions

View file

@ -22,7 +22,6 @@ import qualified Data.Aeson
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString as S
import qualified Data.Set
import qualified Data.Vector
import Prelude
@ -53,7 +52,7 @@ instance ToJSON' String where
-- Note that if the string contains invalid UTF8 characters not using
-- the FileSystemEncoding, this is the same as Data.Text.pack.
packString :: String -> T.Text
packString s = case T.decodeUtf8' (S.concat $ L.toChunks $ encodeBS s) of
packString s = case T.decodeUtf8' (encodeBS s) of
Right t -> t
Left _ -> T.pack s

View file

@ -19,10 +19,10 @@ import Data.ByteString.UTF8 (fromString, toString)
import Data.Char
toB64 :: String -> String
toB64 = toString . B64.encode . L.toStrict . encodeBS
toB64 = toString . B64.encode . encodeBS
fromB64Maybe :: String -> Maybe String
fromB64Maybe s = either (const Nothing) (Just . decodeBS . L.fromStrict)
fromB64Maybe s = either (const Nothing) (Just . decodeBL . L.fromStrict)
(B64.decode $ fromString s)
fromB64 :: String -> String

View file

@ -15,7 +15,9 @@ module Utility.FileSystemEncoding (
RawFilePath,
fromRawFilePath,
toRawFilePath,
decodeBL,
decodeBS,
encodeBL,
encodeBS,
decodeW8,
encodeW8,
@ -38,6 +40,7 @@ import Data.List
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
#ifdef mingw32_HOST_OS
import qualified Data.ByteString.UTF8 as S8
import qualified Data.ByteString.Lazy.UTF8 as L8
#endif
@ -107,21 +110,35 @@ _encodeFilePath fp = unsafePerformIO $ do
`catchNonAsync` (\_ -> return fp)
{- Decodes a ByteString into a FilePath, applying the filesystem encoding. -}
decodeBS :: L.ByteString -> FilePath
decodeBL :: L.ByteString -> FilePath
#ifndef mingw32_HOST_OS
decodeBS = encodeW8NUL . L.unpack
decodeBL = encodeW8NUL . L.unpack
#else
{- On Windows, we assume that the ByteString is utf-8, since Windows
- only uses unicode for filenames. -}
decodeBS = L8.toString
decodeBL = L8.toString
#endif
decodeBS :: S.ByteString -> FilePath
#ifndef mingw32_HOST_OS
decodeBS = encodeW8NUL . S.unpack
#else
decodeBS = S8.toString
#endif
{- Encodes a FilePath into a ByteString, applying the filesystem encoding. -}
encodeBS :: FilePath -> L.ByteString
encodeBL :: FilePath -> L.ByteString
#ifndef mingw32_HOST_OS
encodeBS = L.pack . decodeW8NUL
encodeBL = L.pack . decodeW8NUL
#else
encodeBS = L8.fromString
encodeBL = L8.fromString
#endif
encodeBS :: FilePath -> S.ByteString
#ifndef mingw32_HOST_OS
encodeBS = S.pack . decodeW8NUL
#else
encodeBS = S8.fromString
#endif
{- Recent versions of the unix package have this alias; defined here

View file

@ -102,7 +102,7 @@ sideLockFile lockfile = do
let shortbase = reverse $ take 32 $ reverse base
let md5sum = if base == shortbase
then ""
else show (md5 (encodeBS base))
else show (md5 (encodeBL base))
dir <- ifM (doesDirectoryExist "/dev/shm")
( return "/dev/shm"
, return "/tmp"