Implement String -> ByteString and vice versa conversion for filepaths

This commit is contained in:
Julian Ospald 2016-04-05 00:55:25 +02:00
parent 877d8c4089
commit 148eeb619f
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
2 changed files with 16 additions and 0 deletions

View File

@ -18,6 +18,7 @@ library
exposed-modules: HPath, HPath.Foreign, HPath.Internal
build-depends: base >= 4 && <5
, bytestring
, encoding
, exceptions
, deepseq
, word8

View File

@ -63,6 +63,7 @@ module HPath
,dropTrailingPathSeparator
,dropWhileEnd
,equalFilePath
,fpToString
,joinPath
,normalise
,splitDirectories
@ -70,6 +71,7 @@ module HPath
,splitPath
,stripPrefix
,takeDirectory
,userStringToFP
-- * Queries
,hasDot
,hasDoublePS
@ -91,6 +93,8 @@ import Control.Monad.Catch (MonadThrow(..))
import Data.ByteString(ByteString)
import qualified Data.ByteString as B
import Data.Data
import Data.Encoding(decodeStrictByteString, encodeStrictByteString)
import Data.Encoding.UTF8(UTF8(..))
import qualified Data.List as L
import Data.Maybe
import Data.Word8
@ -441,6 +445,17 @@ stripPrefix :: ByteString -> ByteString -> Maybe ByteString
stripPrefix a b = B.pack `fmap` L.stripPrefix (B.unpack a) (B.unpack b)
-- |Uses UTF-8 decoding to convert the bytestring into a String.
fpToString :: ByteString -> String
fpToString = decodeStrictByteString UTF8
-- |Uses UTF-8 encoding to convert a user provides String into
-- a ByteString, which represents a filepath.
userStringToFP :: String -> ByteString
userStringToFP = encodeStrictByteString UTF8
--------------------------------------------------------------------------------
-- ByteString Query functions