Skip to content

refactor: simplify isValidKeyId complexity using regex#997

Merged
annejan merged 2 commits intomainfrom
fix/isValidKeyId-complexity
Apr 13, 2026
Merged

refactor: simplify isValidKeyId complexity using regex#997
annejan merged 2 commits intomainfrom
fix/isValidKeyId-complexity

Conversation

@nogeenhenk
Copy link
Copy Markdown
Contributor

@nogeenhenk nogeenhenk commented Apr 13, 2026

User description

Summary

  • Reduced cyclomatic complexity of isValidKeyId from 20 to 8
  • Replaced multiple conditional branches with QRegularExpression-based validation
  • All existing tests pass

Changes

  • Strip 0x/0X and <> prefixes with single remove() call
  • Check special formats (@, /, #, &) with one regex match
  • Validate hex string (8-40 chars) with single regex instead of character-by-character loop

Summary by CodeRabbit

  • Refactor
    • Simplified and modernized key-ID validation for more consistent behavior and easier maintenance.
    • Broadened accepted key-ID formats to allow a few common prefix styles while preserving previous angle-bracket handling.
    • Improved overall validation robustness and reduced implementation complexity.

CodeAnt-AI Description

Simplify key ID validation while keeping the same accepted formats

What Changed

  • Key IDs still accept the same prefixed and special-form formats
  • Hex-only key IDs are now checked with a single pattern, covering 8 to 40 characters
  • Empty values remain invalid

Impact

✅ Consistent key ID checks
✅ Fewer invalid key IDs slipping through
✅ Faster key ID validation

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

Reduced cyclomatic complexity from 20 to 8 by replacing multiple
conditional branches with QRegularExpression-based validation.

- Strip 0x/0X and <> prefixes with single remove() call
- Check special formats (@, /, #, &) with one regex match
- Validate hex string with single regex instead of loop
@codeant-ai
Copy link
Copy Markdown
Contributor

codeant-ai bot commented Apr 13, 2026

CodeAnt AI is reviewing your PR.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: 69daf94a-1583-4e69-8764-9cb1f91b88df

📥 Commits

Reviewing files that changed from the base of the PR and between c14cd6a and 44e12f0.

📒 Files selected for processing (1)
  • src/util.cpp

📝 Walkthrough

Walkthrough

Util::isValidKeyId in src/util.cpp was rewritten to replace manual per-character normalization/validation with regex-based checks: it still strips surrounding <...>, removes a leading 0x/0X via regex, treats @///#/& as single-character prefixes, and validates remaining input as 8–40 hex characters or the special-prefix case.

Changes

Cohort / File(s) Summary
Key ID validation
src/util.cpp
Replaced manual normalization and per-char checks with QRegularExpression-based logic. Strips surrounding <...>, removes leading 0x/0X, returns true for single-character prefixes (@, /, #, &) or when input contains @, otherwise enforces full-match hex pattern ^[0-9A-Fa-f]{8,40}$. File ends without a trailing newline.

Sequence Diagram(s)

(Skipped — changes are a focused validation refactor not requiring a multi-component sequence diagram.)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 I hopped through brackets, bits, and "0x" lore,
Swapped tedious checks for regex on the floor,
One-char flags I kept — @, /, #, & —
Hex strings now measured, tidy and neat,
A rabbit cheers: validation complete! 🎉

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main change: refactoring isValidKeyId to reduce complexity using regex instead of manual validation loops.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/isValidKeyId-complexity

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codeant-ai codeant-ai bot added the size:XS This PR changes 0-9 lines, ignoring generated files label Apr 13, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/util.cpp`:
- Around line 284-285: The normalization currently uses
normalized.remove(QRegularExpression("^0[xX]|<|>$")) which fails for inputs like
"<0xABC...>" because the alternatives interfere; update the normalization logic
in src/util.cpp (the variable normalized and its QRegularExpression usage) to
first strip surrounding angle brackets then strip an optional leading 0x/0X (or
use a single regex that matches an optional leading '<' followed by an optional
0x prefix and trailing '>'), so that values like "<0xABC...>" become "ABC..."
and Pass::getRecipientList() won't drop the recipient; adjust the
QRegularExpression accordingly to reliably remove both wrappers in either order.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: 685c65ca-f308-4ad2-9025-ba34ed8e1513

📥 Commits

Reviewing files that changed from the base of the PR and between 7698f81 and c14cd6a.

📒 Files selected for processing (1)
  • src/util.cpp

@codeant-ai
Copy link
Copy Markdown
Contributor

codeant-ai bot commented Apr 13, 2026

CodeAnt AI finished reviewing your PR.

Fixed regex logic error where < was unanchored, removing every < character
anywhere in the string. Now strips surrounding <> first, then removes
optional 0x/0X prefix.
@coveralls
Copy link
Copy Markdown

coveralls commented Apr 13, 2026

Coverage Status

coverage: 20.93% (-0.1%) from 21.034% — fix/isValidKeyId-complexity into main

@annejan annejan mentioned this pull request Apr 13, 2026
@annejan annejan merged commit 74f09dd into main Apr 13, 2026
24 checks passed
@annejan annejan deleted the fix/isValidKeyId-complexity branch April 13, 2026 21:56
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 27.06%. Comparing base (7698f81) to head (44e12f0).
⚠️ Report is 3 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #997      +/-   ##
==========================================
- Coverage   27.17%   27.06%   -0.11%     
==========================================
  Files          39       39              
  Lines        3316     3311       -5     
==========================================
- Hits          901      896       -5     
  Misses       2415     2415              
Flag Coverage Δ
qtpass 27.06% <100.00%> (-0.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants