all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tim X <timx@nospam.dev.null>
To: help-gnu-emacs@gnu.org
Subject: Re: use of "match-string"
Date: Wed, 09 Mar 2011 08:36:17 +1100	[thread overview]
Message-ID: <87r5ahi9bi.fsf@rapttech.com.au> (raw)
In-Reply-To: mailman.2.1299605488.4046.help-gnu-emacs@gnu.org

ken <gebser@mousecar.com> writes:

> Part of this function doesn't make sense--
>
> (defun html-toc-find-max ()
>   (goto-char (point-min))
>   (let ((max-toc 0))
>     (while (search-forward-regexp html-toc-tocref nil t)
>       (if (> (string-to-int (match-string 1)) max-toc)
>           (setq max-toc (string-to-int (match-string 1)))))
>     (1+ max-toc)))
>
> -- specifically, where match-string is first called and turned into a
> number.  The docs say that match-string returns a string....  Yes, this
> can be done I suppose, but to what end?  Moreover, depending upon its
> value, this "number" may then be assigned to a variable, and that value
> then compared with subsequent strings.
>
> Perhaps I'm missing some nuance here.  The entirety of the code is
> below.  Does anyone understand what's going on here?
>

I'm not sure what it is you find 'odd' about the above function. Apart
from the fact it should be using string-to-number (string-to-int has
been marked obsolete since 22.1), it seems reasonable to me. 

The regexp used in the match is 

> (defvar html-toc-tocref     (concat "<A NAME=\"" html-toc-name-pre
>                                     "\\([0-9]*\\)\">"))

Note the 1st (and only) grouping in the regexp i.e. \\([0-9]*\\), which
will match on 0 or more digits between 0..9. This is what (match-string
1) will return. (though as it is [0-9]* it could return 0 or more
digits, so match-string 1 could be "", which may be an issue).

The string-to-int call will return that value as a number rather than as
a string, which is then compared to max-toc (initially set to 0), not to
a string. The final value has 1 added to it. So, your not comparing
strings, you are comparing strings of numers that are converted to be a number.

So, this function would search through the buffer for the specified
regexp, extract the group of digits as a string, convert them to a
number and compare them to the last one found. If the number is larger,
it would set that as the max and then continue the loop. Finally, it
adds 1. 

I can see some things I would do differently and even if you don't find
a match, your max-toc value will have a value of at least 1, but apart
from that, it seems to do whatever it was intended to do. 

Tim


-- 
tcross (at) rapttech dot com dot au


       reply	other threads:[~2011-03-08 21:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.2.1299605488.4046.help-gnu-emacs@gnu.org>
2011-03-08 21:36 ` Tim X [this message]
2011-03-08 23:48   ` use of "match-string" ken
2011-03-08 17:31 ken
2011-03-08 19:06 ` PJ Weisberg
2011-03-08 19:32   ` ken

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=87r5ahi9bi.fsf@rapttech.com.au \
    --to=timx@nospam.dev.null \
    --cc=help-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.