9
9
10
10
import CoreFoundation
11
11
12
+ #if os(Windows)
13
+ let defaultPathSep = " \\ "
14
+ let validPathSeps : [ Character ] = [ " \\ " , " / " ]
15
+ #else
16
+ let defaultPathSep = " / "
17
+ let validPathSeps : [ Character ] = [ " / " ]
18
+ #endif
19
+
12
20
public func NSTemporaryDirectory( ) -> String {
13
21
#if os(Windows)
14
22
let cchLength : DWORD = GetTempPathW ( 0 , nil )
@@ -51,8 +59,8 @@ public func NSTemporaryDirectory() -> String {
51
59
}
52
60
#endif
53
61
if let tmpdir = ProcessInfo . processInfo. environment [ " TMPDIR " ] {
54
- if !tmpdir. hasSuffix ( " / " ) {
55
- return tmpdir + " / "
62
+ if !validPathSeps . contains ( where : { tmpdir. hasSuffix ( String ( $0 ) ) } ) {
63
+ return tmpdir + defaultPathSep
56
64
} else {
57
65
return tmpdir
58
66
}
@@ -70,15 +78,15 @@ public func NSTemporaryDirectory() -> String {
70
78
extension String {
71
79
72
80
internal var _startOfLastPathComponent : String . Index {
73
- precondition ( !hasSuffix( " / " ) && length > 1 )
81
+ precondition ( !validPathSeps . contains ( where : { hasSuffix ( String ( $0 ) ) } ) && length > 1 )
74
82
75
83
let startPos = startIndex
76
84
var curPos = endIndex
77
85
78
86
// Find the beginning of the component
79
87
while curPos > startPos {
80
88
let prevPos = index ( before: curPos)
81
- if self [ prevPos] == " / " {
89
+ if validPathSeps . contains ( self [ prevPos] ) {
82
90
break
83
91
}
84
92
curPos = prevPos
@@ -88,7 +96,7 @@ extension String {
88
96
}
89
97
90
98
internal var _startOfPathExtension : String . Index ? {
91
- precondition ( !hasSuffix( " / " ) )
99
+ precondition ( !validPathSeps . contains ( where : { hasSuffix ( String ( $0 ) ) } ) )
92
100
93
101
var currentPosition = endIndex
94
102
let startOfLastPathComponent = _startOfLastPathComponent
@@ -97,7 +105,7 @@ extension String {
97
105
while currentPosition > startOfLastPathComponent {
98
106
let previousPosition = index ( before: currentPosition)
99
107
let character = self [ previousPosition]
100
- if character == " / " {
108
+ if validPathSeps . contains ( character) {
101
109
return nil
102
110
} else if character == " . " {
103
111
if startOfLastPathComponent == previousPosition {
@@ -128,10 +136,10 @@ extension String {
128
136
if isEmpty {
129
137
return str
130
138
}
131
- if hasSuffix ( " / " ) {
139
+ if validPathSeps . contains ( where : { hasSuffix ( String ( $0 ) ) } ) {
132
140
return self + str
133
141
}
134
- return self + " / " + str
142
+ return self + defaultPathSep + str
135
143
}
136
144
137
145
internal func _stringByFixingSlashes( compress : Bool = true , stripTrailing: Bool = true ) -> String {
@@ -142,13 +150,13 @@ extension String {
142
150
var curPos = startPos
143
151
144
152
while curPos < endPos {
145
- if result [ curPos] == " / " {
153
+ if validPathSeps . contains ( result [ curPos] ) {
146
154
var afterLastSlashPos = curPos
147
- while afterLastSlashPos < endPos && result [ afterLastSlashPos] == " / " {
155
+ while afterLastSlashPos < endPos && validPathSeps . contains ( result [ afterLastSlashPos] ) {
148
156
afterLastSlashPos = result. index ( after: afterLastSlashPos)
149
157
}
150
158
if afterLastSlashPos != result. index ( after: curPos) {
151
- result. replaceSubrange ( curPos ..< afterLastSlashPos, with: [ " / " ] )
159
+ result. replaceSubrange ( curPos ..< afterLastSlashPos, with: [ Character ( defaultPathSep ) ] )
152
160
endPos = result. endIndex
153
161
}
154
162
curPos = afterLastSlashPos
@@ -157,7 +165,7 @@ extension String {
157
165
}
158
166
}
159
167
}
160
- if stripTrailing && result. length > 1 && result. hasSuffix ( " / " ) {
168
+ if stripTrailing && result. length > 1 && validPathSeps . contains ( where : { result. hasSuffix ( String ( $0 ) ) } ) {
161
169
result. remove ( at: result. index ( before: result. endIndex) )
162
170
}
163
171
return result
@@ -223,7 +231,7 @@ extension NSString {
223
231
224
232
public var deletingLastPathComponent : String {
225
233
let fixedSelf = _stringByFixingSlashes ( )
226
- if fixedSelf == " / " || fixedSelf == " " {
234
+ if validPathSeps . contains ( where : { String ( $0 ) == fixedSelf } ) || fixedSelf == " " {
227
235
return fixedSelf
228
236
}
229
237
@@ -235,7 +243,7 @@ extension NSString {
235
243
236
244
// absolute path, single component
237
245
case fixedSelf. index ( after: fixedSelf. startIndex) :
238
- return " / "
246
+ return defaultPathSep
239
247
240
248
// all common cases
241
249
case let startOfLast:
@@ -244,7 +252,7 @@ extension NSString {
244
252
}
245
253
246
254
internal func _stringByFixingSlashes( compress : Bool = true , stripTrailing: Bool = true ) -> String {
247
- if _swiftObject == " / " {
255
+ if validPathSeps . contains ( where : { String ( $0 ) == _swiftObject } ) {
248
256
return _swiftObject
249
257
}
250
258
@@ -255,13 +263,13 @@ extension NSString {
255
263
var curPos = startPos
256
264
257
265
while curPos < endPos {
258
- if result [ curPos] == " / " {
266
+ if validPathSeps . contains ( result [ curPos] ) {
259
267
var afterLastSlashPos = curPos
260
- while afterLastSlashPos < endPos && result [ afterLastSlashPos] == " / " {
268
+ while afterLastSlashPos < endPos && validPathSeps . contains ( result [ afterLastSlashPos] ) {
261
269
afterLastSlashPos = result. index ( after: afterLastSlashPos)
262
270
}
263
271
if afterLastSlashPos != result. index ( after: curPos) {
264
- result. replaceSubrange ( curPos ..< afterLastSlashPos, with: [ " / " ] )
272
+ result. replaceSubrange ( curPos ..< afterLastSlashPos, with: [ Character ( defaultPathSep ) ] )
265
273
endPos = result. endIndex
266
274
}
267
275
curPos = afterLastSlashPos
@@ -270,7 +278,7 @@ extension NSString {
270
278
}
271
279
}
272
280
}
273
- if stripTrailing && result. hasSuffix ( " / " ) {
281
+ if stripTrailing && validPathSeps . contains ( where : { result. hasSuffix ( String ( $0 ) ) } ) {
274
282
result. remove ( at: result. index ( before: result. endIndex) )
275
283
}
276
284
return result
@@ -310,7 +318,7 @@ extension NSString {
310
318
}
311
319
312
320
public func appendingPathExtension( _ str: String ) -> String ? {
313
- if str. hasPrefix ( " / " ) || self == " " || self == " / " {
321
+ if validPathSeps . contains ( where : { str. hasPrefix ( String ( $0 ) ) } ) || self == " " || validPathSeps . contains ( where : { String ( $0 ) . _nsObject == self } ) {
314
322
print ( " Cannot append extension \( str) to path \( self ) " )
315
323
return nil
316
324
}
@@ -323,7 +331,7 @@ extension NSString {
323
331
return _swiftObject
324
332
}
325
333
326
- let endOfUserName = _swiftObject. firstIndex ( of : " / " ) ?? _swiftObject. endIndex
334
+ let endOfUserName = _swiftObject. firstIndex ( where : { validPathSeps . contains ( $0 ) } ) ?? _swiftObject. endIndex
327
335
let startOfUserName = _swiftObject. index ( after: _swiftObject. startIndex)
328
336
let userName = String ( _swiftObject [ startOfUserName..< endOfUserName] )
329
337
let optUserName : String ? = userName. isEmpty ? nil : userName
@@ -355,12 +363,10 @@ extension NSString {
355
363
}
356
364
357
365
// TODO: pathComponents keeps final path separator if any. Check that logic.
358
- if components . last == " / " && components. count > 1 {
366
+ if validPathSeps . contains ( where : { String ( $0 ) == components . last } ) && components. count > 1 {
359
367
components. removeLast ( )
360
368
}
361
369
362
- let isAbsolutePath = components. first == " / "
363
-
364
370
var resolvedPath = components. removeFirst ( )
365
371
for component in components {
366
372
switch component {
@@ -422,7 +428,7 @@ extension NSString {
422
428
let commonPath = searchAllFilesInDirectory ? path : _ensureLastPathSeparator ( deletingLastPathComponent)
423
429
424
430
if searchAllFilesInDirectory {
425
- outputName = " / "
431
+ outputName = defaultPathSep
426
432
} else {
427
433
if let lcp = _longestCommonPrefix ( matches, caseSensitive: flag) {
428
434
outputName = ( commonPath + lcp)
@@ -435,7 +441,7 @@ extension NSString {
435
441
}
436
442
437
443
internal func _stringIsPathToDirectory( _ path: String ) -> Bool {
438
- if !path. hasSuffix ( " / " ) {
444
+ if !validPathSeps . contains ( where : { path. hasSuffix ( String ( $0 ) ) } ) {
439
445
return false
440
446
}
441
447
@@ -534,11 +540,11 @@ extension NSString {
534
540
}
535
541
536
542
internal func _ensureLastPathSeparator( _ path: String ) -> String {
537
- if path. hasSuffix ( " / " ) || path. isEmpty {
543
+ if validPathSeps . contains ( where : { path. hasSuffix ( String ( $0 ) ) } ) || path. isEmpty {
538
544
return path
539
545
}
540
546
541
- return path + " / "
547
+ return path + defaultPathSep
542
548
}
543
549
544
550
public var fileSystemRepresentation : UnsafePointer < Int8 > {
0 commit comments