Skip to content

Commit

Permalink
Eliminate hasPrefix usage for pseudo HTTP header names (#87)
Browse files Browse the repository at this point in the history
UTF8 view is faster than hasPrefix

rdar://144395951
  • Loading branch information
guoye-zhang authored Feb 16, 2025
1 parent 99d1c41 commit 4bd2f81
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Sources/HTTPTypes/HTTPFieldName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension HTTPField {
return nil
}
let token: Substring
if name.hasPrefix(":") {
if name.utf8.first == UInt8(ascii: ":") {
token = name.dropFirst()
} else {
token = Substring(name)
Expand Down Expand Up @@ -90,7 +90,7 @@ extension HTTPField {
}

var isPseudo: Bool {
self.rawName.hasPrefix(":")
self.rawName.utf8.first == UInt8(ascii: ":")
}
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ extension HTTPField.Name: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let nameString = try container.decode(String.self)
if nameString.hasPrefix(":") {
if nameString.utf8.first == UInt8(ascii: ":") {
guard nameString.lowercased() == nameString,
HTTPField.isValidToken(nameString.dropFirst())
else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/HTTPTypes/HTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ extension HTTPRequest.PseudoHeaderFields: Codable {
}
extendedConnectProtocol = field
default:
guard field.name.rawName.hasPrefix(":") else {
guard field.name.rawName.utf8.first == UInt8(ascii: ":") else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\"\(field)\" is not a pseudo header field"
Expand Down
2 changes: 1 addition & 1 deletion Sources/HTTPTypes/HTTPResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ extension HTTPResponse.PseudoHeaderFields: Codable {
}
status = field
default:
guard field.name.rawName.hasPrefix(":") else {
guard field.name.rawName.utf8.first == UInt8(ascii: ":") else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\"\(field)\" is not a pseudo header field"
Expand Down

0 comments on commit 4bd2f81

Please sign in to comment.