From 2b965523be7f23cf62a1e90479e6f1f2d0ed70a9 Mon Sep 17 00:00:00 2001 From: Andre Karalus Date: Fri, 9 Dec 2022 02:36:55 +0100 Subject: [PATCH] API feature: Get minimum/maximum length of a possible match --- .../com/florianingerl/util/regex/Pattern.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/regex/src/main/java/com/florianingerl/util/regex/Pattern.java b/regex/src/main/java/com/florianingerl/util/regex/Pattern.java index 17d7896..3ba3789 100644 --- a/regex/src/main/java/com/florianingerl/util/regex/Pattern.java +++ b/regex/src/main/java/com/florianingerl/util/regex/Pattern.java @@ -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. *