@@ -28,12 +28,6 @@ function BuiltInFunctions.validateVariadicArgs(expectedLen, args)
2828 end
2929end
3030
31- -- Wrapper for keyMatch
32- function BuiltInFunctions .keyMatchFunc (args )
33- BuiltInFunctions .validateVariadicArgs (2 , args )
34- return BuiltInFunctions .keyMatch (args [1 ], args [2 ])
35- end
36-
3731-- KeyMatch determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
3832-- For example, "/foo/bar" matches "/foo/*"
3933function BuiltInFunctions .keyMatch (key1 , key2 )
@@ -49,10 +43,10 @@ function BuiltInFunctions.keyMatch(key1, key2)
4943 return (key1 == string.sub (key2 , 1 , i - 1 ))
5044end
5145
52- -- Wrapper for keyGet
53- function BuiltInFunctions .keyGetFunc (args )
46+ -- Wrapper for keyMatch
47+ function BuiltInFunctions .keyMatchFunc (args )
5448 BuiltInFunctions .validateVariadicArgs (2 , args )
55- return BuiltInFunctions .keyGet (args [1 ], args [2 ])
49+ return BuiltInFunctions .keyMatch (args [1 ], args [2 ])
5650end
5751
5852-- KeyGet returns the matched part
@@ -72,10 +66,10 @@ function BuiltInFunctions.keyGet(key1, key2)
7266 return " "
7367end
7468
75- -- Wrapper for keyMatch2
76- function BuiltInFunctions .keyMatch2Func (args )
69+ -- Wrapper for keyGet
70+ function BuiltInFunctions .keyGetFunc (args )
7771 BuiltInFunctions .validateVariadicArgs (2 , args )
78- return BuiltInFunctions .keyMatch2 (args [1 ], args [2 ])
72+ return BuiltInFunctions .keyGet (args [1 ], args [2 ])
7973end
8074
8175-- KeyMatch2 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
@@ -87,6 +81,12 @@ function BuiltInFunctions.keyMatch2(key1, key2)
8781 return BuiltInFunctions .regexMatch (key1 , " ^" .. key .. " $" )
8882end
8983
84+ -- Wrapper for keyMatch2
85+ function BuiltInFunctions .keyMatch2Func (args )
86+ BuiltInFunctions .validateVariadicArgs (2 , args )
87+ return BuiltInFunctions .keyMatch2 (args [1 ], args [2 ])
88+ end
89+
9090-- KeyGet2 returns value matched pattern
9191-- For example, "/resource1" matches "/:resource"
9292-- if the pathVar == "resource", then "resource1" will be returned
@@ -117,12 +117,6 @@ function BuiltInFunctions.keyGet2Func(args)
117117 return BuiltInFunctions .keyGet2 (args [1 ], args [2 ],args [3 ])
118118end
119119
120- -- Wrapper for keyMatch3
121- function BuiltInFunctions .keyMatch3Func (args )
122- BuiltInFunctions .validateVariadicArgs (2 , args )
123- return BuiltInFunctions .keyMatch3 (args [1 ], args [2 ])
124- end
125-
126120-- KeyMatch3 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
127121-- For example, "/foo/bar" matches "/foo/*", "/resource1" matches "/{resource}"
128122function BuiltInFunctions .keyMatch3 (key1 , key2 )
@@ -131,10 +125,10 @@ function BuiltInFunctions.keyMatch3(key1, key2)
131125 return BuiltInFunctions .regexMatch (key1 , " ^" .. key .. " $" )
132126end
133127
134- -- Wrapper for keyMatch4
135- function BuiltInFunctions .keyMatch4Func (args )
128+ -- Wrapper for keyMatch3
129+ function BuiltInFunctions .keyMatch3Func (args )
136130 BuiltInFunctions .validateVariadicArgs (2 , args )
137- return BuiltInFunctions .keyMatch4 (args [1 ], args [2 ])
131+ return BuiltInFunctions .keyMatch3 (args [1 ], args [2 ])
138132end
139133
140134-- KeyMatch4 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
@@ -170,10 +164,34 @@ function BuiltInFunctions.keyMatch4(key1, key2)
170164 return true
171165end
172166
173- -- Wrapper for regexMatch
174- function BuiltInFunctions .regexMatchFunc (args )
167+ -- Wrapper for keyMatch4
168+ function BuiltInFunctions .keyMatch4Func (args )
175169 BuiltInFunctions .validateVariadicArgs (2 , args )
176- return BuiltInFunctions .regexMatch (args [1 ], args [2 ])
170+ return BuiltInFunctions .keyMatch4 (args [1 ], args [2 ])
171+ end
172+
173+ -- KeyMatch5 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *
174+ -- For example,
175+ -- - "/foo/bar?status=1&type=2" matches "/foo/bar"
176+ -- - "/parent/child1" and "/parent/child1" matches "/parent/*"
177+ -- - "/parent/child1?status=1" matches "/parent/*"
178+ function BuiltInFunctions .keyMatch5 (key1 , key2 )
179+ local i = string.find (key1 , " ?" , 1 , true )
180+
181+ if i then
182+ key1 = string.sub (key1 , 1 , i - 1 )
183+ end
184+
185+ key2 = string.gsub (key2 , " /%*" , " /.*" )
186+ key2 = string.gsub (key2 , " %{[^/]+%}" , " [^/]+" )
187+
188+ return string.match (key1 , " ^" .. key2 .. " $" ) ~= nil
189+ end
190+
191+ -- Wrapper for keyMatch5
192+ function BuiltInFunctions .keyMatch5Func (args )
193+ BuiltInFunctions .validateVariadicArgs (2 , args )
194+ return BuiltInFunctions .keyMatch5 (args [1 ], args [2 ])
177195end
178196
179197-- RegexMatch determines whether key1 matches the pattern of key2 in regular expression.
@@ -186,6 +204,12 @@ function BuiltInFunctions.regexMatch(key1, key2)
186204 end
187205end
188206
207+ -- Wrapper for regexMatch
208+ function BuiltInFunctions .regexMatchFunc (args )
209+ BuiltInFunctions .validateVariadicArgs (2 , args )
210+ return BuiltInFunctions .regexMatch (args [1 ], args [2 ])
211+ end
212+
189213-- IPMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR pattern.
190214-- For example, "192.168.2.123" matches "192.168.2.0/24"
191215function BuiltInFunctions .IPMatch (ip1 , ip2 )
@@ -232,12 +256,6 @@ function BuiltInFunctions.IPMatchFunc(args)
232256 return BuiltInFunctions .IPMatch (args [1 ], args [2 ])
233257end
234258
235- -- Wrapper for globMatch
236- function BuiltInFunctions .globMatchFunc (args )
237- BuiltInFunctions .validateVariadicArgs (2 , args )
238- return BuiltInFunctions .globMatch (args [1 ], args [2 ])
239- end
240-
241259-- GlobMatch determines whether key1 matches the pattern of key2 using glob pattern
242260function BuiltInFunctions .globMatch (key1 , key2 )
243261 if posix .fnmatch (key2 , key1 , posix .FNM_PATHNAME or posix .FNM_PERIOD ) == 0 then
@@ -247,28 +265,10 @@ function BuiltInFunctions.globMatch(key1, key2)
247265 end
248266end
249267
250- -- Wrapper for keyMatch5
251- function BuiltInFunctions .keyMatch5Func (args )
268+ -- Wrapper for globMatch
269+ function BuiltInFunctions .globMatchFunc (args )
252270 BuiltInFunctions .validateVariadicArgs (2 , args )
253- return BuiltInFunctions .keyMatch5 (args [1 ], args [2 ])
254- end
255-
256- -- KeyMatch5 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *
257- -- For example,
258- -- - "/foo/bar?status=1&type=2" matches "/foo/bar"
259- -- - "/parent/child1" and "/parent/child1" matches "/parent/*"
260- -- - "/parent/child1?status=1" matches "/parent/*"
261- function BuiltInFunctions .keyMatch5 (key1 , key2 )
262- local i = string.find (key1 , " ?" , 1 , true )
263-
264- if i then
265- key1 = string.sub (key1 , 1 , i - 1 )
266- end
267-
268- key2 = string.gsub (key2 , " /%*" , " /.*" )
269- key2 = string.gsub (key2 , " %{[^/]+%}" , " [^/]+" )
270-
271- return string.match (key1 , " ^" .. key2 .. " $" ) ~= nil
271+ return BuiltInFunctions .globMatch (args [1 ], args [2 ])
272272end
273273
274274-- GenerateGFunction is the factory method of the g(_, _) function.
0 commit comments