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

Code before:
        }
        
        public init?(httpHeaders headers: HTTPHeaders) {
            let directives = headers.parseDirectives(name: .range).flatMap { $0 }
            guard !directives.isEmpty, directives[0].parameter != nil else {
                return nil
            }
            let rangeCandidates: [HTTPHeaders.Range.Value] = directives.enumerated()
                .compactMap { (index, value) in
                    if index == 0 {
                        return HTTPHeaders.Range.Value.from(requestStr: value.parameter!)
                    }
                    return HTTPHeaders.Range.Value.from(requestStr: value.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 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 rangeCandidates = rangesStr.split(separator: ",", omittingEmptySubsequences: false)
                .compactMap(HTTPHeaders.Range.Value.from)
            guard !rangeCandidates.isEmpty else {
                return nil
            }
            let lowerCasedUnit = unitStr.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
            self.unit = lowerCasedUnit == "bytes"
                ? RangeUnit.bytes
                : RangeUnit.custom(value: lowerCasedUnit)
            self.ranges = rangeCandidates
        }
        
        public func serialize() -> String {
            return "\(unit.serialize())=\(ranges.map { $0.serialize() }.joined(separator: ", "))"
        }
        
    }
    
    struct ContentRange : Equatable {
        public let unit: RangeUnit
        public let range: HTTPHeaders.ContentRange.Value

Review comment (suggestion):
This type of parsing should be handled by `parseDirectives` as it's non-trivial to get right. For example, your code here isn't taking into consideration quoted strings. 

Code after:
        
        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
        
        init?(directive: HTTPHeaders.Directive) {
            let splitResult = directive.value.split(separator: " ")
            guard splitResult.count == 2 else {
                return nil
            }
            let (unitStr, rangeStr) = (splitResult[0], splitResult[1])
            let lowerCasedUnit = unitStr.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
            guard let contentRange = HTTPHeaders.ContentRange.Value.from(responseStr: rangeStr) else {
                return nil
            }
            self.unit = lowerCasedUnit == "bytes"
                ? RangeUnit.bytes
                : RangeUnit.custom(value: lowerCasedUnit)
            self.range = contentRange
        }