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

Code before:
        ///- `<range-start>-<range-end>/<size>`
        ///- `<range-start>-<range-end>/*`
        ///- `*/<size>`
        ///
        /// - parameters:
        ///     - requestStr: String representing the response range
        /// - returns: A `HTTPHeaders.ContentRange.Value` if the `responseStr` is valid, `nil` otherwise.
        public static func from<T>(responseStr: T) -> HTTPHeaders.ContentRange.Value? 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)
        }
        
        ///Serializes `HTTPHeaders.Range.Value` to a string for use within the HTTP `Content-Range` header.
        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 (suggestion):
Again, we're not checking the array size before accessing here. If the array can be of size one or two, the guard at the top should be changed to reflect that

Code after:
        ///- `*/<size>`
        ///
        /// - parameters:
        ///     - requestStr: String representing the response range
        /// - returns: A `HTTPHeaders.ContentRange.Value` if the `responseStr` is valid, `nil` otherwise.
        public static func from<T>(responseStr: T) -> HTTPHeaders.ContentRange.Value? where T : StringProtocol {
            let ranges = responseStr.split(separator: "-", omittingEmptySubsequences: false)
                .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }

            switch ranges.count {
            case 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)
            case 2:
                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)
            default: return nil
            }
        }
        
        ///Serializes `HTTPHeaders.Range.Value` to a string for use within the HTTP `Content-Range` header.
        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)"
            }
        }
    }
}