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

Code before:
    func serialize() -> String {
        return "\(unit.serialize()) \(range.serialize())"
    }
}

public enum ResponseContentRange : Equatable {
    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) })
        guard ranges.count <= 2 else {
            return nil
        }
        if ranges.count == 1 {
            let anyRangeOfSize = ranges[0].split(separator: "/", omittingEmptySubsequences: false)
            guard anyRangeOfSize.count == 2,
                anyRangeOfSize[0] == "*",
                let size = Int(anyRangeOfSize[1]) else {
                    return nil
            }
            return .any(size: size)
        }
        guard let start = Int(ranges[0]) else {
            return nil
        }
        let limits = ranges[1].split(separator: "/", omittingEmptySubsequences: false)
        guard limits.count == 2, let end = Int(limits[0]) else {
            return nil
        }
        if limits[1] == "*" {
            return .within(start: start, end: end)
        }
        guard let limit = Int(limits[1]) else {
            return nil
        }
        return .withinWithLimit(start: start, end: end, limit: limit)
    }
    
    public func serialize() -> String {
        switch self {
            case .any(let size):
                return "*/\(size)"
            case .within(let start, let end):
                return "\(start)-\(end)/*"
            case .withinWithLimit(let start, let end, let limit):
                return "\(start)-\(end)/\(limit)"
        }
    }

Review comment (bug):
I think we need a check on `ranges.count > 0` or `!ranges.isEmpty` otherwise this _could_ crash with an `Index out of bound`

Code after:
        }
        
        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())"
        }
        
    }
    
    /// Convenience for accessing the Content-Range response header.
    ///
    /// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range
    public var contentRange: ContentRange? {
        get {
            return HTTPHeaders.ContentRange(directives: self.parseDirectives(name: .contentRange).flatMap { $0 })
        }
        set {
            if self.contains(name: .contentRange) {
                self.remove(name: .contentRange)
            }
            guard let newValue = newValue else {
                return
            }
            self.add(name: .contentRange, value: newValue.serialize())
        }
    }
    
    /// Convenience for accessing the `Range` request header.
    ///
    /// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
    public var range: Range? {
        get {
            return HTTPHeaders.Range(directives: self.parseDirectives(name: .range).flatMap { $0 })
        }
        set {
            if self.contains(name: .range) {
                self.remove(name: .range)
            }
            guard let newValue = newValue else {
                return