-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a single set and encode stuff in map value
- Loading branch information
Showing
2 changed files
with
31 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
package origins | ||
|
||
type SchemePorts map[string]map[int]struct{} // TODO: replace struct by int: 1 means exact match, 2 means arbitrary subs, 3 means both | ||
type SchemePorts map[string]map[int]int // TODO: replace struct by int: 0: nothing, 1 means exact match, 2 means arbitrary subs, 3 means both | ||
// TODO: nil ports for scheme means all ports allowed? | ||
|
||
func NewSchemePorts(scheme string, port int) SchemePorts { | ||
func NewSchemePorts(scheme string, port int, wildcardSubs bool) SchemePorts { | ||
set := make(SchemePorts) | ||
set.Add(scheme, port) | ||
set.Add(scheme, port, wildcardSubs) | ||
return set | ||
} | ||
|
||
func (s SchemePorts) Add(scheme string, port int) { | ||
var m map[int]struct{} | ||
func (s SchemePorts) Add(scheme string, port int, wildcardSubs bool) { | ||
var m map[int]int | ||
if m = s[scheme]; m == nil { | ||
m = make(map[int]struct{}) | ||
m = make(map[int]int) | ||
} | ||
m[port] = struct{}{} | ||
var i int | ||
if wildcardSubs { | ||
i = 2 | ||
} else { | ||
i = 1 | ||
} | ||
m[port] = m[port] | i | ||
s[scheme] = m | ||
} | ||
|
||
func (s SchemePorts) Contains(scheme string, port int) bool { | ||
func (s SchemePorts) Contains(scheme string, port int, wildcardSubs bool) bool { | ||
ports, found := s[scheme] | ||
if !found { | ||
return false | ||
} | ||
_, found = ports[port] | ||
return found | ||
v := ports[port] | ||
return !wildcardSubs && v&1 == 1 || wildcardSubs && v&2 == 2 | ||
} |