From: Simon Strandgaard Date: 2004-05-20T20:33:44+09:00 Subject: Re: seraching backward in files Patrick Gundlach wrote: > I'd wonder if there is a simple method in searching backward in > files. I want to read a pdf file, so I have to do the following: > > 1) goto the end of file (pdffile.seek(0,IO::SEEK_END)) > 2) search backward for the string "%%EOF" within the last 1024 bytes > 3) search backward for "startxref" > 4) read the next bytes,..... > > > Do I have to write my own seach backward routine or is there some > functionality for it? Ruby's native regexp engine cannot go backwards... thats why I am working on my own regexp-engine. However I don't have a FileInputStream class yet (but I can easily add one). The text is spelled backwards, because we want to search backwards. # pseudo code iterator = file.create_iterator_end.reverse re = NewRegexp.new('(?xm) .{0,1024} FOE%% .{0,4000} ferxtrats') matchdata = re.match(iterator) The engine is written entirely in Ruby, so speed isn't impressive. Its fairly compatible with Ruby1.8's builtin GNU regexp, and its carefully tested (more than 2000 tests). http://raa.ruby-lang.org/list.rhtml?name=regexp -- Simon Strandgaard