Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove expsensive regex for hex parsing #116

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions Sources/UBUserInterface/UIColor/UIColor+HEX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,19 @@ extension UIColor {
///
/// - Parameter hex: The raw hex string
public convenience init?(ub_hexString hex: String) {
let input = hex.trimmingCharacters(in: .whitespaces)
// We want to crash if the regex cannot be formed. Error from the Framework that needs an update
let hexStringRegex = try! NSRegularExpression(pattern: "^\\#?([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$", options: .caseInsensitive)
let input = hex.trimmingCharacters(in: .whitespacesAndNewlines)

guard let hexStringResult = hexStringRegex.firstMatch(in: input, range: NSRange(input.startIndex..., in: input)) else {
return nil
}
// Remove optional '#' prefix
let hexString = input.hasPrefix("#") ? String(input.dropFirst()) : input

guard hexStringResult.numberOfRanges == 2,
let hexCapturedRange = Range(hexStringResult.range(at: 1), in: input) else {
// Validate length (3, 4, 6, or 8)
guard [3, 4, 6, 8].contains(hexString.count),
hexString.allSatisfy({ $0.isHexDigit }) else {
return nil
}

let hexString = String(input[hexCapturedRange])
var hexValue: UInt32 = 0

guard Scanner(string: hexString).scanHexInt32(&hexValue) else {
var hexValue: UInt64 = 0
guard Scanner(string: hexString).scanHexInt64(&hexValue) else {
return nil
}

Expand All @@ -84,11 +80,11 @@ extension UIColor {
case 4:
self.init(hex4: UInt16(hexValue))
case 6:
self.init(hex6: hexValue)
self.init(hex6: UInt32(hexValue))
case 8:
self.init(hex8: hexValue)
self.init(hex8: UInt32(hexValue))
default:
fatalError("Should not be able to get other then 3-4-6-8 hex. Check regex")
fatalError("Unexpected hex string length. This should not occur.")
Comment on lines +83 to +87
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider replacing fatalError with nil return.

While the validation ensures this case should never occur, using fatalError in an initializer might be too aggressive. Consider returning nil instead to maintain the failable initializer pattern.

             case 8:
                 self.init(hex8: UInt32(hexValue))
             default:
-                fatalError("Unexpected hex string length. This should not occur.")
+                return nil // This case is prevented by the guard statement above
         }

Committable suggestion skipped: line range outside the PR's diff.

}
}

Expand Down
Loading