New creates and returns a new Matcher implementing the given pattern. The pattern syntax is defined in the package doc comment. In addition to the pattern syntax syntax, New("") returns nil, nil. The nil *Matcher is valid for use: it returns true from ShouldEnable and false from ShouldPrint for all
(pattern string)
| 192 | // [Hash], [Matcher.ShouldEnable], and [Matcher.ShouldPrint] entirely |
| 193 | // when they recognize the nil Matcher. |
| 194 | func New(pattern string) (*Matcher, error) { |
| 195 | if pattern == "" { |
| 196 | return nil, nil |
| 197 | } |
| 198 | |
| 199 | m := new(Matcher) |
| 200 | |
| 201 | p := pattern |
| 202 | // Special case for leading 'q' so that 'qn' quietly disables, e.g. fmahash=qn to disable fma |
| 203 | // Any instance of 'v' disables 'q'. |
| 204 | if len(p) > 0 && p[0] == 'q' { |
| 205 | m.quiet = true |
| 206 | p = p[1:] |
| 207 | if p == "" { |
| 208 | return nil, &parseError{"invalid pattern syntax: " + pattern} |
| 209 | } |
| 210 | } |
| 211 | // Allow multiple v, so that “bisect cmd vPATTERN” can force verbose all the time. |
| 212 | for len(p) > 0 && p[0] == 'v' { |
| 213 | m.verbose = true |
| 214 | m.quiet = false |
| 215 | p = p[1:] |
| 216 | if p == "" { |
| 217 | return nil, &parseError{"invalid pattern syntax: " + pattern} |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // Allow multiple !, each negating the last, so that “bisect cmd !PATTERN” works |
| 222 | // even when bisect chooses to add its own !. |
| 223 | m.enable = true |
| 224 | for len(p) > 0 && p[0] == '!' { |
| 225 | m.enable = !m.enable |
| 226 | p = p[1:] |
| 227 | if p == "" { |
| 228 | return nil, &parseError{"invalid pattern syntax: " + pattern} |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if p == "n" { |
| 233 | // n is an alias for !y. |
| 234 | m.enable = !m.enable |
| 235 | p = "y" |
| 236 | } |
| 237 | |
| 238 | // Parse actual pattern syntax. |
| 239 | result := true |
| 240 | bits := uint64(0) |
| 241 | start := 0 |
| 242 | wid := 1 // 1-bit (binary); sometimes 4-bit (hex) |
| 243 | for i := 0; i <= len(p); i++ { |
| 244 | // Imagine a trailing - at the end of the pattern to flush final suffix |
| 245 | c := byte('-') |
| 246 | if i < len(p) { |
| 247 | c = p[i] |
| 248 | } |
| 249 | if i == start && wid == 1 && c == 'x' { // leading x for hex |
| 250 | start = i + 1 |
| 251 | wid = 4 |