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 {
@@ -132,10 +140,10 @@ extension String {
132
140
if isEmpty {
133
141
return str
134
142
}
135
- if hasSuffix ( " / " ) {
143
+ if validPathSeps . contains ( where : { hasSuffix ( String ( $0 ) ) } ) {
136
144
return self + str
137
145
}
138
- return self + " / " + str
146
+ return self + defaultPathSep + str
139
147
}
140
148
141
149
internal func _stringByFixingSlashes( compress : Bool = true , stripTrailing: Bool = true ) -> String {
@@ -146,13 +154,13 @@ extension String {
146
154
var curPos = startPos
147
155
148
156
while curPos < endPos {
149
- if result [ curPos] == " / " {
157
+ if validPathSeps . contains ( result [ curPos] ) {
150
158
var afterLastSlashPos = curPos
151
- while afterLastSlashPos < endPos && result [ afterLastSlashPos] == " / " {
159
+ while afterLastSlashPos < endPos && validPathSeps . contains ( result [ afterLastSlashPos] ) {
152
160
afterLastSlashPos = result. index ( after: afterLastSlashPos)
153
161
}
154
162
if afterLastSlashPos != result. index ( after: curPos) {
155
- result. replaceSubrange ( curPos ..< afterLastSlashPos, with: [ " / " ] )
163
+ result. replaceSubrange ( curPos ..< afterLastSlashPos, with: [ Character ( defaultPathSep ) ] )
156
164
endPos = result. endIndex
157
165
}
158
166
curPos = afterLastSlashPos
@@ -161,7 +169,7 @@ extension String {
161
169
}
162
170
}
163
171
}
164
- if stripTrailing && result. length > 1 && result. hasSuffix ( " / " ) {
172
+ if stripTrailing && result. length > 1 && validPathSeps . contains ( where : { result. hasSuffix ( String ( $0 ) ) } ) {
165
173
result. remove ( at: result. index ( before: result. endIndex) )
166
174
}
167
175
return result
@@ -227,7 +235,7 @@ extension NSString {
227
235
228
236
public var deletingLastPathComponent : String {
229
237
let fixedSelf = _stringByFixingSlashes ( )
230
- if fixedSelf == " / " || fixedSelf == " " {
238
+ if validPathSeps . contains ( where : { String ( $0 ) == fixedSelf } ) || fixedSelf == " " {
231
239
return fixedSelf
232
240
}
233
241
@@ -239,7 +247,7 @@ extension NSString {
239
247
240
248
// absolute path, single component
241
249
case fixedSelf. index ( after: fixedSelf. startIndex) :
242
- return " / "
250
+ return defaultPathSep
243
251
244
252
// all common cases
245
253
case let startOfLast:
@@ -248,7 +256,7 @@ extension NSString {
248
256
}
249
257
250
258
internal func _stringByFixingSlashes( compress : Bool = true , stripTrailing: Bool = true ) -> String {
251
- if _swiftObject == " / " {
259
+ if validPathSeps . contains ( where : { String ( $0 ) == _swiftObject } ) {
252
260
return _swiftObject
253
261
}
254
262
@@ -259,13 +267,13 @@ extension NSString {
259
267
var curPos = startPos
260
268
261
269
while curPos < endPos {
262
- if result [ curPos] == " / " {
270
+ if validPathSeps . contains ( result [ curPos] ) {
263
271
var afterLastSlashPos = curPos
264
- while afterLastSlashPos < endPos && result [ afterLastSlashPos] == " / " {
272
+ while afterLastSlashPos < endPos && validPathSeps . contains ( result [ afterLastSlashPos] ) {
265
273
afterLastSlashPos = result. index ( after: afterLastSlashPos)
266
274
}
267
275
if afterLastSlashPos != result. index ( after: curPos) {
268
- result. replaceSubrange ( curPos ..< afterLastSlashPos, with: [ " / " ] )
276
+ result. replaceSubrange ( curPos ..< afterLastSlashPos, with: [ Character ( defaultPathSep ) ] )
269
277
endPos = result. endIndex
270
278
}
271
279
curPos = afterLastSlashPos
@@ -274,7 +282,7 @@ extension NSString {
274
282
}
275
283
}
276
284
}
277
- if stripTrailing && result. hasSuffix ( " / " ) {
285
+ if stripTrailing && validPathSeps . contains ( where : { result. hasSuffix ( String ( $0 ) ) } ) {
278
286
result. remove ( at: result. index ( before: result. endIndex) )
279
287
}
280
288
return result
@@ -314,7 +322,7 @@ extension NSString {
314
322
}
315
323
316
324
public func appendingPathExtension( _ str: String ) -> String ? {
317
- if str. hasPrefix ( " / " ) || self == " " || self == " / " {
325
+ if validPathSeps . contains ( where : { str. hasPrefix ( String ( $0 ) ) } ) || self == " " || validPathSeps . contains ( where : { String ( $0 ) . _nsObject == self } ) {
318
326
print ( " Cannot append extension \( str) to path \( self ) " )
319
327
return nil
320
328
}
@@ -327,7 +335,7 @@ extension NSString {
327
335
return _swiftObject
328
336
}
329
337
330
- let endOfUserName = _swiftObject. firstIndex ( of : " / " ) ?? _swiftObject. endIndex
338
+ let endOfUserName = _swiftObject. firstIndex ( where : { validPathSeps . contains ( $0 ) } ) ?? _swiftObject. endIndex
331
339
let startOfUserName = _swiftObject. index ( after: _swiftObject. startIndex)
332
340
let userName = String ( _swiftObject [ startOfUserName..< endOfUserName] )
333
341
let optUserName : String ? = userName. isEmpty ? nil : userName
@@ -359,12 +367,10 @@ extension NSString {
359
367
}
360
368
361
369
// TODO: pathComponents keeps final path separator if any. Check that logic.
362
- if components . last == " / " && components. count > 1 {
370
+ if validPathSeps . contains ( where : { String ( $0 ) == components . last } ) && components. count > 1 {
363
371
components. removeLast ( )
364
372
}
365
373
366
- let isAbsolutePath = components. first == " / "
367
-
368
374
var resolvedPath = components. removeFirst ( )
369
375
for component in components {
370
376
switch component {
@@ -426,7 +432,7 @@ extension NSString {
426
432
let commonPath = searchAllFilesInDirectory ? path : _ensureLastPathSeparator ( deletingLastPathComponent)
427
433
428
434
if searchAllFilesInDirectory {
429
- outputName = " / "
435
+ outputName = defaultPathSep
430
436
} else {
431
437
if let lcp = _longestCommonPrefix ( matches, caseSensitive: flag) {
432
438
outputName = ( commonPath + lcp)
@@ -439,7 +445,7 @@ extension NSString {
439
445
}
440
446
441
447
internal func _stringIsPathToDirectory( _ path: String ) -> Bool {
442
- if !path. hasSuffix ( " / " ) {
448
+ if !validPathSeps . contains ( where : { path. hasSuffix ( String ( $0 ) ) } ) {
443
449
return false
444
450
}
445
451
@@ -538,11 +544,11 @@ extension NSString {
538
544
}
539
545
540
546
internal func _ensureLastPathSeparator( _ path: String ) -> String {
541
- if path. hasSuffix ( " / " ) || path. isEmpty {
547
+ if validPathSeps . contains ( where : { path. hasSuffix ( String ( $0 ) ) } ) || path. isEmpty {
542
548
return path
543
549
}
544
550
545
- return path + " / "
551
+ return path + defaultPathSep
546
552
}
547
553
548
554
public var fileSystemRepresentation : UnsafePointer < Int8 > {
0 commit comments