Skip to content

Commit f82389a

Browse files
committed
Merge pull request swiftlang#923 from johnlui/master
[stdlib] Change C-style for loop to Swift-style for-in loop
2 parents e8886a4 + 451ce37 commit f82389a

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
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 += 1 {
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 += 1 {
123+
for i in 0..<8 {
124124
if (value & mask) == mask {
125125
return i
126126
}

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -713,15 +713,18 @@ public func == <Element : Hashable>(lhs: Set<Element>, rhs: Set<Element>) -> Boo
713713
}
714714

715715
let endIndex = lhsNative.endIndex
716-
for var i = lhsNative.startIndex; i != endIndex; i = i.successor() {
716+
var i = lhsNative.startIndex
717+
while i != endIndex {
717718
let key = lhsNative.assertingGet(i)
718719
let bridgedKey: AnyObject = _bridgeToObjectiveCUnconditional(key)
719720
let optRhsValue: AnyObject? = rhsCocoa.maybeGet(bridgedKey)
720721
if let rhsValue = optRhsValue {
721722
if key == _forceBridgeFromObjectiveC(rhsValue, Element.self) {
723+
i = i.successor()
722724
continue
723725
}
724726
}
727+
i = i.successor()
725728
return false
726729
}
727730
return true
@@ -1230,16 +1233,18 @@ public func == <Key : Equatable, Value : Equatable>(
12301233
}
12311234

12321235
let endIndex = lhsNative.endIndex
1233-
for var index = lhsNative.startIndex; index != endIndex;
1234-
index._successorInPlace() {
1236+
var index = lhsNative.startIndex
1237+
while index != endIndex {
12351238
let (key, value) = lhsNative.assertingGet(index)
12361239
let optRhsValue: AnyObject? =
12371240
rhsCocoa.maybeGet(_bridgeToObjectiveCUnconditional(key))
12381241
if let rhsValue = optRhsValue {
12391242
if value == _forceBridgeFromObjectiveC(rhsValue, Value.self) {
1243+
index._successorInPlace()
12401244
continue
12411245
}
12421246
}
1247+
index._successorInPlace()
12431248
return false
12441249
}
12451250
return true
@@ -2113,7 +2118,7 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
21132118
var description: String {
21142119
var result = ""
21152120
#if INTERNAL_CHECKS_ENABLED
2116-
for var i = 0; i != capacity; i += 1 {
2121+
for i in 0..<capacity {
21172122
if isInitializedEntry(i) {
21182123
let key = keyAt(i)
21192124
result += "bucket \(i), ideal bucket = \(_bucket(key)), key = \(key)\n"
@@ -2601,7 +2606,7 @@ final internal class _Native${Self}StorageOwner<${TypeParametersDecl}>
26012606
let bridged = _createBridgedNativeStorage(nativeStorage.capacity)
26022607

26032608
// Bridge everything.
2604-
for var i = 0; i < nativeStorage.capacity; i += 1 {
2609+
for i in 0..<nativeStorage.capacity {
26052610
if nativeStorage.isInitializedEntry(i) {
26062611
let key = _bridgeToObjectiveCUnconditional(nativeStorage.keyAt(i))
26072612
%if Self == 'Set':
@@ -3248,19 +3253,19 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
32483253

32493254
// Find the last bucket in the contiguous chain
32503255
var lastInChain = hole
3251-
for var b = nativeStorage._next(lastInChain);
3252-
nativeStorage.isInitializedEntry(b);
3253-
b = nativeStorage._next(b) {
3256+
var b = nativeStorage._next(lastInChain)
3257+
while nativeStorage.isInitializedEntry(b) {
32543258
lastInChain = b
3259+
b = nativeStorage._next(b)
32553260
}
32563261

32573262
// Relocate out-of-place elements in the chain, repeating until
32583263
// none are found.
32593264
while hole != lastInChain {
32603265
// Walk backwards from the end of the chain looking for
32613266
// something out-of-place.
3262-
var b: Int
3263-
for b = lastInChain; b != hole; b = nativeStorage._prev(b) {
3267+
var b = lastInChain
3268+
while b != hole {
32643269
let idealBucket = nativeStorage._bucket(nativeStorage.keyAt(b))
32653270

32663271
// Does this element belong between start and hole? We need
@@ -3271,6 +3276,7 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
32713276
if start <= hole ? (c0 && c1) : (c0 || c1) {
32723277
break // Found it
32733278
}
3279+
b = nativeStorage._prev(b)
32743280
}
32753281

32763282
if b == hole { // No out-of-place elements found; we're done adjusting
@@ -3409,7 +3415,7 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
34093415
nativeStorage = native
34103416
}
34113417

3412-
for var b = 0; b != nativeStorage.capacity; b += 1 {
3418+
for b in 0..<nativeStorage.capacity {
34133419
if nativeStorage.isInitializedEntry(b) {
34143420
nativeStorage.destroyEntryAt(b)
34153421
}

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
}

0 commit comments

Comments
 (0)