Skip to content

Commit 287439e

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 f657181 commit 287439e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Sources/FoundationEssentials/String/String+Internals.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ extension String {
138138
return try block(buffer.baseAddress!)
139139
}
140140
#else
141-
try self.withCString {
141+
var iter = self.utf8.makeIterator()
142+
let bLeadingSlash = if iter.next() == ._slash, iter.next()?.isLetter ?? false, iter.next() == ._colon { true } else { false }
143+
144+
// Strip the leading `/` on a RFC8089 path (`/[drive-letter]:/...` ). A
145+
// leading slash indicates a rooted path on the drive for the current
146+
// working directory.
147+
return try Substring(self.utf8.dropFirst(bLeadingSlash ? 1 : 0)).withCString {
142148
try block($0)
143149
}
144150
#endif
@@ -153,7 +159,12 @@ extension String {
153159
return try block(buffer.baseAddress!)
154160
}
155161
#else
156-
var mut = self
162+
var iter = self.utf8.makeIterator()
163+
let bLeadingSlash = if iter.next() == ._slash, iter.next()?.isLetter ?? false, iter.next() == ._colon { true } else { false }
164+
165+
var mut: Substring =
166+
Substring(self.utf8[self.utf8.index(self.utf8.startIndex, offsetBy: bLeadingSlash ? 1 : 0)...])
167+
157168
return try mut.withUTF8 { utf8Buffer in
158169
// Leave space for a null byte at the end
159170
try withUnsafeTemporaryAllocation(of: CChar.self, capacity: utf8Buffer.count + 1) { temporaryBuffer in

0 commit comments

Comments
 (0)