开发者

StringScanner scanning IO instead of a string

I've got a parser written using ruby's standard StringScanner. It would be nice if I could use it on streaming files. Is ther开发者_Go百科e an equivalent to StringScanner that doesn't require me to load the whole string into memory?


You might have to rework your parser a bit, but you can feed lines from a file to a scanner like this:

File.open('filepath.txt', 'r') do |file|
  scanner = StringScanner.new(file.readline)
  until file.eof?
    scanner.scan(/whatever/)
    scanner << file.readline
  end
end


StringScanner was intended for that, to load a big string and going back and forth with an internal pointer, if you make it a stream, then the references get lost, you can not use unscan, check_until, pre_match, post_match, well you can, but for that you need to buffer all the previous input.

If you are concerned about the buffer size then just load by chunk of data, and use a simple regexp or a gem called Parser. The simplest way is to read a fix size of data.

# iterate over fixed length records
open("fixed-record-file") do |f|
  while record = f.read(1024)
    # parse here the record using regexp or parser
  end
end

[Updated]

Even with this loop you can use StringSanner, you just need to update the string with each new chunk of data:

string=(str)

Changes the string being scanned to str and resets the scanner. Returns str


There is StringIO.

Sorry misread you question. Take a look at this seems to have streaming options

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