Class: LineInput
Overview
Utility class for line-wise file parsing
Class Method Summary collapse
Instance Method Summary collapse
- #each ⇒ Object
- #eof? ⇒ Boolean
- #getblock(term_re) ⇒ Object
- #getlines_until(re) ⇒ Object (also: #break)
- #getlines_while(re) ⇒ Object (also: #span)
- #gets ⇒ Object
- #gets_if(re, index = nil) ⇒ Object
- #gets_unless(re) ⇒ Object
-
#initialize(f, entry = nil) ⇒ LineInput
constructor
A new instance of LineInput.
- #inspect ⇒ Object
- #lineno ⇒ Object
- #name ⇒ Object
- #next? ⇒ Boolean
- #path ⇒ Object
- #peek ⇒ Object
- #skip_blank_lines ⇒ Object
- #ungets(line) ⇒ Object
- #until_match(re) ⇒ Object
- #until_terminator(re) ⇒ Object
- #while_match(re) ⇒ Object
Constructor Details
#initialize(f, entry = nil) ⇒ LineInput
Returns a new instance of LineInput.
22 23 24 25 26 27 28 |
# File 'lib/bitclust/lineinput.rb', line 22 def initialize(f, entry = nil) @input = f @entry = entry @buf = [] @lineno = 0 @eof_p = false end |
Class Method Details
Instance Method Details
#each ⇒ Object
117 118 119 120 121 |
# File 'lib/bitclust/lineinput.rb', line 117 def each while line = gets() yield line end end |
#eof? ⇒ Boolean
34 35 36 |
# File 'lib/bitclust/lineinput.rb', line 34 def eof? @eof_p end |
#getblock(term_re) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/bitclust/lineinput.rb', line 173 def getblock(term_re) buf = [] until_terminator(term_re) do |line| buf.push line end buf end |
#getlines_until(re) ⇒ Object Also known as: break
155 156 157 158 159 160 161 |
# File 'lib/bitclust/lineinput.rb', line 155 def getlines_until(re) buf = [] until_match(re) do |line| buf.push line end buf end |
#getlines_while(re) ⇒ Object Also known as: span
134 135 136 137 138 139 140 |
# File 'lib/bitclust/lineinput.rb', line 134 def getlines_while(re) buf = [] while_match(re) do |line| buf.push line end buf end |
#gets ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/bitclust/lineinput.rb', line 54 def gets unless @buf.empty? @lineno += 1 line = @buf.pop line&.location ||= BitClust::Location.new(path, @lineno) return line end return nil if @eof_p # to avoid ARGF blocking. line = @input.gets @eof_p = true unless line @lineno += 1 line&.location ||= BitClust::Location.new(path, @lineno) line end |
#gets_if(re, index = nil) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/bitclust/lineinput.rb', line 98 def gets_if(re, index = nil) line = gets() if not line or not (re =~ line) ungets line return nil end return $~[index] if index line end |
#gets_unless(re) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/bitclust/lineinput.rb', line 108 def gets_unless(re) line = gets() if not line or re =~ line ungets line return nil end line end |
#inspect ⇒ Object
30 31 32 |
# File 'lib/bitclust/lineinput.rb', line 30 def inspect "\#<#{self.class} file=#{@input.inspect} line=#{lineno()}>" end |
#lineno ⇒ Object
50 51 52 |
# File 'lib/bitclust/lineinput.rb', line 50 def lineno @lineno end |
#name ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/bitclust/lineinput.rb', line 42 def name if @entry @entry.inspect else "-" end end |
#next? ⇒ Boolean
82 83 84 |
# File 'lib/bitclust/lineinput.rb', line 82 def next? peek() ? true : false end |
#path ⇒ Object
38 39 40 |
# File 'lib/bitclust/lineinput.rb', line 38 def path @input.path if @input.respond_to?(:path) end |
#peek ⇒ Object
76 77 78 79 80 |
# File 'lib/bitclust/lineinput.rb', line 76 def peek line = gets() ungets line if line line end |
#skip_blank_lines ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/bitclust/lineinput.rb', line 86 def skip_blank_lines n = 0 while line = gets() unless line.strip.empty? ungets line return n end n += 1 end n end |
#ungets(line) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/bitclust/lineinput.rb', line 69 def ungets(line) return unless line @lineno -= 1 @buf.push line line end |
#until_match(re) ⇒ Object
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/bitclust/lineinput.rb', line 144 def until_match(re) while line = gets() if re =~ line ungets line return end yield line end nil end |
#until_terminator(re) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/bitclust/lineinput.rb', line 165 def until_terminator(re) while line = gets() return if re =~ line # discard terminal line yield line end nil end |
#while_match(re) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/bitclust/lineinput.rb', line 123 def while_match(re) while line = gets() unless re =~ line ungets line return end yield line end nil end |