Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes and refactoring of normalise #23

Merged
merged 3 commits into from
Oct 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions System/FilePath/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ splitPath x = [drive | drive /= ""] ++ f path
-- > Windows: splitDirectories "C:\\test\\file" == ["C:\\", "test", "file"]
-- > Valid x => joinPath (splitDirectories x) `equalFilePath` x
-- > splitDirectories "" == []
-- > Windows: splitDirectories "C:\\test\\\\\\file" == ["C:\\", "test", "file"]
-- > splitDirectories "/test///file" == ["/","test","file"]
splitDirectories :: FilePath -> [FilePath]
splitDirectories path =
if hasDrive path then head pathComponents : f (tail pathComponents)
Expand Down Expand Up @@ -749,15 +751,18 @@ makeRelative root path
-- > Windows: normalise "c:\\file/bob\\" == "C:\\file\\bob\\"
-- > Windows: normalise "c:\\" == "C:\\"
-- > Windows: normalise "\\\\server\\test" == "\\\\server\\test"
-- > Windows: normalise "//server/test" == "\\\\server\\test"
-- > Windows: normalise "c:/file" == "C:\\file"
-- > Windows: normalise "/file" == "\\file"
-- > Windows: normalise "\\" == "\\"
-- > normalise "." == "."
-- > Posix: normalise "./" == "./"
-- > Posix: normalise "./." == "./"
-- > Posix: normalise "/" == "/"
-- > Posix: normalise "bob/fred/." == "bob/fred/"
normalise :: FilePath -> FilePath
normalise path = joinDrive' (normaliseDrive drv) (f pth)
++ [pathSeparator | isDirPath pth]
++ [pathSeparator | isDirPath pth && length pth > 1]
where
(drv,pth) = splitDrive path

Expand All @@ -767,13 +772,10 @@ normalise path = joinDrive' (normaliseDrive drv) (f pth)
isDirPath xs = hasTrailingPathSeparator xs
|| not (null xs) && last xs == '.' && hasTrailingPathSeparator (init xs)

f = joinPath . dropDots . splitDirectories . propSep
f = joinPath . dropDots . propSep . splitDirectories

propSep (a:b:xs)
| isPathSeparator a && isPathSeparator b = propSep (a:xs)
propSep (a:xs)
| isPathSeparator a = pathSeparator : propSep xs
propSep (x:xs) = x : propSep xs
propSep (x:xs) | all isPathSeparator x = [pathSeparator] : xs
| otherwise = x : xs
propSep [] = []

dropDots = filter ("." /=)
Expand All @@ -782,7 +784,7 @@ normaliseDrive :: FilePath -> FilePath
normaliseDrive drive | isPosix = drive
normaliseDrive drive = if isJust $ readDriveLetter x2
then map toUpper x2
else drive
else x2
where
x2 = map repSlash drive

Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
* Bug fix: on Windows, `dropTrailingPathSeparator "/"` now returns `"/"`
unchanged, instead of the normalised `"\\"`.

* Bug fix: on Windows, `normalise "\\"` now retuns `"\\"` unchanged,
instead of `"\\\\"`.

* Bug fix: on Windows, `normalise "//server/test"` now retuns
`"\\\\server\\test"`, instead of `"//server/test"` unchanged.

## 1.3.0.2 *Mar 2014*

* Bundled with GHC 7.8.1
Expand Down