Skip to content

Commit fafd9d3

Browse files
committed
[span] readability improvements
1 parent e37475b commit fafd9d3

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

stdlib/public/core/Span/RawSpan.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ extension RawSpan {
179179
) {
180180
_precondition(count >= 0, "Count must not be negative")
181181
self.init(
182-
_unchecked: pointer, byteCount: count*MemoryLayout<T>.stride
182+
_unchecked: pointer, byteCount: count * MemoryLayout<T>.stride
183183
)
184184
}
185185

@@ -601,8 +601,8 @@ extension RawSpan {
601601
@_alwaysEmitIntoClient
602602
public func _extracting(droppingLast k: Int) -> Self {
603603
_precondition(k >= 0, "Can't drop a negative number of elements.")
604-
let dc = min(k, byteCount)
605-
return Self(_unchecked: _pointer, byteCount: byteCount&-dc)
604+
let droppedCount = min(k, byteCount)
605+
return Self(_unchecked: _pointer, byteCount: byteCount &- droppedCount)
606606
}
607607

608608
/// Returns a span containing the trailing bytes of the span,
@@ -625,7 +625,7 @@ extension RawSpan {
625625
public func _extracting(last maxLength: Int) -> Self {
626626
_precondition(maxLength >= 0, "Can't have a suffix of negative length.")
627627
let newCount = min(maxLength, byteCount)
628-
let newStart = _pointer?.advanced(by: byteCount&-newCount)
628+
let newStart = _pointer?.advanced(by: byteCount &- newCount)
629629
return Self(_unchecked: newStart, byteCount: newCount)
630630
}
631631

@@ -647,8 +647,8 @@ extension RawSpan {
647647
@_alwaysEmitIntoClient
648648
public func _extracting(droppingFirst k: Int) -> Self {
649649
_precondition(k >= 0, "Can't drop a negative number of elements.")
650-
let dc = min(k, byteCount)
651-
let newStart = _pointer?.advanced(by: dc)
652-
return Self(_unchecked: newStart, byteCount: byteCount&-dc)
650+
let droppedCount = min(k, byteCount)
651+
let newStart = _pointer?.advanced(by: droppedCount)
652+
return Self(_unchecked: newStart, byteCount: byteCount &- droppedCount)
653653
}
654654
}

stdlib/public/core/Span/Span.swift

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extension Span where Element: ~Copyable {
6363
) {
6464
_precondition(
6565
((Int(bitPattern: buffer.baseAddress) &
66-
(MemoryLayout<Element>.alignment&-1)) == 0),
66+
(MemoryLayout<Element>.alignment &- 1)) == 0),
6767
"baseAddress must be properly aligned to access Element"
6868
)
6969
self.init(_unchecked: buffer.baseAddress, count: buffer.count)
@@ -102,11 +102,11 @@ extension Span where Element: ~Copyable {
102102
@_alwaysEmitIntoClient
103103
@lifetime(immortal)
104104
public init(
105-
_unsafeStart start: UnsafePointer<Element>,
105+
_unsafeStart pointer: UnsafePointer<Element>,
106106
count: Int
107107
) {
108108
_precondition(count >= 0, "Count must not be negative")
109-
self.init(_unsafeElements: .init(start: start, count: count))
109+
self.init(_unsafeElements: .init(start: pointer, count: count))
110110
}
111111
}
112112

@@ -177,12 +177,14 @@ extension Span where Element: BitwiseCopyable {
177177
) {
178178
_precondition(
179179
((Int(bitPattern: buffer.baseAddress) &
180-
(MemoryLayout<Element>.alignment&-1)) == 0),
180+
(MemoryLayout<Element>.alignment &- 1)) == 0),
181181
"baseAddress must be properly aligned to access Element"
182182
)
183183
let (byteCount, stride) = (buffer.count, MemoryLayout<Element>.stride)
184184
let (count, remainder) = byteCount.quotientAndRemainder(dividingBy: stride)
185-
_precondition(remainder == 0, "Span must contain a whole number of elements")
185+
_precondition(
186+
remainder == 0, "Span must contain a whole number of elements"
187+
)
186188
self.init(_unchecked: buffer.baseAddress, count: count)
187189
}
188190

