From 51406acd8b48bb7cf0f41231e1249d1bcb5b272f Mon Sep 17 00:00:00 2001 From: zjregee <89204657+zjregee@users.noreply.github.com> Date: Fri, 21 Jan 2022 09:09:11 +0800 Subject: [PATCH] feat: add globMatch (#26) --- Sources/Casbin/Util/MatchFunctions.swift | 60 ++++++++++++++++++- .../UtilsTests/KeyMatchTests.swift | 38 ++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/Sources/Casbin/Util/MatchFunctions.swift b/Sources/Casbin/Util/MatchFunctions.swift index c2cdb2e..05f7a69 100644 --- a/Sources/Casbin/Util/MatchFunctions.swift +++ b/Sources/Casbin/Util/MatchFunctions.swift @@ -102,8 +102,64 @@ extension Util { return ipAddr1 == ipAddr2 } } - //TODO:GlobMatch + // GlobMatch determines whether key1 matches the pattern of key2 using glob pattern public static func globMatch(_ key1:String,_ key2: String) -> Bool { - fatalError("") + let key1Split = key1.split(separator: "/") + var key2Split = key2.split(separator: "/") + if key2Split.count > 0 && key2Split[0] == "*" { + key2Split.removeFirst() + } + if key1Split.count != key2Split.count { + return false + } else { + for i in 0.. Bool { + var s = s + var p = p + while s.count > 0 && p.count > 0 && p[p.index(p.endIndex, offsetBy: -1)] != "*" { + if s[s.index(s.endIndex, offsetBy: -1)] == p[p.index(p.endIndex, offsetBy: -1)] { + s = String(s[s.startIndex..