Class: ARGF

Inherits:
Object show all
Defined in:
lib/backports/1.8.7/argf/lines.rb

Class Method Summary collapse

Class Method Details

.each_charObject Also known as: chars



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/backports/1.8.7/argf/each_char.rb', line 6

def each_char
  return to_enum(:each_char) unless block_given?
  if $KCODE == "UTF-8"
    lookup = 7.downto(4)
    while c = read(1) do
      n = c[0]
      leftmost_zero_bit = lookup.find{|i| n[i].zero? }
      case leftmost_zero_bit
      when 7 # ASCII
        yield c
      when 6 # UTF 8 complementary characters
        next # Encoding error, ignore
      else
        more = read(6-leftmost_zero_bit)
        break unless more
        yield c+more
      end
    end
  else
    while s = read(1)
      yield s
    end
  end

  self
end