Skip to content

Commit c4a9c5f

Browse files
committed
Replace some C-style forloop with equivalent logic
1 parent 6f8bee1 commit c4a9c5f

File tree

7 files changed

+32
-26
lines changed

7 files changed

+32
-26
lines changed

stdlib/public/core/CString.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public func _persistCString(s: UnsafePointer<CChar>) -> [CChar]? {
6060
}
6161
let length = Int(_swift_stdlib_strlen(s))
6262
var result = [CChar](count: length + 1, repeatedValue: 0)
63-
for var i = 0; i < length; ++i {
63+
for i in 0..<length {
6464
// FIXME: this will not compile on platforms where 'CChar' is unsigned.
6565
result[i] = s[i]
6666
}

stdlib/public/core/Character.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public struct Character :
120120
@warn_unused_result
121121
static func _smallSize(value: UInt64) -> Int {
122122
var mask: UInt64 = 0xFF
123-
for var i = 0; i < 8; ++i {
123+
for i in 0..<8 {
124124
if (value & mask) == mask {
125125
return i
126126
}

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -730,12 +730,14 @@ public func == <Element : Hashable>(lhs: Set<Element>, rhs: Set<Element>) -> Boo
730730
}
731731

732732
let endIndex = lhsNative.endIndex
733-
for var i = lhsNative.startIndex; i != endIndex; i = i.successor() {
734-
let key = lhsNative.assertingGet(i)
733+
var index = lhsNative.startIndex
734+
while index != endIndex {
735+
let key = lhsNative.assertingGet(index)
735736
let bridgedKey: AnyObject = _bridgeToObjectiveCUnconditional(key)
736737
let optRhsValue: AnyObject? = rhsCocoa.maybeGet(bridgedKey)
737738
if let rhsValue = optRhsValue {
738739
if key == _forceBridgeFromObjectiveC(rhsValue, Element.self) {
740+
index = index.successor()
739741
continue
740742
}
741743
}
@@ -1247,13 +1249,14 @@ public func == <Key : Equatable, Value : Equatable>(
12471249
}
12481250

12491251
let endIndex = lhsNative.endIndex
1250-
for var index = lhsNative.startIndex; index != endIndex;
1251-
index._successorInPlace() {
1252+
var index = lhsNative.startIndex
1253+
while index != endIndex {
12521254
let (key, value) = lhsNative.assertingGet(index)
12531255
let optRhsValue: AnyObject? =
12541256
rhsCocoa.maybeGet(_bridgeToObjectiveCUnconditional(key))
12551257
if let rhsValue = optRhsValue {
12561258
if value == _forceBridgeFromObjectiveC(rhsValue, Value.self) {
1259+
index._successorInPlace()
12571260
continue
12581261
}
12591262
}
@@ -2130,7 +2133,7 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
21302133
var description: String {
21312134
var result = ""
21322135
#if INTERNAL_CHECKS_ENABLED
2133-
for var i = 0; i != capacity; ++i {
2136+
for i in 0..<capacity {
21342137
if isInitializedEntry(i) {
21352138
let key = keyAt(i)
21362139
result += "bucket \(i), ideal bucket = \(_bucket(key)), key = \(key)\n"
@@ -2618,7 +2621,7 @@ final internal class _Native${Self}StorageOwner<${TypeParametersDecl}>
26182621
let bridged = _createBridgedNativeStorage(nativeStorage.capacity)
26192622

26202623
// Bridge everything.
2621-
for var i = 0; i < nativeStorage.capacity; ++i {
2624+
for i in 0..<nativeStorage.capacity {
26222625
if nativeStorage.isInitializedEntry(i) {
26232626
let key = _bridgeToObjectiveCUnconditional(nativeStorage.keyAt(i))
26242627
%if Self == 'Set':
@@ -3265,20 +3268,19 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
32653268

32663269
// Find the last bucket in the contiguous chain
32673270
var lastInChain = hole
3268-
for var b = nativeStorage._next(lastInChain);
3269-
nativeStorage.isInitializedEntry(b);
3270-
b = nativeStorage._next(b) {
3271-
lastInChain = b
3271+
var bucket = nativeStorage._next(lastInChain)
3272+
while nativeStorage.isInitializedEntry(bucket) {
3273+
lastInChain = bucket
3274+
bucket = nativeStorage._next(bucket)
32723275
}
3273-
32743276
// Relocate out-of-place elements in the chain, repeating until
32753277
// none are found.
32763278
while hole != lastInChain {
32773279
// Walk backwards from the end of the chain looking for
32783280
// something out-of-place.
3279-
var b: Int
3280-
for b = lastInChain; b != hole; b = nativeStorage._prev(b) {
3281-
let idealBucket = nativeStorage._bucket(nativeStorage.keyAt(b))
3281+
var bucket: Int = lastInChain
3282+
while bucket != hole {
3283+
let idealBucket = nativeStorage._bucket(nativeStorage.keyAt(bucket))
32823284

32833285
// Does this element belong between start and hole? We need
32843286
// two separate tests depending on whether [start,hole] wraps
@@ -3288,15 +3290,16 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
32883290
if start <= hole ? (c0 && c1) : (c0 || c1) {
32893291
break // Found it
32903292
}
3293+
bucket = nativeStorage._prev(bucket)
32913294
}
32923295

3293-
if b == hole { // No out-of-place elements found; we're done adjusting
3296+
if bucket == hole { // No out-of-place elements found; we're done adjusting
32943297
break
32953298
}
32963299

32973300
// Move the found element into the hole
3298-
nativeStorage.moveInitializeFrom(nativeStorage, at: b, toEntryAt: hole)
3299-
hole = b
3301+
nativeStorage.moveInitializeFrom(nativeStorage, at: bucket, toEntryAt: hole)
3302+
hole = bucket
33003303
}
33013304
}
33023305

stdlib/public/core/Prespecialized.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ struct _Prespecialize {
2929
a[0] = a[j]
3030
}
3131

32-
for var i1 = 0; i1 < a.count; ++i1 {
33-
for var i2 = 0; i2 < a.count; ++i2 {
32+
for i1 in 0..<a.count {
33+
for i2 in 0..<a.count {
3434
a[i1] = a[i2]
3535
}
3636
}

stdlib/public/core/StringCharacterView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ extension String.CharacterView : CollectionType {
145145
unicodeScalars[start].value)
146146
start._successorInPlace()
147147

148-
for ; start != end; start._successorInPlace() {
148+
while start != end {
149149
// FIXME(performance): consider removing this "fast path". A branch
150150
// that is hard to predict could be worse for performance than a few
151151
// loads from cache to fetch the property 'gcb1'.
@@ -158,6 +158,7 @@ extension String.CharacterView : CollectionType {
158158
break
159159
}
160160
gcb0 = gcb1
161+
start._successorInPlace()
161162
}
162163

163164
return start._position - startIndexUTF16

stdlib/public/core/Unicode.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,9 +705,8 @@ public func transcode<
705705

706706
var inputDecoder = inputEncoding.init()
707707
var hadError = false
708-
for var scalar = inputDecoder.decode(&input);
709-
!scalar.isEmptyInput();
710-
scalar = inputDecoder.decode(&input) {
708+
var scalar = inputDecoder.decode(&input);
709+
while !scalar.isEmptyInput() {
711710
switch scalar {
712711
case .Result(let us):
713712
OutputEncoding.encode(us, output: output)
@@ -721,6 +720,7 @@ public func transcode<
721720
hadError = true
722721
}
723722
}
723+
scalar = inputDecoder.decode(&input)
724724
}
725725
return hadError
726726
}

stdlib/public/core/UnsafePointer.swift.gyb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ ${comment}
210210
_debugPrecondition(
211211
source < self || source >= self + count,
212212
"${Self}.assignBackwardFrom non-preceding overlapping range; use assignFrom instead")
213-
for var i = count; --i >= 0; {
213+
var i = count
214+
while i >= 0 {
215+
i -= 1
214216
self[i] = source[i]
215217
}
216218
}

0 commit comments

Comments
 (0)