Method: Stupidedi::Reader::FileInput#index

Defined in:
lib/stupidedi/reader/input/file_input.rb

#index(value) ⇒ Integer

Returns the smallest ‘n`, where #at`(n)` == `element`

Parameters:

  • element (Object)

    the element to find in the input

Returns:

  • (Integer)
  • nil if ‘element` is not present in the input



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/stupidedi/reader/input/file_input.rb', line 67

def index(value)
  @io.seek(@offset)
  length = value.length

  # We need to start with value != buffer, and this is a reasonable guess
  buffer = "\377" * length

  until @io.eof?
    buffer.slice!(0)
    buffer = buffer + @io.read(1)

    if buffer == value
      return @io.tell - @offset - length
    end
  end
end