Why does Data.append (Mutable Range Replaceable Random Slice <Data>) add slice.count bytes from the beginning of the base collection?

Using Data.append (a replaceable random access range) , I expected the bytes in the start / end indices of the provided slice to be added to the Data instance. Instead, Slice.count bytes from the beginning of the Slice.base base collection Slice.base . In contrast, creating data instances using a slice results in bytes between the start and end slice indices populating the instance.

 // Swift Playground, Xcode Version 8.3 (8E162) import Foundation var fooData = Data() let barData = Data([0, 1, 2, 3, 4, 5]) let slice = barData.suffix(from: 3) fooData.append(slice) // [0, 1, 2] Data(slice) // [3, 4, 5] 

Is this the expected behavior, and if so, what can help me better understand the behavior of Data.append in this context and its difference from Data.init ?

In addition, given that the docs for the Mutable Range are replaced by Random Access Slice, encourage the use of slices “only for temporary computing”, do Data.init and Data.append refer to the Slice.base collection or create your own copy of bytes?

+7
swift foundation
source share
1 answer

I registered a JIRA question, which is probably the best way to track the possible answer:

https://bugs.swift.org/browse/SR-4473

+1
source share

All Articles