help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Stream-based Scanning of File-Buffers


From: Pascal J. Bourguignon
Subject: Re: Stream-based Scanning of File-Buffers
Date: Fri, 25 Sep 2009 15:16:31 +0200
User-agent: Gnus/5.101 (Gnus v5.10.10) Emacs/22.2 (gnu/linux)

Nordlöw <per.nordlow@gmail.com> writes:

> Is there a way to perform string/regexp scanning (using search-forward
> or re-search-forward) in a file-buffer whose contents is loaded only
> when it's needed, kind of like file streams in C, so that I only do a
> physical read on the blocks that I actually scan and then skip the
> rest of the file as soon as I get a hit.

There are emacs lisp primitives that allow you to load a file on
demand.  However, the regular expressions functions will only work on
a string or a buffer (faster on a buffer), so when you reach the end
of the buffer without a match, you will have to further load a chunk
and retry the regexp.  In general, for a regexp such as "a.*z", if
your file contains a 'a' in the first position, a lot of 'b's, and a
'z' in last position, you will have to load the whole file to match it
with the provided regexp functions.

Of course, you may write your own regexp compiler.  A regexp such as
"a.*z" would be compiled to something like:

(lambda (stream)
   (loop 
      for ch = (read-char stream nil)
      while (and ch (char/= ch ?a))
      finally (return (if ch
                         ;; got a 'a'
                         (loop for ch = (read-char stream nil)
                               while (and ch (char/= ch ?z))
                               finally (return (if ch
                                                 'found-match
                                                  nil)))
                         ;; got eof
                         nil))))

In such a case, you need only a one character buffer: ch.
Other regular expressions may need bigger buffers.  


-- 
__Pascal Bourguignon__


reply via email to

[Prev in Thread] Current Thread [Next in Thread]