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

Code before:
            : 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) {
        let splitResult = header.split(separator: " ", omittingEmptySubsequences: false)
        guard splitResult.count == 2 else {
            return nil
        }
        let (unitStr, rangeStr) = (splitResult[0], splitResult[1])
        let lowerCasedUnit = unitStr.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
        self.unit = lowerCasedUnit == "bytes"
            ? RangeUnit.bytes
            : RangeUnit.custom(value: lowerCasedUnit)
        guard let contentRange = ResponseContentRange.from(responseStr: rangeStr) else {
            return nil
        }
        self.range = contentRange
    }
    
    func asHeaderString() -> String {
        return "\(unit.asHeaderString()) \(range.asHeaderString())"
    }
}

public enum ResponseContentRange {
    case within(start: Int, end: Int)
    case withinWithLimit(start: Int, end: Int, limit: Int)
    case any(size: Int)
    
    public static func from<T>(responseStr: T) -> ResponseContentRange? where T : StringProtocol {
        let ranges = responseStr.split(separator: "-", omittingEmptySubsequences: false)
            .map({ $0.trimmingCharacters(in: .whitespacesAndNewlines) })
        if ranges.count > 2 {
            return nil
        }

Review comment (refactor):
Please move this to the line right after you assign `rangeStr`

Code after:
            }
            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
        }
        
        public init(unit: RangeUnit, range: HTTPHeaders.ContentRange.Value) {
            self.unit = unit
            self.range = range
        }
        
        init?(directives: [Directive]) {
            guard directives.count == 1 else {
                return nil
            }
            self.init(directive: directives[0])
        }
        
        public func serialize() -> String {
            return "\(unit.serialize()) \(range.serialize())"
        }