-
Notifications
You must be signed in to change notification settings - Fork 0
/
width.go
96 lines (75 loc) · 1.93 KB
/
width.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package hre
import (
"regexp/syntax"
)
// RegexpMaxWidth calculates the maximum width of a parsed Regexp pattern.
func RegexpMaxWidth(re *syntax.Regexp) int {
switch re.Op {
case syntax.OpNoMatch, syntax.OpEmptyMatch:
// matches no strings
// matches empty string
return 0
case syntax.OpLiteral:
// matches Runes sequence
return len(re.Rune)
case syntax.OpCharClass, syntax.OpAnyCharNotNL, syntax.OpAnyChar:
// matches Runes interpreted as range pair list
// matches any character except newline
// matches any character
return 1
case syntax.OpBeginLine, syntax.OpEndLine:
// matches empty string at beginning of line
// matches empty string at end of line
fallthrough
case syntax.OpBeginText, syntax.OpEndText:
// matches empty string at beginning of text
// matches empty string at end of text
fallthrough
case syntax.OpWordBoundary, syntax.OpNoWordBoundary:
// matches word boundary `\b`
// matches word non-boundary `\B`
return 0
case syntax.OpCapture:
// capturing subexpression with index Cap, optional name Name
return RegexpMaxWidth(re.Sub0[0])
case syntax.OpStar, syntax.OpPlus:
// matches Sub[0] zero or more times
// matches Sub[0] one or more times return -1
return -1
case syntax.OpQuest:
// matches Sub[0] zero or one times
return RegexpMaxWidth(re.Sub0[0])
case syntax.OpRepeat:
// matches Sub[0] at least Min times, at most Max (Max == -1 is no limit)
if re.Max == -1 {
return -1
}
return re.Max * RegexpMaxWidth(re.Sub0[0])
case syntax.OpConcat:
// matches concatenation of Subs
var total int
for _, sub := range re.Sub {
n := RegexpMaxWidth(sub)
if n == -1 {
return -1
}
total += n
}
return total
case syntax.OpAlternate:
// matches alternation of Subs
var max int
for _, sub := range re.Sub {
n := RegexpMaxWidth(sub)
if n == -1 {
return -1
}
if n > max {
max = n
}
}
return max
default:
return 0
}
}