Skip to content

Commit aaebc0e

Browse files
committed
FoundationEssentials: strip leading /
When creating a file system representation from the path, we need to strip the leading `/` which is added for the RFC representation of the Windows path. Without this, the C runtime will fail with an invalid argument error. Unfortunately this regresses the test suite by causing a hang.
1 parent 6061198 commit aaebc0e

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Sources/FoundationEssentials/String/String+Internals.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,17 @@ extension String {
138138
return try block(buffer.baseAddress!)
139139
}
140140
#else
141-
try self.withCString {
141+
var iter = self.utf8.makeIterator()
142+
#if os(Windows)
143+
let bLeadingSlash = if iter.next() == ._slash, iter.next()?.isLetter ?? false, iter.next() == ._colon { true } else { false }
144+
#else
145+
let bLeadingSlash = false
146+
#endif
147+
148+
// Strip the leading `/` on a RFC8089 path (`/[drive-letter]:/...` ). A
149+
// leading slash indicates a rooted path on the drive for the current
150+
// working directory.
151+
return try Substring(self.utf8.dropFirst(bLeadingSlash ? 1 : 0)).withCString {
142152
try block($0)
143153
}
144154
#endif
@@ -153,7 +163,16 @@ extension String {
153163
return try block(buffer.baseAddress!)
154164
}
155165
#else
156-
var mut = self
166+
var iter = self.utf8.makeIterator()
167+
#if os(Windows)
168+
let bLeadingSlash = if iter.next() == ._slash, iter.next()?.isLetter ?? false, iter.next() == ._colon { true } else { false }
169+
#else
170+
let bLeadingSlash = false
171+
#endif
172+
173+
var mut: Substring =
174+
Substring(self.utf8[self.utf8.index(self.utf8.startIndex, offsetBy: bLeadingSlash ? 1 : 0)...])
175+
157176
return try mut.withUTF8 { utf8Buffer in
158177
// Leave space for a null byte at the end
159178
try withUnsafeTemporaryAllocation(of: CChar.self, capacity: utf8Buffer.count + 1) { temporaryBuffer in

0 commit comments

Comments
 (0)