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

Code before:
        
        ///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) }
            guard ranges.count <= 2 else { return nil }
            let start = Int(ranges[0])
            let end = Int(ranges.count > 1 ? ranges[1] : "")
            if start == nil && end == nil {
                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!)
        }
        
        ///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)
        case withinWithLimit(start: Int, end: Int, limit: Int)
        case any(size: Int)

Review comment (suggestion):
This is a way to avoid force unwraps:
```suggestion
            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)
            }
```

Code after:
        ///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)
        case withinWithLimit(start: Int, end: Int, limit: Int)
        case any(size: Int)
        
        ///Parses a string representing a response range in one of the following formats: