Skip to content

Commit

Permalink
Replace NSRegularExpression with Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Mar 19, 2024
1 parent 739dd1e commit f86dd32
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions Sources/Hummingbird/Files/FileMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Foundation
import HTTPTypes
import HummingbirdCore
import Logging
import NIOCore
import NIOPosix
Expand Down Expand Up @@ -229,37 +230,27 @@ extension FileMiddleware {
///
/// Also supports open ended ranges
private func getRangeFromHeaderValue(_ header: String) -> ClosedRange<Int>? {
let groups = self.matchRegex(header, expression: "^bytes=([\\d]*)-([\\d]*)$")
guard groups.count == 3 else { return nil }

if groups[1] == "" {
guard let upperBound = Int(groups[2]) else { return nil }
return 0...upperBound
} else if groups[2] == "" {
guard let lowerBound = Int(groups[1]) else { return nil }
return lowerBound...Int.max
} else {
guard let lowerBound = Int(groups[1]),
let upperBound = Int(groups[2]) else { return nil }
return lowerBound...upperBound
}
}

private func matchRegex(_ string: String, expression: String) -> [Substring] {
let nsRange = NSRange(string.startIndex..<string.endIndex, in: string)
guard let regularExpression = try? NSRegularExpression(pattern: expression, options: []),
let firstMatch = regularExpression.firstMatch(in: string, range: nsRange)
else {
return []
}
do {
var parser = Parser(header)
guard try parser.read("bytes=") else { return nil }
let lower = parser.read { $0.properties.numericType == .decimal }.string
guard try parser.read("-") else { return nil }
let upper = parser.read { $0.properties.numericType == .decimal }.string

var groups: [Substring] = []
groups.reserveCapacity(firstMatch.numberOfRanges)
for i in 0..<firstMatch.numberOfRanges {
guard let range = Range(firstMatch.range(at: i), in: string) else { continue }
groups.append(string[range])
if lower == "" {
guard let upperBound = Int(upper) else { return nil }
return 0...upperBound
} else if upper == "" {
guard let lowerBound = Int(lower) else { return nil }
return lowerBound...Int.max
} else {
guard let lowerBound = Int(lower),
let upperBound = Int(upper) else { return nil }
return lowerBound...upperBound
}
} catch {
return nil
}
return groups
}

private func createETag(_ strings: [String]) -> String {
Expand Down

0 comments on commit f86dd32

Please sign in to comment.