@@ -428,10 +430,13 @@ extension Span where Element: ~Copyable {
428430
public subscript(unchecked position: Index) -> Element {
429431
//FIXME: change to unsafeRawAddress or unsafeAddress when ready
430432
_read {
431-
let element = _start().advanced(by: position&*MemoryLayout<Element>.stride)
432-
let binding = Builtin.bindMemory(element._rawValue, count._builtinWordValue, Element.self)
433-
defer { Builtin.rebindMemory(element._rawValue, binding) }
434-
yield UnsafePointer<Element>(element._rawValue).pointee
433+
let elementOffset = position &* MemoryLayout<Element>.stride
434+
let address = _start().advanced(by: elementOffset)
435+
let binding = Builtin.bindMemory(
436+
address._rawValue, 1._builtinWordValue, Element.self
437+
)
438+
defer { Builtin.rebindMemory(address._rawValue, binding) }
439+
yield UnsafePointer<Element>(address._rawValue).pointee
435440
}
436441
}
437442
}
@@ -471,7 +476,8 @@ extension Span where Element: BitwiseCopyable {
471476
@_alwaysEmitIntoClient
472477
public subscript(unchecked position: Index) -> Element {
473478
get {
474-
let address = _start().advanced(by: position&*MemoryLayout<Element>.stride)
479+
let elementOffset = position &* MemoryLayout<Element>.stride
480+
let address = _start().advanced(by: elementOffset)
475481
return address.loadUnaligned(as: Element.self)
476482
}
477483
}
@@ -525,7 +531,7 @@ extension Span where Element: ~Copyable {
525531
@unsafe
526532
@_alwaysEmitIntoClient
527533
public func _extracting(unchecked bounds: Range<Index>) -> Self {
528-
let delta = bounds.lowerBound&*MemoryLayout<Element>.stride
534+
let delta = bounds.lowerBound &* MemoryLayout<Element>.stride
529535
return Span(_unchecked: _pointer?.advanced(by: delta), count: bounds.count)
530536
}
531537

@@ -676,8 +682,8 @@ extension Span where Element: ~Copyable {
676682
}
677683
let start = _start()
678684
let stride = MemoryLayout<Element>.stride
679-
let spanEnd = spanStart + stride&*span._count
680-
if spanStart < start || spanEnd > (start + stride&*_count) { return nil }
685+
let spanEnd = spanStart + stride &* span._count
686+
if spanStart < start || spanEnd > (start + stride &* _count) { return nil }
681687
let byteOffset = start.distance(to: spanStart)
682688
let (lower, r) = byteOffset.quotientAndRemainder(dividingBy: stride)
683689
guard r == 0 else { return nil }
@@ -732,7 +738,7 @@ extension Span where Element: ~Copyable {
732738
public func _extracting(droppingLast k: Int) -> Self {
733739
_precondition(k >= 0, "Can't drop a negative number of elements.")
734740
let droppedCount = min(k, count)
735-
return Self(_unchecked: _pointer, count: count&-droppedCount)
741+
return Self(_unchecked: _pointer, count: count &- droppedCount)
736742
}
737743

738744
/// Returns a span containing the final elements of the span,
@@ -755,7 +761,8 @@ extension Span where Element: ~Copyable {
755761
public func _extracting(last maxLength: Int) -> Self {
756762
_precondition(maxLength >= 0, "Can't have a suffix of negative length.")
757763
let newCount = min(maxLength, count)
758-
let newStart = _pointer?.advanced(by: (count&-newCount)*MemoryLayout<Element>.stride)
764+
let offset = (count &- newCount) * MemoryLayout<Element>.stride
765+
let newStart = _pointer?.advanced(by: offset)
759766
return Self(_unchecked: newStart, count: newCount)
760767
}
761768

@@ -778,7 +785,8 @@ extension Span where Element: ~Copyable {
778785
public func _extracting(droppingFirst k: Int) -> Self {
779786
_precondition(k >= 0, "Can't drop a negative number of elements.")
780787
let droppedCount = min(k, count)
781-
let newStart = _pointer?.advanced(by: droppedCount*MemoryLayout<Element>.stride)
782-
return Self(_unchecked: newStart, count: count&-droppedCount)
788+
let offset = droppedCount * MemoryLayout<Element>.stride
789+
let newStart = _pointer?.advanced(by: offset)
790+
return Self(_unchecked: newStart, count: count &- droppedCount)
783791
}
784792
}

0 commit comments

Comments
 (0)