all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Matt Swift" <swift@alum.mit.edu>
Cc: bug-gnu-emacs@gnu.org
Subject: RE: `match-data' set improperly
Date: Wed, 12 Feb 2003 14:25:11 -0500	[thread overview]
Message-ID: <001001c2d2cc$7925d8c0$6200a8c0@swift.xxx> (raw)
In-Reply-To: <5xsmutsrbv.fsf@kfs2.cua.dk>

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	other threads:[~2003-02-12 19:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-12  3:16 `match-data' set improperly Matthew Swift
2003-02-12 10:53 ` Kim F. Storm
2003-02-12 19:25   ` Matt Swift [this message]
     [not found] ` <mailman.1845.1045043916.21513.bug-gnu-emacs@gnu.org>
2003-02-12 16:02   ` Kevin Rodgers
2003-02-13 10:08     ` Richard Stallman
  -- strict thread matches above, loose matches on Subject: below --
2003-02-13  0:54 Luc Teirlinck
2003-02-13  6:42 ` Matt Swift
2003-02-13 12:25   ` Kim F. Storm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='001001c2d2cc$7925d8c0$6200a8c0@swift.xxx' \
    --to=swift@alum.mit.edu \
    --cc=bug-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.