fixes #23663; Add hash() for Path (#23664)

fixes #23663
This commit is contained in:
ringabout
2024-05-31 17:07:48 +08:00
committed by GitHub
parent c1f31cedbb
commit cdfc886f88
2 changed files with 17 additions and 6 deletions

View File

@@ -9,7 +9,7 @@ export osseps
import std/envvars
import std/private/osappdirs
import std/pathnorm
import std/[pathnorm, hashes, sugar, strutils]
from std/private/ospaths2 import joinPath, splitPath,
ReadDirEffect, WriteDirEffect,
@@ -25,6 +25,13 @@ export ReadDirEffect, WriteDirEffect
type
Path* = distinct string
func hash*(x: Path): Hash =
let x = x.string.dup(normalizePath)
if FileSystemCaseSensitive:
result = x.hash
else:
result = x.toLowerAscii.hash
template `$`*(x: Path): string =
string(x)

View File

@@ -6,15 +6,12 @@ import std/paths
import std/assertions
import pathnorm
from std/private/ospaths2 {.all.} import joinPathImpl
import std/sugar
import std/[sugar, sets]
proc normalizePath*(path: Path; dirSep = DirSep): Path =
result = Path(pathnorm.normalizePath(path.string, dirSep))
func `==`(x, y: Path): bool =
x.string == y.string
func joinPath*(parts: varargs[Path]): Path =
var estimatedLen = 0
var state = 0
@@ -231,4 +228,11 @@ block ospaths:
when doslikeFileSystem:
doAssert joinPath(Path"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools\\", Path"..\\..\\VC\\vcvarsall.bat") == r"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat".Path
doAssert joinPath(Path"C:\\foo", Path"..\\a") == r"C:\a".Path
doAssert joinPath(Path"C:\\foo\\", Path"..\\a") == r"C:\a".Path
doAssert joinPath(Path"C:\\foo\\", Path"..\\a") == r"C:\a".Path
block: # bug #23663
var s: HashSet[Path]
s.incl("/a/b/c/..".Path)
doAssert "/a/b/".Path in s
doAssert "/a/b/c".Path notin s