MCPcopy
hub / github.com/caddyserver/caddy / specificity

Function specificity

caddyconfig/httpcaddyfile/httptype.go:1726–1741  ·  view source on GitHub ↗

specificity returns len(s) minus any wildcards (*) and placeholders ({...}). Basically, it's a length count that penalizes the use of wildcards and placeholders. This is useful for comparing hostnames and paths. However, wildcards in paths are not a sure answer to the question of specificity. For ex

(s string)

Source from the content-addressed store, hash-verified

1724// 'a.example.com', but is '/a' more or less specific
1725// than '/a*'?
1726func specificity(s string) int {
1727 l := len(s) - strings.Count(s, "*")
1728 for len(s) > 0 {
1729 start := strings.Index(s, "{")
1730 if start < 0 {
1731 return l
1732 }
1733 end := strings.Index(s[start:], "}") + start + 1
1734 if end <= start {
1735 return l
1736 }
1737 l -= end - start
1738 s = s[end:]
1739 }
1740 return l
1741}
1742
1743type counter struct {
1744 n *int

Callers 2

serversFromPairingsMethod · 0.85
TestSpecificityFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestSpecificityFunction · 0.68