github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/portset/range.go (about) 1 package portset 2 3 // PortRange is an inclusive range of ports. 4 type PortRange struct { 5 From uint16 6 To uint16 7 } 8 9 // Contains returns whether the given port is in the range. 10 func (r PortRange) Contains(port uint16) bool { 11 return r.From <= port && port <= r.To 12 } 13 14 // PortRangeSet is a set of port ranges. 15 type PortRangeSet struct { 16 ranges []PortRange 17 } 18 19 // Contains returns whether the given port is in the set. 20 func (s PortRangeSet) Contains(port uint16) bool { 21 i, j := 0, len(s.ranges) 22 for i < j { 23 h := int(uint(i+j) >> 1) 24 switch { 25 case port > s.ranges[h].To: 26 i = h + 1 27 case port < s.ranges[h].From: 28 j = h 29 default: 30 return true 31 } 32 } 33 return false 34 }