> On Jun 26, 2017, at 14:05 , Philippe Hausler <phaus...@apple.com> wrote: > > > >> On Jun 26, 2017, at 1:47 PM, Roderick Mann <rm...@latencyzero.com> wrote: >> >>> >>> On Jun 26, 2017, at 10:20 , Charles Srstka <cocoa...@charlessoft.com> wrote: >>> >>> Rats, I was hoping that one of the reasons about being so explicit what >>> we’re going to access and where with bindMemory() and friends would be to >>> take care of these sorts of issues. >>> >>> In that case, the simplest way to do it is probably just this: >>> >>> let crc = (UInt16(myData[myData.endIndex]) << 8) | >>> UInt16(myData[myData.endIndex - 1]) >> >> By the way, self.endIndex == self.count, so shouldn't these both have an >> additional 1 subtracted? >> >> That's what I'm seeing, and what the docs show. What's the point of >> endIndex? Completeness? > > If the data was sliced. > > e.g. > > let d = Data(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) > let slice = d[2..<4] > > slice.endIndex != slice.count
Ohhh, yeah, gotcha. Now I know something new about slices. Thanks (and thanks, Charles, for your expanded explanation)! > >> >>> >>> (or the reverse, depending on the endianness of the source data) >>> >>> Charles >>> >>>> On Jun 26, 2017, at 12:05 PM, Philippe Hausler via swift-users >>>> <swift-users@swift.org> wrote: >>>> >>>> Data.copyBytes will do that under the hood >>>> >>>> var crc: UInt16 = 0 >>>> let amountCopied = withUnsafeMutablePointer(to: &crc) { data.copyBytes(to: >>>> UnsafeMutableBufferPointer(start: $0, count: 1)) } >>>> if amountCopied == MemoryLayout<UInt16>.size { >>>> // we have a full crc >>>> } >>>> >>>> That will probably do what you want; plus it will allow you to do it from >>>> a given range of bytes. >>>> >>>> >>>>> On Jun 26, 2017, at 9:57 AM, Joe Groff via swift-users >>>>> <swift-users@swift.org> wrote: >>>>> >>>>> >>>>>> On Jun 26, 2017, at 1:55 AM, Daniel Vollmer via swift-users >>>>>> <swift-users@swift.org> wrote: >>>>>> >>>>>> Hi Rick, >>>>>> >>>>>>> On 26. Jun 2017, at 02:37, Rick Mann via swift-users >>>>>>> <swift-users@swift.org> wrote: >>>>>> >>>>>> [snip] >>>>>> >>>>>>> I'd also like to avoid unnecessary copying of the data. All of it is >>>>>>> immutable for the purposes of this problem. >>>>>>> >>>>>>> How can I get the UInt16 that starts at byte X in a Data? Same goes for >>>>>>> Double or Int32 or whatever. >>>>>> >>>>>> I’m not sure what Swift’s stance on this is, but not all platforms allow >>>>>> misaligned memory accesses (such as your attempt to access a UInt16 that >>>>>> lies at an odd memory address). >>>>> >>>>> Unaligned memory accesses are not currently allowed by the language >>>>> semantics, regardless of the underlying ISA. You should use memcpy if you >>>>> need to load potentially-unaligned values out of raw memory. >>>>> >>>>> -Joe >>>>> _______________________________________________ >>>>> swift-users mailing list >>>>> swift-users@swift.org >>>>> https://lists.swift.org/mailman/listinfo/swift-users >>>> >>> >> >> >> -- >> Rick Mann >> rm...@latencyzero.com > -- Rick Mann rm...@latencyzero.com _______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users