Module: Binascii::Utils

Defined in:
lib/binascii/utils.rb

Class Method Summary collapse

Class Method Details

.each_byte_pair(data) {|[data.getbyte(-1), nil]| ... } ⇒ Object

Yields:

  • ([data.getbyte(-1), nil])


18
19
20
21
22
# File 'lib/binascii/utils.rb', line 18

def each_byte_pair(data, &block)
  data.force_encoding('ASCII-8BIT')
  data.each_byte.each_cons(2, &block)
  yield [data.getbyte(-1), nil]
end

.each_byte_quad(data) {|[nil, data.getbyte(0), data.getbyte(1), data.getbyte(2)]| ... } ⇒ Object

Yields:

  • ([nil, data.getbyte(0), data.getbyte(1), data.getbyte(2)])


4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/binascii/utils.rb', line 4

def each_byte_quad(data, &block)
  data.force_encoding('ASCII-8BIT')
  yield [nil, data.getbyte(0), data.getbyte(1), data.getbyte(2)]
  return if data.bytesize == 1

  data.each_byte.each_cons(4, &block)

  if data.bytesize > 2
    yield [data.getbyte(-3), data.getbyte(-2), data.getbyte(-1), nil]
  end

  yield [data.getbyte(-2), data.getbyte(-1), nil, nil]
end

.render_string(data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/binascii/utils.rb', line 24

def render_string(data)
  read_pos = 0
  write_pos = 0
  line_start_pos = 0
  result = ''

  each_byte_pair(data) do |current, trailing|
    if current == 13
      if trailing != 10
        write_pos = line_start_pos
        next
      end
    elsif current == 10
      line_start_pos = read_pos
    end

    if write_pos >= result.bytesize
      result << current
    else
      result.setbyte(write_pos, current)
    end

    read_pos += 1
    write_pos += 1
  end

  result
end