Class: ThreadQueues::StringBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/thread_queues/string_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ StringBuffer

Returns a new instance of StringBuffer.



6
7
8
9
10
11
12
# File 'lib/thread_queues/string_buffer.rb', line 6

def initialize(queue)
  @queue = queue
  @buffer = ""
  @mutex = Monitor.new
  @lineno = 0
  @pos = 0
end

Instance Attribute Details

#linenoObject (readonly)

Returns the value of attribute lineno.



4
5
6
# File 'lib/thread_queues/string_buffer.rb', line 4

def lineno
  @lineno
end

#posObject (readonly)

Returns the value of attribute pos.



4
5
6
# File 'lib/thread_queues/string_buffer.rb', line 4

def pos
  @pos
end

Instance Method Details

#each_byte(&blk) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/thread_queues/string_buffer.rb', line 65

def each_byte(&blk)
  with_enumerator(blk) do |y|
    each_chunk do |chunk|
      chunk.each_byte do |byte|
        y << byte
      end
    end
  end
end

#each_char(&blk) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/thread_queues/string_buffer.rb', line 55

def each_char(&blk)
  with_enumerator(blk) do |y|
    each_chunk do |chunk|
      chunk.each_char do |char|
        y << char
      end
    end
  end
end

#each_line(&blk) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/thread_queues/string_buffer.rb', line 44

def each_line(&blk)
  with_enumerator(blk) do |y|
    begin
      loop do
        y << gets
      end
    rescue EOFError
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
123
# File 'lib/thread_queues/string_buffer.rb', line 114

def empty?
  if @pos == 0 && !@closed
    begin
      store_more_in_buffer
    rescue EOFError
    end
  end

  @pos == 0 && @buffer.empty?
end

#gets(sep = "\n", limit = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/thread_queues/string_buffer.rb', line 14

def gets(sep = "\n", limit = nil)
  if limit == nil && !sep.is_a?(String)
    limit = sep
    sep = "\n"
  end

  @mutex.synchronize do
    loop do
      if match = @buffer.match(/\A([\s\S]+?)#{Regexp.escape(sep)}/)
        take = match[0]
        if limit && take.length > limit
          take = take.slice(0, limit)
        end

        @buffer.gsub!(/\A#{Regexp.escape(take)}/, "")
        @lineno += 1
        @pos += match[0].bytesize
        return take
      end

      begin
        store_more_in_buffer
      rescue EOFError => e
        return rest_of_buffer if @buffer.length > 0
        raise e
      end
    end
  end
end

#read(length = nil, outbuf = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/thread_queues/string_buffer.rb', line 75

def read(length = nil, outbuf = nil)
  return read_all if length == nil

  content = nil

  @mutex.synchronize do
    loop do
      if @buffer.length >= length
        content = @buffer.slice!(0, length)
        @pos += content.bytesize
        break
      end

      begin
        store_more_in_buffer
      rescue EOFError => e
        if @buffer.length > 0
          content = rest_of_buffer
          break
        end

        # Return nil if length is set like a normal IO - otherwise an empty string.
        if length
          return nil
        else
          content = ""
        end
      end
    end
  end

  if outbuf
    outbuf.clear
    outbuf << content
  end

  return content
end