MCPcopy Create free account
hub / github.com/OpenAtomFoundation/SmartIDE / NewPattern

Function NewPattern

cli/pkg/ssh_config/config.go:465–497  ·  view source on GitHub ↗

NewPattern creates a new Pattern for matching hosts. NewPattern("*") creates a Pattern that matches all hosts. From the manpage, a pattern consists of zero or more non-whitespace characters, `*' (a wildcard that matches zero or more characters), or `?' (a wildcard that matches exactly one character

(s string)

Source from the content-addressed store, hash-verified

463//
464// Host 192.168.0.?
465func NewPattern(s string) (*Pattern, error) {
466 if s == "" {
467 return nil, errors.New("ssh_config: empty pattern")
468 }
469 negated := false
470 if s[0] == '!' {
471 negated = true
472 s = s[1:]
473 }
474 var buf bytes.Buffer
475 buf.WriteByte('^')
476 for i := 0; i < len(s); i++ {
477 // A byte loop is correct because all metacharacters are ASCII.
478 switch b := s[i]; b {
479 case '*':
480 buf.WriteString(".*")
481 case '?':
482 buf.WriteString(".?")
483 default:
484 // borrowing from QuoteMeta here.
485 if special(b) {
486 buf.WriteByte('\\')
487 }
488 buf.WriteByte(b)
489 }
490 }
491 buf.WriteByte('$')
492 r, err := regexp.Compile(buf.String())
493 if err != nil {
494 return nil, err
495 }
496 return &Pattern{str: s, regex: r, not: negated}, nil
497}
498
499// Host describes a Host directive and the keywords that follow it.
500type Host struct {

Callers 2

parseKVMethod · 0.85
initFunction · 0.85

Calls 2

specialFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected