77 See http://swift.org/LICENSE.txt for license information
88 See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99*/
10+ #if os(Windows)
11+ import Foundation
12+ #endif
1013
1114/// Represents an absolute file system path, independently of what (or whether
1215/// anything at all) exists at that path in the file system at any given time.
@@ -370,6 +373,10 @@ private struct PathImpl: Hashable {
370373 /// string consisting of just `.` if there is no directory part (which is
371374 /// the case if and only if there is no path separator).
372375 fileprivate var dirname : String {
376+ #if os(Windows)
377+ let dir = string. deletingLastPathComponent
378+ return dir == " " ? " . " : dir
379+ #else
373380 // FIXME: This method seems too complicated; it should be simplified,
374381 // if possible, and certainly optimized (using UTF8View).
375382 // Find the last path separator.
@@ -385,6 +392,7 @@ private struct PathImpl: Hashable {
385392 // Otherwise, it's the string up to (but not including) the last path
386393 // separator.
387394 return String ( string. prefix ( upTo: idx) )
395+ #endif
388396 }
389397
390398 fileprivate var basename : String {
@@ -561,11 +569,13 @@ private func mayNeedNormalization(absolute string: String) -> Bool {
561569///
562570/// The normalization rules are as described for the AbsolutePath struct.
563571private func normalize( absolute string: String ) -> String {
572+ #if os(Windows)
573+ return string. standardizingPath
574+ #else
564575 precondition ( string. first == " / " , " Failure normalizing \( string) , absolute paths should start with '/' " )
565576
566577 // At this point we expect to have a path separator as first character.
567578 assert ( string. first == " / " )
568-
569579 // Fast path.
570580 if !mayNeedNormalization( absolute: string) {
571581 return string
@@ -621,13 +631,17 @@ private func normalize(absolute string: String) -> String {
621631
622632 // Use the result as our stored string.
623633 return result
634+ #endif
624635}
625636
626637/// Private function that normalizes and returns a relative string. Asserts
627638/// that `string` does not start with a path separator.
628639///
629640/// The normalization rules are as described for the AbsolutePath struct.
630641private func normalize( relative string: String ) -> String {
642+ #if os(Windows)
643+ return string. standardizingPath
644+ #else
631645 precondition ( string. first != " / " )
632646
633647 // FIXME: Here we should also keep track of whether anything actually has
@@ -688,4 +702,5 @@ private func normalize(relative string: String) -> String {
688702
689703 // If the result is empty, return `.`, otherwise we return it as a string.
690704 return result. isEmpty ? " . " : result
705+ #endif
691706}
0 commit comments