| 
 | 1 | +//===----------------------------------------------------------------------===//  | 
 | 2 | +//  | 
 | 3 | +// This source file is part of the Swift.org open source project  | 
 | 4 | +//  | 
 | 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors  | 
 | 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception  | 
 | 7 | +//  | 
 | 8 | +// See https://swift.org/LICENSE.txt for license information  | 
 | 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors  | 
 | 10 | +//  | 
 | 11 | +//===----------------------------------------------------------------------===//  | 
 | 12 | + | 
 | 13 | +extension Data {  | 
 | 14 | +    /// An iterator over the contents of the data.  | 
 | 15 | +    ///  | 
 | 16 | +    /// The iterator will increment byte-by-byte.  | 
 | 17 | +    @inlinable // This is @inlinable as trivially computable.  | 
 | 18 | +    public func makeIterator() -> Data.Iterator {  | 
 | 19 | +        return Iterator(self, at: startIndex)  | 
 | 20 | +    }  | 
 | 21 | +      | 
 | 22 | +    public struct Iterator : IteratorProtocol, Sendable {  | 
 | 23 | +        @usableFromInline  | 
 | 24 | +        internal typealias Buffer = (  | 
 | 25 | +            UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,  | 
 | 26 | +            UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,  | 
 | 27 | +            UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,  | 
 | 28 | +            UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)  | 
 | 29 | +          | 
 | 30 | +        @usableFromInline internal let _data: Data  | 
 | 31 | +        @usableFromInline internal var _buffer: Buffer  | 
 | 32 | +        @usableFromInline internal var _idx: Data.Index  | 
 | 33 | +        @usableFromInline internal let _endIdx: Data.Index  | 
 | 34 | +          | 
 | 35 | +        @usableFromInline // This is @usableFromInline as a non-trivial initializer.  | 
 | 36 | +        internal init(_ data: Data, at loc: Data.Index) {  | 
 | 37 | +            // The let vars prevent this from being marked as @inlinable  | 
 | 38 | +            _data = data  | 
 | 39 | +            _buffer = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)  | 
 | 40 | +            _idx = loc  | 
 | 41 | +            _endIdx = data.endIndex  | 
 | 42 | +              | 
 | 43 | +            let bufferSize = MemoryLayout<Buffer>.size  | 
 | 44 | +            Swift.withUnsafeMutableBytes(of: &_buffer) {  | 
 | 45 | +                $0.withMemoryRebound(to: UInt8.self) { [endIndex = data.endIndex] buf in  | 
 | 46 | +                    let bufferIdx = (loc - data.startIndex) % bufferSize  | 
 | 47 | +                    let end = (endIndex - (loc - bufferIdx) > bufferSize) ? (loc - bufferIdx + bufferSize) : endIndex  | 
 | 48 | +                    data.copyBytes(to: buf, from: (loc - bufferIdx)..<end)  | 
 | 49 | +                }  | 
 | 50 | +            }  | 
 | 51 | +        }  | 
 | 52 | +          | 
 | 53 | +        public mutating func next() -> UInt8? {  | 
 | 54 | +            let idx = _idx  | 
 | 55 | +            let bufferSize = MemoryLayout<Buffer>.size  | 
 | 56 | +              | 
 | 57 | +            guard idx < _endIdx else { return nil }  | 
 | 58 | +            _idx += 1  | 
 | 59 | +              | 
 | 60 | +            let bufferIdx = (idx - _data.startIndex) % bufferSize  | 
 | 61 | +              | 
 | 62 | +              | 
 | 63 | +            if bufferIdx == 0 {  | 
 | 64 | +                var buffer = _buffer  | 
 | 65 | +                Swift.withUnsafeMutableBytes(of: &buffer) {  | 
 | 66 | +                    $0.withMemoryRebound(to: UInt8.self) {  | 
 | 67 | +                        // populate the buffer  | 
 | 68 | +                        _data.copyBytes(to: $0, from: idx..<(_endIdx - idx > bufferSize ? idx + bufferSize : _endIdx))  | 
 | 69 | +                    }  | 
 | 70 | +                }  | 
 | 71 | +                _buffer = buffer  | 
 | 72 | +            }  | 
 | 73 | +              | 
 | 74 | +            return Swift.withUnsafeMutableBytes(of: &_buffer) {  | 
 | 75 | +                $0.load(fromByteOffset: bufferIdx, as: UInt8.self)  | 
 | 76 | +            }  | 
 | 77 | +        }  | 
 | 78 | +    }  | 
 | 79 | +}  | 
0 commit comments