Skip to content

Implement appendTail and appendReplacement methods in Matcher - #590

Open
Jitmisra wants to merge 1 commit into
javapathfinder:masterfrom
Jitmisra:feature/matcher-append-methods
Open

Implement appendTail and appendReplacement methods in Matcher#590
Jitmisra wants to merge 1 commit into
javapathfinder:masterfrom
Jitmisra:feature/matcher-append-methods

Conversation

@Jitmisra

@Jitmisra Jitmisra commented Jan 18, 2026

Copy link
Copy Markdown

#589

Summary

Implements the two missing appendTail() and appendReplacement() methods in JPF's Matcher model class. These were marked as TODO in the codebase.

Problem

Programs using the standard Java regex find-and-replace pattern could not be verified by JPF:

while (m.find()) {
    m.appendReplacement(sb, "replacement");
}
m.appendTail(sb);

Calling these methods would fail because they weren't implemented.

Solution

Files Changed:

File Description
src/classes/.../java/util/regex/Matcher.java Added native method declarations with Javadoc
src/peers/.../JPF_java_util_regex_Matcher.java Implemented native peer methods
src/tests/.../regex/MatcherAppendTest.java Added 5 unit tests

Implementation Details

The tricky part was handling Java 11+ compact strings. Unlike Java 8 where StringBuffer uses char[], Java 11+ uses:

  • byte[] for storage (compact representation)
  • coder field: 0 = LATIN1, 1 = UTF16

The implementation:

  1. Delegates to the real host JVM Matcher.appendReplacement/appendTail
  2. Converts the result back to JPF's StringBuffer format
  3. Handles encoding upgrades (LATIN1 → UTF16) when needed

Tests Added

  • testAppendReplacementAndTail - Multiple replacements
  • testAppendReplacementSingleMatch - Single match
  • testAppendTailNoMatch - No matches scenario
  • testAppendReplacementWithGroupReference - Using $1 groups
  • testAppendReplacementEmptyString - Empty replacement

Verification

./gradlew test
BUILD SUCCESSFUL
parallelTest: 1019 passed, 0 failed
singleThreadTest: 16 passed, 0 failed

@Jitmisra
Jitmisra force-pushed the feature/matcher-append-methods branch from 79f6551 to 94b3192 Compare March 25, 2026 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant