Repository: vapor/vapor
File: Sources/Vapor/HTTP/Headers/HTTPHeaders+ContentRange.swift (Swift)

Code before:
            case .bytes:
                return "bytes"
            case .custom(let value):
                return value
        }
    }
}

public struct RangeHeader {
    public let unit: RangeUnit
    public let ranges: [RequestedRange]
    
    public init(unit: RangeUnit, ranges: [RequestedRange]) {
        self.unit = unit
        self.ranges = ranges
    }
    
    public init?(requestHeader header: String) {
        let splitResult = header.split(separator: "=", omittingEmptySubsequences: false)
        guard splitResult.count == 2 else {
            return nil
        }
        let (unitStr, rangesStr) = (splitResult[0], splitResult[1])
        let ranges = rangesStr.split(separator: ",", omittingEmptySubsequences: false)
            .map { it in RequestedRange.from(requestStr: it) }
        if ranges.filter({ $0 == nil }).count > 0 {
            return nil
        }
        let lowerCasedUnit = unitStr.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
        self.unit = lowerCasedUnit == "bytes"
            ? RangeUnit.bytes
            : RangeUnit.custom(value: lowerCasedUnit)
        self.ranges = ranges.compactMap { $0 }
    }
    
    func asHeaderString() -> String {
        return "\(unit.asHeaderString())=\(ranges.map { $0.asHeaderString() }.joined(separator: ", "))"
    }
    
}

public struct ContentRangeHeader {
    public let unit: RangeUnit
    public let range: ResponseContentRange
    
    public init(unit: RangeUnit, range: ResponseContentRange) {
        self.unit = unit
        self.range = range
    }
    
    public init?(responseHeader header: String) {

Review comment (performance):
Please use `!ranges.filter({ $0 == nil}).isEmpty` as it's more efficient.

Code after:
        
        public func serialize() -> String {
            switch self {
            case .bytes:
                return "bytes"
            case .custom(let value):
                return value
            }
        }
    }
    
    /// Represents the HTTP `Range` request header.
    /// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
    public struct Range: Equatable {
        public let unit: RangeUnit
        public let ranges: [HTTPHeaders.Range.Value]
        
        public init(unit: RangeUnit, ranges: [HTTPHeaders.Range.Value]) {
            self.unit = unit
            self.ranges = ranges
        }
        
        init?(directives: [HTTPHeaders.Directive]) {
            let rangeCandidates: [HTTPHeaders.Range.Value] = directives.enumerated().compactMap {
                if $0.0 == 0, let parameter = $0.1.parameter {
                    return HTTPHeaders.Range.Value.from(requestStr: parameter)
                }
                return HTTPHeaders.Range.Value.from(requestStr: $0.1.value)
            }
            guard !rangeCandidates.isEmpty else {
                return nil
            }
            self.ranges = rangeCandidates
            let lowerCasedUnit = directives[0].value.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
            self.unit = lowerCasedUnit == "bytes"
                ? RangeUnit.bytes
                : RangeUnit.custom(value: lowerCasedUnit)
        }
        
        public func serialize() -> String {
            return "\(unit.serialize())=\(ranges.map { $0.serialize() }.joined(separator: ", "))"
        }
    }
    
    /// Represents the HTTP `Content-Range` response header.
    ///
    /// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range
    public struct ContentRange: Equatable {
        public let unit: RangeUnit
        public let range: HTTPHeaders.ContentRange.Value