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

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

RE: `match-data' set improperly


From: Matt Swift
Subject: RE: `match-data' set improperly
Date: Wed, 12 Feb 2003 14:25:11 -0500

The documentation (TeXinfo and docstrings) is pretty clear that
(match-string N) when submatch N>0 does not match anything is nil, and it is
also clear that (match-string 0) is like the case for N>0 but referring to
the entire match instead of a submatch.  There is no suggestion that
match-data is ever undefined, once one match has been done, and the error
message you get is obscure.

Certainly the problem could be simply in documentation.  But the valid value
of (match-string 0) after a failure *ought* to be nil, just as it is for,
say, (match-string 4) when there is no submatch 4 or submatch 4 matched
nothing.  If I had to check what `string-match' and friends returned every
time before accessing the match-data, it would be a pain in the neck and I
might hallucinate that I was programming in C not Emacs-Lisp.

i.e., ideally, 

  (string-match "a" "b")  => nil
  (match-data 'integers) => (nil nil)

  (string-match "ab\\(cd\\)?\\(ef\\)?" "abef") => 0
  (match-data 'integers) => (0 4 nil nil 2 4)

  (string-match "a" "b")  => nil
  (match-data 'integers) => (nil nil)

I do not see much use for refraining to change the match data when a match
fails (if that is what is happening, and I'm not sure it is), and programs
that want to do that can implement it easily with `save-match-data', e.g.,
depending on whether one wants to be as careful as save-match-data to
restore match data if there is an error when doing the `string-match', 

  (defun string-match-or-I-never-existed (r s &optional start)
    (let ((md (match-data))
          (result (string-match r s start)))
        (or result (set-match-data md))

;; NOTE above relies on undocumented but observed fact that `set-match-data'
;;      always returns nil.

 
-----Original Message-----
From: Kim F. Storm [mailto:no-spam@cua.dk] 
Sent: Wednesday, February 12, 2003 5:53 AM
To: Matthew Swift
Cc: bug-gnu-emacs@gnu.org
Subject: Re: `match-data' set improperly


Matthew Swift <swift@alum.mit.edu> writes:

> Evaluating the following `let' form gives varying results, but the results
> should be consistent.  
 
> My *guess* is that when `string-match' fails, it either fails to set
> `match-data' or sets it incorrectly in one of two (or more) ways.

My guess is that you are not supposed to use match-data when
string-match fails, so its value is indeed undefined in that case.

What _valid_ values would you expect it to contain after the match failed?






reply via email to

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