Skip to content

refactor(api): replace regex literals with compiled patterns for improved performance and readability#6511

Open
balazs-szucs wants to merge 3 commits into
Stirling-Tools:mainfrom
balazs-szucs:pattern-constants
Open

refactor(api): replace regex literals with compiled patterns for improved performance and readability#6511
balazs-szucs wants to merge 3 commits into
Stirling-Tools:mainfrom
balazs-szucs:pattern-constants

Conversation

@balazs-szucs
Copy link
Copy Markdown
Collaborator

Description of Changes

This pull request refactors several utility classes and controllers to replace inline regular expression usage with precompiled Pattern constants. 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)

UI Changes (if applicable)

  • Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR)

Testing (if applicable)

  • I have run task check to verify linters, typechecks, and tests pass
  • I have tested my changes locally. Refer to the Testing Guide for more details.

Copilot AI review requested due to automatic review settings June 1, 2026 17:11
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines ignoring generated files. label Jun 1, 2026
@stirlingbot stirlingbot Bot added Java Pull requests that update Java code Security Security-related issues or pull requests API API-related issues or pull requests refactor Code changes that neither fix a bug nor add a feature labels Jun 1, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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) with Pattern.matcher(...).replaceAll(...), matcher(...).matches(), and Pattern.split(...).
  • Rename SQL normalization helper to sanitizeSql and 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.

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

Labels

API API-related issues or pull requests Java Pull requests that update Java code refactor Code changes that neither fix a bug nor add a feature Security Security-related issues or pull requests size:M This PR changes 30-99 lines ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants