9
9
10
10
import CoreFoundation
11
11
12
+ #if os(Windows)
13
+ let validPathSeps : [ Character ] = [ " \\ " , " / " ]
14
+ #else
15
+ let validPathSeps : [ Character ] = [ " / " ]
16
+ #endif
17
+
12
18
public func NSTemporaryDirectory( ) -> String {
13
19
#if os(Windows)
14
20
let cchLength : DWORD = GetTempPathW ( 0 , nil )
@@ -51,7 +57,7 @@ public func NSTemporaryDirectory() -> String {
51
57
}
52
58
#endif
53
59
if let tmpdir = ProcessInfo . processInfo. environment [ " TMPDIR " ] {
54
- if !tmpdir. hasSuffix ( " / " ) {
60
+ if !validPathSeps . contains ( where : { tmpdir. hasSuffix ( String ( $0 ) ) } ) {
55
61
return tmpdir + " / "
56
62
} else {
57
63
return tmpdir
@@ -70,15 +76,15 @@ public func NSTemporaryDirectory() -> String {
70
76
extension String {
71
77
72
78
internal var _startOfLastPathComponent : String . Index {
73
- precondition ( !hasSuffix( " / " ) && length > 1 )
79
+ precondition ( !validPathSeps . contains ( where : { hasSuffix ( String ( $0 ) ) } ) && length > 1 )
74
80
75
81
let startPos = startIndex
76
82
var curPos = endIndex
77
83
78
84
// Find the beginning of the component
79
85
while curPos > startPos {
80
86
let prevPos = index ( before: curPos)
81
- if self [ prevPos] == " / " {
87
+ if validPathSeps . contains ( self [ prevPos] ) {
82
88
break
83
89
}
84
90
curPos = prevPos
@@ -88,7 +94,7 @@ extension String {
88
94
}
89
95
90
96
internal var _startOfPathExtension : String . Index ? {
91
- precondition ( !hasSuffix( " / " ) )
97
+ precondition ( !validPathSeps . contains ( where : { hasSuffix ( String ( $0 ) ) } ) )
92
98
93
99
var currentPosition = endIndex
94
100
let startOfLastPathComponent = _startOfLastPathComponent
@@ -97,7 +103,7 @@ extension String {
97
103
while currentPosition > startOfLastPathComponent {
98
104
let previousPosition = index ( before: currentPosition)
99
105
let character = self [ previousPosition]
100
- if character == " / " {
106
+ if validPathSeps . contains ( character) {
101
107
return nil
102
108
} else if character == " . " {
103
109
if startOfLastPathComponent == previousPosition {
@@ -132,8 +138,8 @@ extension String {
132
138
if isEmpty {
133
139
return str
134
140
}
135
- if hasSuffix ( " / " ) {
136
- return self + str
141
+ if validPathSeps . contains ( where : { hasSuffix ( String ( $0 ) ) } ) {
142
+ return self + str
137
143
}
138
144
return self + " / " + str
139
145
}
@@ -146,9 +152,9 @@ extension String {
146
152
var curPos = startPos
147
153
148
154
while curPos < endPos {
149
- if result [ curPos] == " / " {
155
+ if validPathSeps . contains ( result [ curPos] ) {
150
156
var afterLastSlashPos = curPos
151
- while afterLastSlashPos < endPos && result [ afterLastSlashPos] == " / " {
157
+ while afterLastSlashPos < endPos && validPathSeps . contains ( result [ afterLastSlashPos] ) {
152
158
afterLastSlashPos = result. index ( after: afterLastSlashPos)
153
159
}
154
160
if afterLastSlashPos != result. index ( after: curPos) {
@@ -161,7 +167,7 @@ extension String {
161
167
}
162
168
}
163
169
}
164
- if stripTrailing && result. length > 1 && result. hasSuffix ( " / " ) {
170
+ if stripTrailing && result. length > 1 && validPathSeps . contains ( where : { result. hasSuffix ( String ( $0 ) ) } ) {
165
171
result. remove ( at: result. index ( before: result. endIndex) )
166
172
}
167
173
return result
@@ -248,7 +254,7 @@ extension NSString {
248
254
}
249
255
250
256
internal func _stringByFixingSlashes( compress : Bool = true , stripTrailing: Bool = true ) -> String {
251
- if _swiftObject == " / " {
257
+ if validPathSeps . contains ( where : { String ( $0 ) == _swiftObject } ) {
252
258
return _swiftObject
253
259
}
254
260
@@ -259,9 +265,9 @@ extension NSString {
259
265
var curPos = startPos
260
266
261
267
while curPos < endPos {
262
- if result [ curPos] == " / " {
268
+ if validPathSeps . contains ( result [ curPos] ) {
263
269
var afterLastSlashPos = curPos
264
- while afterLastSlashPos < endPos && result [ afterLastSlashPos] == " / " {
270
+ while afterLastSlashPos < endPos && validPathSeps . contains ( result [ afterLastSlashPos] ) {
265
271
afterLastSlashPos = result. index ( after: afterLastSlashPos)
266
272
}
267
273
if afterLastSlashPos != result. index ( after: curPos) {
@@ -274,7 +280,7 @@ extension NSString {
274
280
}
275
281
}
276
282
}
277
- if stripTrailing && result. hasSuffix ( " / " ) {
283
+ if stripTrailing && validPathSeps . contains ( where : { result. hasSuffix ( String ( $0 ) ) } ) {
278
284
result. remove ( at: result. index ( before: result. endIndex) )
279
285
}
280
286
return result
@@ -314,7 +320,7 @@ extension NSString {
314
320
}
315
321
316
322
public func appendingPathExtension( _ str: String ) -> String ? {
317
- if str. hasPrefix ( " / " ) || self == " " || self == " / " {
323
+ if validPathSeps . contains ( where : { str. hasPrefix ( String ( $0 ) ) } ) || self == " " || validPathSeps . contains ( where : { String ( $0 ) . _nsObject == self } ) {
318
324
print ( " Cannot append extension \( str) to path \( self ) " )
319
325
return nil
320
326
}
@@ -327,7 +333,7 @@ extension NSString {
327
333
return _swiftObject
328
334
}
329
335
330
- let endOfUserName = _swiftObject. firstIndex ( of : " / " ) ?? _swiftObject. endIndex
336
+ let endOfUserName = _swiftObject. firstIndex ( where : { validPathSeps . contains ( $0 ) } ) ?? _swiftObject. endIndex
331
337
let startOfUserName = _swiftObject. index ( after: _swiftObject. startIndex)
332
338
let userName = String ( _swiftObject [ startOfUserName..< endOfUserName] )
333
339
let optUserName : String ? = userName. isEmpty ? nil : userName
@@ -359,12 +365,10 @@ extension NSString {
359
365
}
360
366
361
367
// TODO: pathComponents keeps final path separator if any. Check that logic.
362
- if components . last == " / " && components. count > 1 {
368
+ if validPathSeps . contains ( where : { String ( $0 ) == components . last } ) && components. count > 1 {
363
369
components. removeLast ( )
364
370
}
365
371
366
- let isAbsolutePath = components. first == " / "
367
-
368
372
var resolvedPath = components. removeFirst ( )
369
373
for component in components {
370
374
switch component {
@@ -439,7 +443,7 @@ extension NSString {
439
443
}
440
444
441
445
internal func _stringIsPathToDirectory( _ path: String ) -> Bool {
442
- if !path. hasSuffix ( " / " ) {
446
+ if !validPathSeps . contains ( where : { path. hasSuffix ( String ( $0 ) ) } ) {
443
447
return false
444
448
}
445
449
@@ -538,7 +542,7 @@ extension NSString {
538
542
}
539
543
540
544
internal func _ensureLastPathSeparator( _ path: String ) -> String {
541
- if path. hasSuffix ( " / " ) || path. isEmpty {
545
+ if validPathSeps . contains ( where : { path. hasSuffix ( String ( $0 ) ) } ) || path. isEmpty {
542
546
return path
543
547
}
544
548
0 commit comments