refactor(api): replace regex literals with compiled patterns for improved performance and readability#6511
Open
balazs-szucs wants to merge 3 commits into
Open
Conversation
…oved performance and readability
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR reduces repeated regex compilation by introducing precompiled Pattern constants and switching from String.replaceAll(...) / String.matches(...) / String.split(...) to Pattern-based operations across storage providers, controllers, and utility/services.
Changes:
- Precompile commonly used regex patterns (control characters, newlines, whitespace, comments, share-link URI, non-alphanumerics) as
static final Pattern. - Replace inline regex calls (
replaceAll,matches,split) withPattern.matcher(...).replaceAll(...),matcher(...).matches(), andPattern.split(...). - Rename SQL normalization helper to
sanitizeSqland apply precompiled patterns for script sanitization.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| app/proprietary/src/main/java/stirling/software/proprietary/storage/provider/S3StorageProvider.java | Precompile control-character regex and reuse it for filename/header sanitization. |
| app/proprietary/src/main/java/stirling/software/proprietary/storage/provider/LocalStorageProvider.java | Same control-character pattern optimization for local filename sanitization. |
| app/proprietary/src/main/java/stirling/software/proprietary/service/PdfCommentAgentOrchestrator.java | Precompile newline pattern for safer filename handling. |
| app/proprietary/src/main/java/stirling/software/proprietary/security/service/DatabaseService.java | Precompile comment/whitespace patterns and apply them in SQL sanitization; rename helper. |
| app/proprietary/src/main/java/stirling/software/proprietary/controller/api/PdfCommentAgentController.java | Precompile newline pattern for safe logging/filename handling. |
| app/proprietary/src/main/java/stirling/software/proprietary/controller/api/MathAuditorAgentController.java | Precompile newline pattern for safe logging/filename handling. |
| app/common/src/main/java/stirling/software/common/util/RequestUriUtils.java | Precompile share-link URI regex and use matcher().matches(). |
| app/common/src/main/java/stirling/software/common/util/PdfTextLocator.java | Precompile non-alphanumeric regex used in normalization. |
| app/common/src/main/java/stirling/software/SPDF/pdf/parser/LineAlignmentTableParser.java | Precompile whitespace split pattern for tokenization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Changes
This pull request refactors several utility classes and controllers to replace inline regular expression usage with precompiled
Patternconstants. This change improves performance, consistency, and maintainability by ensuring that regex patterns are compiled only once and reused throughout the codebase. Additionally, it enhances code clarity and security in filename and SQL content sanitization.Checklist
General
Documentation
Translations (if applicable)
scripts/counter_translation.pyUI Changes (if applicable)
Testing (if applicable)
task checkto verify linters, typechecks, and tests pass