Skip to content

Commit

Permalink
Merge pull request #55 from pratiksomangoudar/master
Browse files Browse the repository at this point in the history
Performance improvement in BuiltInFunctions
  • Loading branch information
hsluoyz committed Mar 2, 2020
2 parents 1490daa + 97c06c9 commit 33a93ef
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java
Expand Up @@ -28,6 +28,10 @@
import java.util.regex.Pattern;

public class BuiltInFunctions {

private static Pattern keyMatch2Pattern = Pattern.compile("(.*):[^/]+(.*)");
private static Pattern keyMatch3Pattern = Pattern.compile("(.*)\\{[^/]+\\}(.*)");

/**
* keyMatch determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
* For example, "/foo/bar" matches "/foo/*"
Expand Down Expand Up @@ -58,14 +62,12 @@ public static boolean keyMatch(String key1, String key2) {
*/
public static boolean keyMatch2(String key1, String key2) {
key2 = key2.replace("/*", "/.*");

Pattern p = Pattern.compile("(.*):[^/]+(.*)");
while (true) {
if (!key2.contains("/:")) {
break;
}

key2 = "^" + p.matcher(key2).replaceAll("$1[^/]+$2") + "$";
key2 = "^" + keyMatch2Pattern.matcher(key2).replaceAll("$1[^/]+$2") + "$";
}

return regexMatch(key1, key2);
Expand All @@ -82,13 +84,12 @@ public static boolean keyMatch2(String key1, String key2) {
public static boolean keyMatch3(String key1, String key2) {
key2 = key2.replace("/*", "/.*");

Pattern p = Pattern.compile("(.*)\\{[^/]+\\}(.*)");
while (true) {
if (!key2.contains("/{")) {
break;
}

key2 = p.matcher(key2).replaceAll("$1[^/]+$2");
key2 = keyMatch3Pattern.matcher(key2).replaceAll("$1[^/]+$2");
}

return regexMatch(key1, key2);
Expand Down

0 comments on commit 33a93ef

Please sign in to comment.