Add Pattern.programSize()
and Matcher.programSize()
#180
+90
−1
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.
This PR exposes
Pattern.programSize()
andMatcher.programSize()
public API.The program size represents a very approximate measure of a regexp's "cost". Larger numbers are more expensive than smaller numbers.
Similar to the canonical C++ implementation (see
RE2.ProgramSize()
), re2j will return the program size as the number of instructions of the regex program without making any promises or claims except "larger is more expensive".Context: The need for this change arose from cross-language projects, such as gRPC and CEL. gRPC needs to configure the maximum size of regex programs in CEL to the same number across all languages. While it's possible in gRPC-Cpp via CEL-Cpp
options.regex_max_program_size
that uses re2's ProgramSize, CEL-Java doesn't provide the same configuration option simply because the program size is not available in re2j. In Go, the number of instruction is available in Golen(syntax.Prog.Inst)
.Internal ref: go/grpc-cel-integration
CCs:
@ejona86 (grpc-java), @markdroth (grpc)
@l46kok (cel-java), @TristonianJones (cel)