| 1 |
{-# LANGUAGE OverloadedStrings, BangPatterns #-} |
| 2 |
|
| 3 |
module Network.Wai.Application.Classic.Path ( |
| 4 |
Path |
| 5 |
, pathString |
| 6 |
, fromString |
| 7 |
, (</>), (<\>), (<.>) |
| 8 |
, breakAtSeparator, hasLeadingPathSeparator, hasTrailingPathSeparator |
| 9 |
, isSuffixOf |
| 10 |
) where |
| 11 |
|
| 12 |
import Data.ByteString (ByteString) |
| 13 |
import qualified Data.ByteString as BS |
| 14 |
import qualified Data.ByteString.Char8 as B8 |
| 15 |
import Data.String |
| 16 |
import Data.Word |
| 17 |
|
| 18 |
-- $setup |
| 19 |
-- >>> :set -XOverloadedStrings |
| 20 |
|
| 21 |
---------------------------------------------------------------- |
| 22 |
|
| 23 |
-- | File path. |
| 24 |
type Path = ByteString |
| 25 |
|
| 26 |
pathString :: Path -> String |
| 27 |
pathString = B8.unpack |
| 28 |
{-# INLINE pathString #-} |
| 29 |
|
| 30 |
---------------------------------------------------------------- |
| 31 |
|
| 32 |
-- pathDot :: Word8 |
| 33 |
-- pathDot = 46 |
| 34 |
|
| 35 |
pathDotBS :: ByteString |
| 36 |
pathDotBS = "." |
| 37 |
|
| 38 |
pathSep :: Word8 |
| 39 |
pathSep = 47 |
| 40 |
|
| 41 |
pathSepBS :: ByteString |
| 42 |
pathSepBS = "/" |
| 43 |
|
| 44 |
{-| |
| 45 |
Checking if the path ends with the path separator. |
| 46 |
|
| 47 |
>>> hasLeadingPathSeparator "/foo/bar" |
| 48 |
True |
| 49 |
>>> hasLeadingPathSeparator "foo/bar" |
| 50 |
False |
| 51 |
-} |
| 52 |
hasLeadingPathSeparator :: Path -> Bool |
| 53 |
hasLeadingPathSeparator bs |
| 54 |
| BS.null bs = False |
| 55 |
| BS.head bs == pathSep = True |
| 56 |
| otherwise = False |
| 57 |
{-# INLINE hasLeadingPathSeparator #-} |
| 58 |
|
| 59 |
{-| |
| 60 |
Checking if the path ends with the path separator. |
| 61 |
|
| 62 |
>>> hasTrailingPathSeparator "/foo/bar/" |
| 63 |
True |
| 64 |
>>> hasTrailingPathSeparator "/foo/bar" |
| 65 |
False |
| 66 |
-} |
| 67 |
hasTrailingPathSeparator :: Path -> Bool |
| 68 |
hasTrailingPathSeparator bs |
| 69 |
| BS.null bs = False |
| 70 |
| BS.last bs == pathSep = True |
| 71 |
| otherwise = False |
| 72 |
{-# INLINE hasTrailingPathSeparator #-} |
| 73 |
|
| 74 |
{-| |
| 75 |
Appending with the file separator. |
| 76 |
|
| 77 |
>>> "/foo" </> "bar" |
| 78 |
"/foo/bar" |
| 79 |
>>> "/foo/" </> "bar" |
| 80 |
"/foo/bar" |
| 81 |
>>> "/foo" </> "/bar" |
| 82 |
"/foo/bar" |
| 83 |
>>> "/foo/" </> "/bar" |
| 84 |
"/foo/bar" |
| 85 |
-} |
| 86 |
|
| 87 |
(</>) :: Path -> Path -> Path |
| 88 |
p1 </> p2 = p |
| 89 |
where |
| 90 |
!has1 = hasTrailingPathSeparator p1 |
| 91 |
!has2 = hasLeadingPathSeparator p2 |
| 92 |
!p | has1 && not has2 = p1 `BS.append` p2 |
| 93 |
| not has1 && has2 = p1 `BS.append` p2 |
| 94 |
| has1 = p1 `BS.append` BS.tail p2 |
| 95 |
| otherwise = BS.concat [p1,pathSepBS,p2] |
| 96 |
{-# INLINE (</>) #-} |
| 97 |
|
| 98 |
{-| |
| 99 |
Removing prefix. The prefix of the second argument is removed |
| 100 |
from the first argument. |
| 101 |
|
| 102 |
>>> "foobar" <\> "foo" |
| 103 |
"bar" |
| 104 |
>>> "foo" <\> "foobar" |
| 105 |
"" |
| 106 |
>>> "foobar" <\> "baz" |
| 107 |
"bar" |
| 108 |
-} |
| 109 |
(<\>) :: Path -> Path -> Path |
| 110 |
p1 <\> p2 = p |
| 111 |
where |
| 112 |
!p = BS.drop (BS.length p2) p1 |
| 113 |
{-# INLINE (<\>) #-} |
| 114 |
|
| 115 |
{-| |
| 116 |
Adding suffix. |
| 117 |
-} |
| 118 |
(<.>) :: Path -> Path -> Path |
| 119 |
p1 <.> p2 = p |
| 120 |
where |
| 121 |
!p = BS.concat [p1,pathDotBS,p2] |
| 122 |
{-# INLINE (<.>) #-} |
| 123 |
|
| 124 |
{-| |
| 125 |
Breaking at the first path separator. |
| 126 |
|
| 127 |
>>> breakAtSeparator "/foo/bar/baz" |
| 128 |
("","/foo/bar/baz") |
| 129 |
>>> breakAtSeparator "foo/bar/baz" |
| 130 |
("foo","/bar/baz") |
| 131 |
>>> breakAtSeparator "foo" |
| 132 |
("foo","") |
| 133 |
-} |
| 134 |
breakAtSeparator :: Path -> (Path,Path) |
| 135 |
breakAtSeparator p = BS.break (== pathSep) p |
| 136 |
{-# INLINE breakAtSeparator #-} |
| 137 |
|
| 138 |
isSuffixOf :: Path -> Path -> Bool |
| 139 |
isSuffixOf = BS.isSuffixOf |
| 140 |
{-# INLINE isSuffixOf #-} |