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

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

RE: `looking-back' strange warning


From: Drew Adams
Subject: RE: `looking-back' strange warning
Date: Fri, 2 Oct 2015 13:28:15 -0700 (PDT)

> > My two cents: Even if you specify an optimal limit, `looking-back' can
> > still be unnecessarily slow.  For example,
> >    (looking-back "xy" 2)
> > is much slower than
> >     (and (> (point) 2)
> >          (save-excursion (goto-char (- (point) 2))
> >                          (looking-at "xy")))
> > where "xy" stands for any plain string.
> 
> Definitely.  And this is in fact a typical case of the
> misuse: the string to check is often a literal, so its
> length is known.
> 
> This is the first thing to mention about `looking-back',
> in terms of performance: avoid it altogether, if you can.

BTW/FWIW -
I use this function in such cases, which can be pretty common:

(defun chars-before (chars)
  "Return non-nil if the literal string CHARS is right before point.
This is more efficient that `looking-back' for this use case."
  (let* ((len  (length chars))
         (idx  (1- len))
         (pt   (point)))
    (catch 'chars-before
      (dolist (char  (append chars ()))
        (unless (condition-case nil
                    (eq char (char-before (- pt idx)))
                  (error nil))          ; e.g. `bobp'
          (throw 'chars-before nil))
        (setq idx  (1- idx)))
      t)))

http://www.emacswiki.org/emacs/download/misc-fns.el



reply via email to

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