Skip to content

API feature: Get minimum/maximum length of a possible match #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions regex/src/main/java/com/florianingerl/util/regex/Pattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,41 @@ void setNext(Node a) {
*/
private transient boolean hasSupplementary;

/**
* The TreeInfo of the complete object tree.
*/
private transient volatile TreeInfo treeInfoRoot;

private TreeInfo getTreeInfoRoot() {
// double checked locking
TreeInfo treeInfo = treeInfoRoot;
if (treeInfo == null) {
synchronized (this) {
treeInfo = treeInfoRoot;
if (treeInfo == null) {
matchRoot.study(treeInfo = new TreeInfo());
treeInfoRoot = treeInfo;
}
}
}
return treeInfo;
}

/**
* @return The minimum length of a possible match.
*/
public int getMinLength() {
return getTreeInfoRoot().minLength;
}

/**
* @return The maximum length of a possible match or {@link Integer.MAX_VALUE} if there is no maximum.
*/
public int getMaxLength() {
TreeInfo treeInfo = getTreeInfoRoot();
return treeInfo.maxValid ? treeInfo.maxLength : Integer.MAX_VALUE;
}

/**
* Compiles the given regular expression into a pattern.
*
Expand Down