@@ -449,18 +449,20 @@ private struct UNIXPath: Path {
449
449
450
450
var dirname : String {
451
451
#if os(Windows)
452
- guard string != " " else {
453
- return " . "
454
- }
455
452
let fsr : UnsafePointer < Int8 > = string. fileSystemRepresentation
456
453
defer { fsr. deallocate ( ) }
457
454
458
455
let path : String = String ( cString: fsr)
459
- return path. withCString ( encodedAs: UTF16 . self) {
460
- let data = UnsafeMutaßlePointer ( mutating: $0)
456
+ let dir : String = path. withCString ( encodedAs: UTF16 . self) {
457
+ let data = UnsafeMutablePointer ( mutating: $0)
461
458
PathCchRemoveFileSpec ( data, path. count)
462
459
return String ( decodingCString: data, as: UTF16 . self)
463
460
}
461
+ // These two expressions represent for the current directory.
462
+ if dir == " \\ " || dir == " " {
463
+ return " . "
464
+ }
465
+ return dir
464
466
#else
465
467
// FIXME: This method seems too complicated; it should be simplified,
466
468
// if possible, and certainly optimized (using UTF8View).
@@ -547,11 +549,13 @@ private struct UNIXPath: Path {
547
549
548
550
init ( normalizingAbsolutePath path: String ) {
549
551
#if os(Windows)
550
- var buffer : [ WCHAR ] = Array < WCHAR > ( repeating: 0 , count: Int ( MAX_PATH + 1 ) )
552
+ var result : PWSTR ?
553
+ defer { LocalFree ( result) }
554
+
551
555
_ = path. withCString ( encodedAs: UTF16 . self) {
552
- PathCanonicalizeW ( & buffer , $0 )
556
+ PathAllocCanonicalize ( $0 , ULONG ( PATHCCH_ALLOW_LONG_PATHS . rawValue ) , & result )
553
557
}
554
- self . init ( string: String ( decodingCString: buffer , as: UTF16 . self) )
558
+ self . init ( string: String ( decodingCString: result! , as: UTF16 . self) )
555
559
#else
556
560
precondition ( path. first == " / " , " Failure normalizing \( path) , absolute paths should start with '/' " )
557
561
@@ -617,11 +621,18 @@ private struct UNIXPath: Path {
617
621
618
622
init ( normalizingRelativePath path: String ) {
619
623
#if os(Windows)
620
- var buffer : [ WCHAR ] = Array < WCHAR > ( repeating: 0 , count: Int ( MAX_PATH + 1 ) )
624
+ var result : PWSTR ?
625
+ defer { LocalFree ( result) }
626
+
621
627
_ = path. replacingOccurrences ( of: " / " , with: " \\ " ) . withCString ( encodedAs: UTF16 . self) {
622
- PathCanonicalizeW ( & buffer, $0)
628
+ PathAllocCanonicalize ( $0, ULONG ( PATHCCH_ALLOW_LONG_PATHS . rawValue) , & result)
629
+ }
630
+
631
+ var canonicalized : String = String ( decodingCString: result!, as: UTF16 . self)
632
+ if canonicalized == " " || canonicalized == " \\ " {
633
+ canonicalized = " . "
623
634
}
624
- self . init ( string: String ( decodingCString : buffer , as : UTF16 . self ) )
635
+ self . init ( string: canonicalized )
625
636
#else
626
637
precondition ( path. first != " / " )
627
638
@@ -715,6 +726,7 @@ private struct UNIXPath: Path {
715
726
#if os(Windows)
716
727
guard path != " " else {
717
728
self . init ( normalizingRelativePath: path)
729
+ return
718
730
}
719
731
let fsr : UnsafePointer < Int8 > = path. fileSystemRepresentation
720
732
defer { fsr. deallocate ( ) }
0 commit comments