Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Using Data.append(Mutable​Range​Replaceable​Random​Access​Slice), I expected the bytes within the start/end indexes of the provided slice to be appended onto the Data instance. Instead, it appears Slice.count bytes from the beginning of the Slice.base underlying collection are appended. In contrast, instantiating Data with a slice results in the bytes between the slice's start and end indexes 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 might help me better understand the behavior of Data.append in this context, and its differences from Data.init?

Additionally, given that the docs for Mutable​Range​Replaceable​Random​Access​Slice encourage using slices "only for transient computation", do Data.init and Data.append reference the Slice.base collection or create their own copy of the bytes?

like image 945
leppert Avatar asked Apr 02 '17 18:04

leppert


1 Answers

I've filed a JIRA issue, which is likely the best place to continue tracking a possible answer:

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

like image 116
leppert Avatar answered Nov 15 '22 10:11

leppert