Class: HTML5::EncodingBytes

Inherits:
String
  • Object
show all
Defined in:
lib/html5/inputstream.rb

Overview

String-like object with an assosiated position and various extra methods If the position is ever greater than the string length then an exception is raised

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from String

#underscore

Constructor Details

#initialize(value) ⇒ EncodingBytes

Returns a new instance of EncodingBytes.



421
422
423
424
# File 'lib/html5/inputstream.rb', line 421

def initialize(value)
  super(value)
  @position = -1
end

Instance Attribute Details

#positionObject

Returns the value of attribute position.



419
420
421
# File 'lib/html5/inputstream.rb', line 419

def position
  @position
end

Instance Method Details

#current_byteObject

Raises:



434
435
436
437
# File 'lib/html5/inputstream.rb', line 434

def current_byte
  raise EOF if @position >= length
  return self[@position].chr
end

#eachObject



426
427
428
429
430
431
432
# File 'lib/html5/inputstream.rb', line 426

def each
  while @position < length
    @position += 1
    yield self[@position]
  end
rescue EOF
end

#find_next(byte_list) ⇒ Object

Move the pointer so it points to the next byte in a set of possible bytes



471
472
473
474
475
# File 'lib/html5/inputstream.rb', line 471

def find_next(byte_list)
  until byte_list.include?(current_byte)
    @position += 1
  end
end

#jump_to(bytes) ⇒ Object

Look for the next sequence of bytes matching a given sequence. If a match is found advance the position to the last byte of the match



459
460
461
462
463
464
465
466
467
# File 'lib/html5/inputstream.rb', line 459

def jump_to(bytes)
  new_position = self[position .. -1].index(bytes)
  if new_position
    @position += (new_position + bytes.length-1)
    return true
  else
    raise EOF
  end
end

#match_bytes(bytes, lower = false) ⇒ Object

Look for a sequence of bytes at the start of a string. If the bytes are found return true and advance the position to the byte after the match. Otherwise return false and leave the position alone



449
450
451
452
453
454
455
# File 'lib/html5/inputstream.rb', line 449

def match_bytes(bytes, lower=false)
  data = self[position ... position+bytes.length]
  data.downcase! if lower
  rv = (data == bytes)
  @position += bytes.length if rv == true
  return rv
end

#skip(chars = SPACE_CHARACTERS) ⇒ Object

Skip past a list of characters



440
441
442
443
444
# File 'lib/html5/inputstream.rb', line 440

def skip(chars=SPACE_CHARACTERS)
  while chars.include?(current_byte)
    @position += 1
  end
end