[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Efficiently checking the initial contents of a file
From: |
Juanma Barranquero |
Subject: |
Re: Efficiently checking the initial contents of a file |
Date: |
Fri, 16 May 2008 13:08:13 +0200 |
On Fri, May 16, 2008 at 12:16 PM, Nordlöw <per.nordlow@gmail.com> wrote:
> How can I efficiently using pure emacs-lisp (without calling any
> external process) investigate the first bytes of a file?
>
> My guess is
> - Open parts of the file into a buffer or string.
> - Alt 1. Switch to the buffer and do things.
> Alt 2. Or do stuff directly the string?
(with-temp-buffer
(insert-file-contents "my-file" nil BEG END)
;; etc
)
should be pretty fast, and more so with wisely chosen BEG / END
values. For additional speed you can use
insert-file-contents-literally, if you don't need code conversions,
decompression, etc.
> Which operations should be performed via a buffer and which should
> operate on a string?
Once the required part of the file is in the buffer, manipulating it
directly or using buffer-(sub)string depends on what do you want to do
with the text, but I wouldn't convert it to a string unless necessary,
i.e., I would use looking-at and re-search-forward rather than
buffer-string + string-match, for example.
Juanma