Module: Wind::IO

Defined in:
lib/wind/io.rb

Overview

Core extensions for Ruby IO

Constant Summary collapse

BUFFER_SIZE =

The buffer size is set to result of ‘getconf PAGESIZE` or 4096 if that fails.

page_size

Instance Method Summary collapse

Instance Method Details

#each_lineios or an_enumerator

Note:

Currently only tested on “regular” text files.

Note:

The buffer size is set to the result of ‘getconf PAGESIZE`, or if that fails, it is set to 4096.

A fast buffered version of IO#each_line.

In my initial tests, it is between 2 and 3.5 times faster than Ruby’s IO#each_line.

You can include Wind::IO in the File class and use this method just like IO#each_line.

Examples:

File.include Wind::IO
File.open(fname, "rt").each_line { |line| puts line }

Returns:

  • (ios or an_enumerator)

    Returns ios if a block is given, else, returns an enumerator



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/wind/io.rb', line 52

def each_line
  return enum_for(:each_line) unless block_given?

  buf = ""
  prev_unfinished_line = ""

  while self.read(BUFFER_SIZE, buf)
    buf.scan(/^.*$\n?/).each do |str|
      if prev_unfinished_line.empty? && str[-1] == "\n"
        # str is a complete line, yield it
        yield str
      elsif prev_unfinished_line.empty?
        # str is the start of an unfinished line
        prev_unfinished_line = str
      elsif !prev_unfinished_line.empty? && str[-1] == "\n"
        # str is the conclusion of a prev_unfinished_line
        prev_unfinished_line << str

        # str is now finished, yield and reset
        yield prev_unfinished_line
        prev_unfinished_line = ""
      else
        # str is a continuation of a prev_unfinished_line
        prev_unfinished_line << str
      end
    end
  end
end