Skip to content

Commit 833f572

Browse files
authored
Merge pull request #1433 from spevans/pr_mutable_characterview
2 parents 458977b + d617447 commit 833f572

File tree

2 files changed

+48
-54
lines changed

2 files changed

+48
-54
lines changed

Foundation/NSPathUtilities.swift

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,23 @@ internal extension String {
103103
internal func _stringByFixingSlashes(compress : Bool = true, stripTrailing: Bool = true) -> String {
104104
var result = self
105105
if compress {
106-
result.withMutableCharacters { characterView in
107-
let startPos = characterView.startIndex
108-
var endPos = characterView.endIndex
109-
var curPos = startPos
110-
111-
while curPos < endPos {
112-
if characterView[curPos] == "/" {
113-
var afterLastSlashPos = curPos
114-
while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
115-
afterLastSlashPos = characterView.index(after: afterLastSlashPos)
116-
}
117-
if afterLastSlashPos != characterView.index(after: curPos) {
118-
characterView.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
119-
endPos = characterView.endIndex
120-
}
121-
curPos = afterLastSlashPos
122-
} else {
123-
curPos = characterView.index(after: curPos)
106+
let startPos = result.startIndex
107+
var endPos = result.endIndex
108+
var curPos = startPos
109+
110+
while curPos < endPos {
111+
if result[curPos] == "/" {
112+
var afterLastSlashPos = curPos
113+
while afterLastSlashPos < endPos && result[afterLastSlashPos] == "/" {
114+
afterLastSlashPos = result.index(after: afterLastSlashPos)
124115
}
116+
if afterLastSlashPos != result.index(after: curPos) {
117+
result.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
118+
endPos = result.endIndex
119+
}
120+
curPos = afterLastSlashPos
121+
} else {
122+
curPos = result.index(after: curPos)
125123
}
126124
}
127125
}
@@ -214,25 +212,23 @@ public extension NSString {
214212

215213
var result = _swiftObject
216214
if compress {
217-
result.withMutableCharacters { characterView in
218-
let startPos = characterView.startIndex
219-
var endPos = characterView.endIndex
220-
var curPos = startPos
221-
222-
while curPos < endPos {
223-
if characterView[curPos] == "/" {
224-
var afterLastSlashPos = curPos
225-
while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
226-
afterLastSlashPos = characterView.index(after: afterLastSlashPos)
227-
}
228-
if afterLastSlashPos != characterView.index(after: curPos) {
229-
characterView.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
230-
endPos = characterView.endIndex
231-
}
232-
curPos = afterLastSlashPos
233-
} else {
234-
curPos = characterView.index(after: curPos)
215+
let startPos = result.startIndex
216+
var endPos = result.endIndex
217+
var curPos = startPos
218+
219+
while curPos < endPos {
220+
if result[curPos] == "/" {
221+
var afterLastSlashPos = curPos
222+
while afterLastSlashPos < endPos && result[afterLastSlashPos] == "/" {
223+
afterLastSlashPos = result.index(after: afterLastSlashPos)
235224
}
225+
if afterLastSlashPos != result.index(after: curPos) {
226+
result.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
227+
endPos = result.endIndex
228+
}
229+
curPos = afterLastSlashPos
230+
} else {
231+
curPos = result.index(after: curPos)
236232
}
237233
}
238234
}

Foundation/NSURL.swift

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -719,25 +719,23 @@ extension NSURL {
719719

720720
var result = p
721721
if compress {
722-
result.withMutableCharacters { characterView in
723-
let startPos = characterView.startIndex
724-
var endPos = characterView.endIndex
725-
var curPos = startPos
726-
727-
while curPos < endPos {
728-
if characterView[curPos] == "/" {
729-
var afterLastSlashPos = curPos
730-
while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
731-
afterLastSlashPos = characterView.index(after: afterLastSlashPos)
732-
}
733-
if afterLastSlashPos != characterView.index(after: curPos) {
734-
characterView.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
735-
endPos = characterView.endIndex
736-
}
737-
curPos = afterLastSlashPos
738-
} else {
739-
curPos = characterView.index(after: curPos)
722+
let startPos = result.startIndex
723+
var endPos = result.endIndex
724+
var curPos = startPos
725+
726+
while curPos < endPos {
727+
if result[curPos] == "/" {
728+
var afterLastSlashPos = curPos
729+
while afterLastSlashPos < endPos && result[afterLastSlashPos] == "/" {
730+
afterLastSlashPos = result.index(after: afterLastSlashPos)
740731
}
732+
if afterLastSlashPos != result.index(after: curPos) {
733+
result.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
734+
endPos = result.endIndex
735+
}
736+
curPos = afterLastSlashPos
737+
} else {
738+
curPos = result.index(after: curPos)
741739
}
742740
}
743741
}

0 commit comments

Comments
 (0)