Class: GDB::Tube::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/gdb/tube/buffer.rb

Overview

IO buffer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuffer

Returns a new instance of Buffer.


9
10
11
12
# File 'lib/gdb/tube/buffer.rb', line 9

def initialize
  @data = []
  @size = 0
end

Instance Attribute Details

#sizeInteger (readonly)

Returns size.

Returns:

  • (Integer)

    size


7
8
9
# File 'lib/gdb/tube/buffer.rb', line 7

def size
  @size
end

Instance Method Details

#<<(str) ⇒ Buffer

Push string into buffer.

Parameters:

  • str (String)

    String to push.

Returns:

  • (Buffer)

    Returns self so this method is chainable.


21
22
23
24
25
26
27
28
# File 'lib/gdb/tube/buffer.rb', line 21

def <<(str)
  str = str.to_s.dup
  return self if str.empty?

  @data << str
  @size += str.size
  self
end

#empty?Boolean

Returns:

  • (Boolean)

30
31
32
# File 'lib/gdb/tube/buffer.rb', line 30

def empty?
  @size.zero?
end

#get(n = nil) ⇒ String

Retrieves at most n bytes from buffer.

Parameters:

  • n (Integer?) (defaults to: nil)

    Maximum number of bytes. n equals nil for unlimited.

Returns:

  • (String)

    Retrieved string.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gdb/tube/buffer.rb', line 41

def get(n = nil)
  if n.nil? || n >= @size
    ret = @data.join
    @data.clear
    @size = 0
  else
    now = 0
    idx = @data.find_index do |s|
      if s.size + now >= n
        true
      else
        now += s.size
        false
      end
    end
    ret = @data.slice!(0, idx + 1).join
    back = ret.slice!(n..-1)
    @data.unshift(back) unless back.empty?
    @size -= n
  end
  ret
end

#unshift(str) ⇒ void

This method returns an undefined value.

Push front.

Parameters:

  • str (String)

    String to be push.


70
71
72
73
74
75
# File 'lib/gdb/tube/buffer.rb', line 70

def unshift(str)
  return if str.nil? || str.empty?

  @data.unshift(str)
  @size += str.size
end