Class: OpenASN::BinaryFormat::ArraysOverlay
- Inherits:
-
Object
- Object
- OpenASN::BinaryFormat::ArraysOverlay
- Defined in:
- lib/openasn/binary_format.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
Instance Method Summary collapse
- #cover?(ip_int) ⇒ Boolean
-
#initialize(bytes, family) ⇒ ArraysOverlay
constructor
A new instance of ArraysOverlay.
Constructor Details
#initialize(bytes, family) ⇒ ArraysOverlay
Returns a new instance of ArraysOverlay.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/openasn/binary_format.rb', line 236 def initialize(bytes, family) asz = BinaryFormat.addr_size(family) rec = BinaryFormat.(family) @count = bytes.bytesize / rec @starts = Array.new(@count) @ends = Array.new(@count) unpack_addr = family == :ipv4 ? ->(s) { s.unpack1("N") } : ->(s) { hi, lo = s.unpack("Q>Q>"); (hi << 64) | lo } @count.times do |i| off = i * rec @starts[i] = unpack_addr.call(bytes[off, asz]) @ends[i] = unpack_addr.call(bytes[off + asz, asz]) end [@starts, @ends].each(&:freeze) end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
234 235 236 |
# File 'lib/openasn/binary_format.rb', line 234 def count @count end |
Instance Method Details
#cover?(ip_int) ⇒ Boolean
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/openasn/binary_format.rb', line 251 def cover?(ip_int) lo = 0 hi = @count - 1 while lo <= hi mid = (lo + hi) / 2 if ip_int < @starts[mid] hi = mid - 1 elsif ip_int > @ends[mid] lo = mid + 1 else return true end end false end |