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

Code before:
            return nil
        }
        if start != nil && end == nil {
            return .start(value: start!)
        }
        if start == nil && end != nil {
            return .tail(value: end!)
        }
        return .within(start: start!, end: end!)
    }
    
    public func serialize() -> String {
        switch self {
            case .start(let value):
                return "\(value)-"
            case .tail(let value):
                return "-\(value)"
            case .within(let start, let end):
                return "\(start)-\(end)"
        }
    }
}

extension RequestedRange {
    
    public func asByteBufferBounds(withMaxSize size: Int) -> (offset: Int64, byteCount: Int) {
        switch self {
            case .start(let value):
                let int64Value = NSNumber(value: value).int64Value
                return (offset: int64Value, byteCount: size-value)
            case .tail(let value):
                let int64Value = NSNumber(value: size-value).int64Value
                return (offset: int64Value, byteCount: value)
            case .within(let start, let end):
                let int64Value = NSNumber(value: start).int64Value
                return (offset: int64Value, byteCount: end + 1)
        }
    }
    
}

extension RequestedRange {
    
    public func asResponseContentRange(limit: Int) -> ResponseContentRange {
        switch self {
            case .start(let start):
                return .withinWithLimit(start: start, end: limit-1, limit: limit)
            case .tail(let end):
                return .withinWithLimit(start: end, end: limit-1, limit: limit)
            case .within(let start, let end):
                return .withinWithLimit(start: start, end: end, limit: limit)

Review comment (suggestion):
We should be fine to just use `numericCast` here when we need to convert Int -> Int64. I'd much rather do that than use `NSNumber` without looking more deeply into the implementation. Who knows what dark magic Foundation is doing. 

Either way, this extension would make more sense next to the FileIO code as a private method. 

Code after:
        case tail(value: Int)
        ///Two integers with single dash in between, e.g. `20-25`
        case within(start: Int, end: Int)
        
        ///Parses a string representing a requested range in one of the following formats:
        ///
        ///- `<range-start>-<range-end>`
        ///- `-<range-end>`
        ///- `<range-start>-`
        ///
        /// - parameters:
        ///     - requestStr: String representing a requested range
        /// - returns: A `HTTPHeaders.Range.Value` if the `requestStr` is valid, `nil` otherwise.
        public static func from<T>(requestStr: T) -> HTTPHeaders.Range.Value? where T: StringProtocol {
            let ranges = requestStr.split(separator: "-", omittingEmptySubsequences: false)
                .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
            let count = ranges.count
            guard count <= 2 else { return nil }
    
            switch (count > 0 ? Int(ranges[0]) : nil, count > 1 ? Int(ranges[1]) : nil) {
            case (nil, nil):
                return nil
            case let (.some(start), nil):
                return .start(value: start)
            case let (nil, .some(tail)):
                return .tail(value: tail)
            case let (.some(start), .some(end)):
                return .within(start: start, end: end)
            }
        }
        
        ///Serializes `HTTPHeaders.Range.Value` to a string for use within the HTTP `Range` header.
        public func serialize() -> String {
            switch self {
            case .start(let value):
                return "\(value)-"
            case .tail(let value):
                return "-\(value)"
            case .within(let start, let end):
                return "\(start)-\(end)"
            }
        }
    }
}

extension HTTPHeaders.ContentRange {
    /// Represents the value of the `Content-Range` request header.
    ///
    /// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range
    public enum Value : Equatable {
        case within(start: Int, end: Int)