Sha256: 2a85a3dae8533cf4d4d14ab9374612dabd0f9f5ca3491d1297fc6b975c820b90

Contents?: true

Size: 761 Bytes

Versions: 73

Compression:

Stored size: 761 Bytes

Contents

unless IO.method_defined? :each_char
  require 'enumerator'

  class IO
    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

    alias_method :chars, :each_char
  end
end

Version data entries

73 entries across 73 versions & 5 rubygems

Version Path
backports-3.25.0 lib/backports/1.8.7/io/each_char.rb
backports-3.24.1 lib/backports/1.8.7/io/each_char.rb
backports-3.24.0 lib/backports/1.8.7/io/each_char.rb
backports-3.23.0 lib/backports/1.8.7/io/each_char.rb
backports-3.22.1 lib/backports/1.8.7/io/each_char.rb
backports-3.22.0 lib/backports/1.8.7/io/each_char.rb
backports-3.21.0 lib/backports/1.8.7/io/each_char.rb
backports-3.20.2 lib/backports/1.8.7/io/each_char.rb
backports-3.20.1 lib/backports/1.8.7/io/each_char.rb
backports-3.20.0 lib/backports/1.8.7/io/each_char.rb
backports-3.19.0 lib/backports/1.8.7/io/each_char.rb
backports-3.18.2 lib/backports/1.8.7/io/each_char.rb
backports-3.18.1 lib/backports/1.8.7/io/each_char.rb
backports-3.18.0 lib/backports/1.8.7/io/each_char.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/backports-3.12.0/lib/backports/1.8.7/io/each_char.rb
backports-3.17.2 lib/backports/1.8.7/io/each_char.rb
backports-3.17.1 lib/backports/1.8.7/io/each_char.rb
backports-3.17.0 lib/backports/1.8.7/io/each_char.rb
backports-3.16.1 lib/backports/1.8.7/io/each_char.rb
backports-3.16.0 lib/backports/1.8.7/io/each_char.rb