Skip to content

Commit 285984e

Browse files
committed
[stdlib] simplify update(fromContentsOf:)
- one fewer variable, one fewer return point
1 parent d24b749 commit 285984e

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -818,26 +818,27 @@ extension Unsafe${Mutable}BufferPointer {
818818
return startIndex.advanced(by: count)
819819
}
820820

821-
guard var position = baseAddress else {
821+
if self.isEmpty {
822822
_precondition(
823823
source.isEmpty,
824824
"buffer cannot contain every element from source."
825825
)
826826
return startIndex
827827
}
828+
_internalInvariant(_position != nil)
828829
var iterator = source.makeIterator()
829-
for index in indices {
830-
guard let value = iterator.next() else {
831-
return index
830+
var index = startIndex
831+
while let value = iterator.next() {
832+
guard index < endIndex else {
833+
_preconditionFailure(
834+
"buffer cannot contain every element from source."
835+
)
836+
break
832837
}
833-
position.pointee = value
834-
position = position.advanced(by: 1)
838+
_position._unsafelyUnwrappedUnchecked[index] = value
839+
formIndex(after: &index)
835840
}
836-
_precondition(
837-
iterator.next() == nil,
838-
"buffer cannot contain every element from source."
839-
)
840-
return endIndex
841+
return index
841842
}
842843

843844
/// Moves every element of an initialized source buffer into the

0 commit comments

Comments
 (0)