Skip to content

Commit 2019a97

Browse files
authored
Merge pull request swiftlang#25066 from ikesyo/use-isempty-over-count
[gardening] Use `Collection.isEmpty` over `Collection.count`
2 parents 4cb6c71 + a35c9f0 commit 2019a97

File tree

11 files changed

+22
-22
lines changed

11 files changed

+22
-22
lines changed

stdlib/public/Darwin/Foundation/Collections+DataProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension Repeated: DataProtocol where Element == UInt8 {
4949
public typealias Regions = Repeated<Data>
5050

5151
public var regions: Repeated<Data> {
52-
guard self.count > 0 else { return repeatElement(Data(), count: 0) }
52+
guard !self.isEmpty else { return repeatElement(Data(), count: 0) }
5353
return repeatElement(Data(CollectionOfOne(self.first!)), count: self.count)
5454
}
5555
}

stdlib/public/Darwin/Foundation/Data.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
639639
@inlinable // This is @inlinable as a convenience initializer.
640640
init(_ srcBuffer: UnsafeRawBufferPointer) {
641641
self.init(count: srcBuffer.count)
642-
if srcBuffer.count > 0 {
642+
if !srcBuffer.isEmpty {
643643
Swift.withUnsafeMutableBytes(of: &bytes) { dstBuffer in
644644
dstBuffer.baseAddress?.copyMemory(from: srcBuffer.baseAddress!, byteCount: srcBuffer.count)
645645
}
@@ -729,7 +729,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
729729

730730
@inlinable // This is @inlinable as trivially computable.
731731
mutating func append(contentsOf buffer: UnsafeRawBufferPointer) {
732-
guard buffer.count > 0 else { return }
732+
guard !buffer.isEmpty else { return }
733733
assert(count + buffer.count <= MemoryLayout<Buffer>.size)
734734
let cnt = count
735735
_ = Swift.withUnsafeMutableBytes(of: &bytes) { rawBuffer in
@@ -1270,7 +1270,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
12701270

12711271
@inlinable // This is @inlinable as a trivial initializer.
12721272
init(_ buffer: UnsafeRawBufferPointer) {
1273-
if buffer.count == 0 {
1273+
if buffer.isEmpty {
12741274
self = .empty
12751275
} else if InlineData.canStore(count: buffer.count) {
12761276
self = .inline(InlineData(buffer))
@@ -1283,7 +1283,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
12831283

12841284
@inlinable // This is @inlinable as a trivial initializer.
12851285
init(_ buffer: UnsafeRawBufferPointer, owner: AnyObject) {
1286-
if buffer.count == 0 {
1286+
if buffer.isEmpty {
12871287
self = .empty
12881288
} else if InlineData.canStore(count: buffer.count) {
12891289
self = .inline(InlineData(buffer))
@@ -2313,7 +2313,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
23132313
}
23142314

23152315
public mutating func append(_ other: Data) {
2316-
guard other.count > 0 else { return }
2316+
guard !other.isEmpty else { return }
23172317
other.withUnsafeBytes { (buffer: UnsafeRawBufferPointer) in
23182318
_representation.append(contentsOf: buffer)
23192319
}

stdlib/public/Darwin/Foundation/URL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ public struct URL : ReferenceConvertible, Equatable {
586586
/// If the data representation is not a legal URL string as ASCII bytes, the URL object may not behave as expected. If the URL cannot be formed then this will return nil.
587587
@available(macOS 10.11, iOS 9.0, *)
588588
public init?(dataRepresentation: __shared Data, relativeTo url: __shared URL?, isAbsolute: Bool = false) {
589-
guard dataRepresentation.count > 0 else { return nil }
589+
guard !dataRepresentation.isEmpty else { return nil }
590590

591591
if isAbsolute {
592592
_url = URL._converted(from: NSURL(absoluteURLWithDataRepresentation: dataRepresentation, relativeTo: url))

stdlib/public/Darwin/Network/NWListener.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public final class NWListener: CustomDebugStringConvertible {
4747
} else {
4848
description = String("\(description).*")
4949
}
50-
if txtRecord != nil && txtRecord!.count > 0 {
51-
description = String("\(description) <\(txtRecord!.count) bytes of txt>")
50+
if let txtRecord = txtRecord, !txtRecord.isEmpty {
51+
description = String("\(description) <\(txtRecord.count) bytes of txt>")
5252
}
5353
return description
5454
}

stdlib/public/core/CollectionDifference.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public struct CollectionDifference<ChangeElement> {
8989
private static func _validateChanges<Changes: Collection>(
9090
_ changes : Changes
9191
) -> Bool where Changes.Element == Change {
92-
if changes.count == 0 { return true }
92+
if changes.isEmpty { return true }
9393

9494
var insertAssocToOffset = Dictionary<Int,Int>()
9595
var removeOffsetToAssoc = Dictionary<Int,Int>()
@@ -181,7 +181,7 @@ public struct CollectionDifference<ChangeElement> {
181181

182182
// Find first insertion via binary search
183183
let firstInsertIndex: Int
184-
if sortedChanges.count == 0 {
184+
if sortedChanges.isEmpty {
185185
firstInsertIndex = 0
186186
} else {
187187
var range = 0...sortedChanges.count

stdlib/public/core/DebuggerSupport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public enum _DebuggerSupport {
119119
isRoot: Bool
120120
) -> Bool {
121121
if isRoot || collectionStatus.isCollection { return true }
122-
if mirror.children.count > 0 { return true }
122+
if !mirror.children.isEmpty { return true }
123123
if mirror.displayStyle == .`class` { return true }
124124
if let sc = mirror.superclassMirror { return ivarCount(mirror: sc) > 0 }
125125
return true

stdlib/public/core/Dictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ extension Collection {
16781678
internal func _makeKeyValuePairDescription<K, V>(
16791679
withTypeName type: String? = nil
16801680
) -> String where Element == (key: K, value: V) {
1681-
if self.count == 0 {
1681+
if self.isEmpty {
16821682
return "[:]"
16831683
}
16841684

stdlib/public/core/KeyPath.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ internal struct KeyPathBuffer {
17201720

17211721
// fetch type, which is in the buffer unless it's the final component
17221722
let nextType: Any.Type?
1723-
if data.count == 0 {
1723+
if data.isEmpty {
17241724
nextType = nil
17251725
} else {
17261726
nextType = _pop(from: &data, as: Any.Type.self)
@@ -2244,7 +2244,7 @@ internal func _appendingKeyPaths<
22442244
}
22452245
}
22462246

2247-
_internalInvariant(destBuffer.count == 0,
2247+
_internalInvariant(destBuffer.isEmpty,
22482248
"did not fill entire result buffer")
22492249
}
22502250

stdlib/public/core/StringUTF8Validation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ internal func validateUTF8(_ buf: UnsafeBufferPointer<UInt8>) -> UTF8ValidationR
154154
}
155155

156156
internal func repairUTF8(_ input: UnsafeBufferPointer<UInt8>, firstKnownBrokenRange: Range<Int>) -> String {
157-
_internalInvariant(input.count > 0, "empty input doesn't need to be repaired")
157+
_internalInvariant(!input.isEmpty, "empty input doesn't need to be repaired")
158158
_internalInvariant(firstKnownBrokenRange.clamped(to: input.indices) == firstKnownBrokenRange)
159159
// During this process, `remainingInput` contains the remaining bytes to process. It's split into three
160160
// non-overlapping sub-regions:
@@ -176,8 +176,8 @@ internal func repairUTF8(_ input: UnsafeBufferPointer<UInt8>, firstKnownBrokenRa
176176
var brokenRange: Range<Int> = firstKnownBrokenRange
177177
var remainingInput = input
178178
repeat {
179-
_internalInvariant(brokenRange.count > 0, "broken range empty")
180-
_internalInvariant(remainingInput.count > 0, "empty remaining input doesn't need to be repaired")
179+
_internalInvariant(!brokenRange.isEmpty, "broken range empty")
180+
_internalInvariant(!remainingInput.isEmpty, "empty remaining input doesn't need to be repaired")
181181
let goodChunk = remainingInput[..<brokenRange.startIndex]
182182

183183
// very likely this capacity reservation does not actually do anything because we reserved space for the entire
@@ -199,6 +199,6 @@ internal func repairUTF8(_ input: UnsafeBufferPointer<UInt8>, firstKnownBrokenRa
199199
case .error(let newBrokenRange):
200200
brokenRange = newBrokenRange
201201
}
202-
} while remainingInput.count > 0
202+
} while !remainingInput.isEmpty
203203
return String(result)
204204
}

stdlib/public/core/VarArgs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public func _encodeBitsAsWords<T>(_ x: T) -> [Int] {
201201
let result = [Int](
202202
repeating: 0,
203203
count: (MemoryLayout<T>.size + MemoryLayout<Int>.size - 1) / MemoryLayout<Int>.size)
204-
_internalInvariant(result.count > 0)
204+
_internalInvariant(!result.isEmpty)
205205
var tmp = x
206206
// FIXME: use UnsafeMutablePointer.assign(from:) instead of memcpy.
207207
_memcpy(dest: UnsafeMutablePointer(result._baseAddressIfContiguous!),

0 commit comments

Comments
 (0)