Skip to content

Commit 8d06ca7

Browse files
committed
Prefer std::copy_n over std::copy where appropriate.
std::copy_n saves us from having to do the addition manually.
1 parent 3e79583 commit 8d06ca7

File tree

6 files changed

+13
-13
lines changed

6 files changed

+13
-13
lines changed

include/swift/Basic/StableHasher.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ class StableHasher final {
122122
if (nhead == sizeof(byteBuffer)) {
123123
// We have headroom available for all 64 bits. Eagerly compress the
124124
// now-full buffer into our state.
125-
std::copy(bits, bits + sizeof(byteBuffer), byteBuffer);
125+
std::copy_n(bits, sizeof(byteBuffer), byteBuffer);
126126
} else if (N >= available) {
127127
// There was some excess - append as many bytes as we can hold and
128128
// compress the buffer into our state.
129-
std::copy(bits, bits + nhead, byteBuffer + bufLen);
129+
std::copy_n(bits, nhead, byteBuffer + bufLen);
130130
} else {
131131
// We have headroom available for these bits.
132-
std::copy(bits, bits + N, byteBuffer + bufLen);
132+
std::copy_n(bits, N, byteBuffer + bufLen);
133133
return setBufferLength(bufLen + N);
134134
}
135135

@@ -138,7 +138,7 @@ class StableHasher final {
138138

139139
// Now reseed the buffer with the remaining bytes.
140140
const uint64_t remainder = N - available;
141-
std::copy(bits + available, bits + N, byteBuffer);
141+
std::copy_n(bits + available, remainder, byteBuffer);
142142
return setBufferLength(remainder);
143143
}
144144

lib/AST/RequirementMachine/Term.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ MutableTerm::compare(const MutableTerm &other, RewriteContext &ctx) const {
194194
/// Replace the subterm in the range [from,to) of this term with \p rhs.
195195
void MutableTerm::rewriteSubTerm(Symbol *from, Symbol *to, Term rhs) {
196196
auto oldSize = size();
197-
unsigned lhsLength = (unsigned)(to - from);
197+
size_t lhsLength = (size_t)(to - from);
198198

199199
if (lhsLength == rhs.size()) {
200200
// Copy the RHS to the LHS.
@@ -213,7 +213,7 @@ void MutableTerm::rewriteSubTerm(Symbol *from, Symbol *to, Term rhs) {
213213
assert(lhsLength < rhs.size());
214214

215215
// Copy the LHS-sized prefix of RHS to the LHS.
216-
auto newTo = std::copy(rhs.begin(), rhs.begin() + lhsLength, from);
216+
auto newTo = std::copy_n(rhs.begin(), lhsLength, from);
217217
assert(newTo == to);
218218

219219
// Insert the remainder of the RHS term.

stdlib/include/llvm/ADT/SmallVector.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ SmallVectorImpl<T> &SmallVectorImpl<T>::
983983
// Assign common elements.
984984
iterator NewEnd;
985985
if (RHSSize)
986-
NewEnd = std::copy(RHS.begin(), RHS.begin()+RHSSize, this->begin());
986+
NewEnd = std::copy_n(RHS.begin(), RHSSize, this->begin());
987987
else
988988
NewEnd = this->begin();
989989

@@ -1005,7 +1005,7 @@ SmallVectorImpl<T> &SmallVectorImpl<T>::
10051005
this->grow(RHSSize);
10061006
} else if (CurSize) {
10071007
// Otherwise, use assignment for the already-constructed elements.
1008-
std::copy(RHS.begin(), RHS.begin()+CurSize, this->begin());
1008+
std::copy_n(RHS.begin(), CurSize, this->begin());
10091009
}
10101010

10111011
// Copy construct the new elements in place.

stdlib/public/LLVMSupport/SmallPtrSet.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize,
192192
if (RHS.isSmall()) {
193193
// Copy a small RHS rather than moving.
194194
CurArray = SmallArray;
195-
std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, CurArray);
195+
std::copy_n(RHS.CurArray, RHS.NumNonEmpty, CurArray);
196196
} else {
197197
CurArray = RHS.CurArray;
198198
RHS.CurArray = RHS.SmallArray;
@@ -228,7 +228,7 @@ void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
228228
// from LHS to RHS.
229229
if (!this->isSmall() && RHS.isSmall()) {
230230
assert(RHS.CurArray == RHS.SmallArray);
231-
std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray);
231+
std::copy_n(RHS.CurArray, RHS.NumNonEmpty, this->SmallArray);
232232
std::swap(RHS.CurArraySize, this->CurArraySize);
233233
std::swap(this->NumNonEmpty, RHS.NumNonEmpty);
234234
std::swap(this->NumTombstones, RHS.NumTombstones);
@@ -241,7 +241,7 @@ void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) {
241241
// from RHS to LHS.
242242
if (this->isSmall() && !RHS.isSmall()) {
243243
assert(this->CurArray == this->SmallArray);
244-
std::copy(this->CurArray, this->CurArray + this->NumNonEmpty,
244+
std::copy_n(this->CurArray, this->NumNonEmpty,
245245
RHS.SmallArray);
246246
std::swap(RHS.CurArraySize, this->CurArraySize);
247247
std::swap(RHS.NumNonEmpty, this->NumNonEmpty);

stdlib/toolchain/Compatibility51/Concurrent.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ template <class ElemTy> struct ConcurrentReadableArray {
434434
auto newCapacity = std::max((size_t)16, count * 2);
435435
auto *newStorage = Storage::allocate(newCapacity);
436436
if (storage) {
437-
std::copy(storage->data(), storage->data() + count, newStorage->data());
437+
std::copy_n(storage->data(), count, newStorage->data());
438438
newStorage->Count.store(count, std::memory_order_relaxed);
439439
FreeList.push_back(storage);
440440
}

tools/swift-stdlib-tool/swift-stdlib-tool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ std::vector<uint8_t> readToEOF(int fd) {
760760
ssize_t readSize = 0;
761761
while ((readSize = read(fd, readBuffer, BUFFER_SIZE)) > 0) {
762762
retData.reserve(retData.size() + readSize);
763-
std::copy(readBuffer, readBuffer + readSize, std::back_inserter(retData));
763+
std::copy_n(readBuffer, readSize, std::back_inserter(retData));
764764
}
765765
return retData;
766766
}

0 commit comments

Comments
 (0)