Skip to content

Commit a61c4f2

Browse files
committed
Pattern matcher: isc.py.util.Matcher
1 parent f66f60b commit a61c4f2

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

isc/py/util/Matcher.cls

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/// Utility to match input against comma-separated string of masks.
2+
Class isc.py.util.Matcher
3+
{
4+
5+
/// Returns $$$YES if input matches at least one element from the list
6+
/// input - string to match
7+
/// list - comma separated list of masks containig * and ?
8+
/// write ##class(isc.py.util.Matcher).MatchOr()
9+
ClassMethod MatchOr(input As %String, list As %String) As %Boolean
10+
{
11+
set ok = $$$NO
12+
for pie=1:1:$L(list,",") {
13+
set mask=$P(list,",",pie)
14+
if mask'="",..Match(input,mask) {
15+
set ok = $$$YES
16+
}
17+
}
18+
quit ok
19+
}
20+
21+
/// Returns $$$YES if input matches all elements from the list
22+
/// input - string to match
23+
/// list - comma separated list of masks containig * and ?
24+
/// write ##class(isc.py.util.Matcher).MatchAnd()
25+
ClassMethod MatchAnd(input As %String, list As %String) As %Boolean
26+
{
27+
set ok = $$$YES
28+
for pie=1:1:$L(list,",") {
29+
set mask=$P(list,",",pie)
30+
if mask'="",'..Match(input,mask) {
31+
set ok = $$$NO
32+
}
33+
}
34+
quit ok
35+
}
36+
37+
/// Returns $$$YES if input matches the mask
38+
/// write ##class(isc.py.util.Matcher).Match()
39+
ClassMethod Match(input As %String, mask As %String) As %Boolean [ CodeMode = expression ]
40+
{
41+
input?@..MaskToPattern(mask)
42+
}
43+
44+
/// Translate mask into a pattern
45+
/// write ##class(isc.py.util.Matcher).MaskToPattern()
46+
ClassMethod MaskToPattern(mask As %String) As %String
47+
{
48+
set pattern = ""
49+
set char = ""
50+
for pos = 1:1:$length(mask) {
51+
set curChar = $extract(mask, pos)
52+
if curChar = "*" {
53+
set pattern = pattern _ $select(char="":"", 1:"1"""_char_"""") _ ".E"
54+
set char = ""
55+
} elseif curChar = "?" {
56+
set pattern = pattern _ $select(char="":"", 1:"1"""_char_"""") _ "1E"
57+
set char = ""
58+
} else {
59+
set char = char _ curChar
60+
}
61+
}
62+
set pattern = pattern _ $select(char="":"", 1:"1"""_char_"""")
63+
quit pattern
64+
}
65+
66+
}
67+

0 commit comments

Comments
 (0)